Sqllite yordamida mobil dastur tayyorlash


Download 140.02 Kb.
bet1/2
Sana18.06.2023
Hajmi140.02 Kb.
#1596429
  1   2
Bog'liq
4 - AMALIY ISH


SQLlite YORDAMIDA MOBIL DASTUR TAYYORLASH
Flutterda SQLlite yordamida baza bilan ishlavchi va kirim – chiqim hisobotlarini yozib qoldirish uchun ishlatilinadigan mobil dastur tuzishni ko’rib chiqamiz bizga birinchi o’rinda, Flutter frameworkida ishlash uchun muhit va ma’lumotlar bazasi bilan ishlash uchun SQLFlite package kerak bo’ladi. Bu package Flutter Frameworkida SQLlite bilan ishlash uchun yozilgan. Dasturni boshlash uchun birinchi o’rinda project yaratib olamiz va unga nom beramiz.

1-rasm
F1 tugamasini bosamiz va suratdagi kabi yuqaridan menu ochiladi. Biz Flutter: New Project ni tanlaymiz va keying menuga o’tamiz. Bu menyudan biz Application bo’limini tanlaymiz.

Shundan so’ng bizga Explorer bo’limi ochiladi. So’ngra Explorer bo’limidan o’zimizga kerakli papkani yaratamiz va tanlaymiz.

Explorer bo’limidan papka yaratib olganimizdan so’ng bizda yangi dastur yozish uchun muhitni tayyorlash jarayoni boshlanadi va birinchi o’rinda bizga Increment va decremrnt dasturi yozilgan kodlar ko’rinadi. Increment va decrement dasturni MyApp classi va main() funksiyasidan boshqa kodlarni ‘chirib tashlaymiz chunki u kodlar bizga kerak bo’lmaydi. So’ngra biz main() function va MyApp classi tuzilishini o’zimizning dasturga kerak bo’ladigan qismlarini yozamiz.

void main() {


runApp(const MyApp());
}

class MyApp extends StatelessWidget {


const MyApp({super.key});


// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

Va kodimiz menu qism uchun quyidagi ko’rinishdagi holatgacha qisqaradi. Bu yerda main() funksiyasi dasturni ishga tushirish uchun kerak bo’ladigan asosiy funksiya hisoblanadi. MyApp classi esa dasturning asosiy widegeti hisoblanib, bu yerda bizga StateLessWidget ya’ni qayta chizilmaydigan widget yordamida dasturning boshlanishini ta’minlaydi. Ya’ni MyApp classiga bizning App uchun kerak bo’ladigan birinchi page chaqiriladi va keyin esa Navigator.push(context) methodi yordamida keying pagelarga o’tilinadi. Navigator.pop(context) yordamida esa asosiy oldingi pagega qaytilinadi. Va biz birinchi classni yaratib o’z dasturimizni tayyorlashni boshlaymiz.


Dasturimizda pagelar ko’pligi tufayli turli hildagi pagelar alishtirish uchun mo’ljallangan widgetlardan yoki methodlardan foydalanishimiz mumkin. Men bu dasturda Route widget yordamida pagelarni alishtiraman. Buning afzallik jihati shundaki bizga bir class ichida barcha pagelarni alishtirish imkoniyati tug’iladi.
class Routes {
static const viewPage = '/viewPage';
static const addDataPage = '/addDataPage';
static const editData = '/editData';
static const settings = '/settings';

static Route generateRoute(RouteSettings routeSettings) {


try {
final Map? args =
routeSettings.arguments as Map?;
args ?? {};
switch (routeSettings.name) {
case viewPage:
return MaterialPageRoute(
settings: routeSettings,
builder: (_) => const ViewPage(),
);
case addDataPage:
return MaterialPageRoute(
settings: routeSettings,
builder: (_) => AddData(
isTrue: args?['isTrue'],
),
);
case editData:
return MaterialPageRoute(
settings: routeSettings,
builder: (_) => EditData(
user: args?['user'],
),
);
case settings:
return MaterialPageRoute(
settings: routeSettings,
builder: (_) => SettingsPage(),
);
default:
return MaterialPageRoute(
settings: routeSettings,
builder: (_) => const ViewPage(),
);
}
} catch (e) {
return MaterialPageRoute(
settings: routeSettings,
builder: (_) => const ViewPage(),
);
}
}
}

Yuqorida bizning dasturimiz uchun kerak bo’ladigan Routes classi keltirilgan bo’lib bu yerda asosiy narsa static Route generateRoute(RouteSettings routeSettings) methodi hisoblanib bu yerda biz switch yordamida pagelarni alishtiramiz.


static const viewPage = '/viewPage';

Bu yerda biz static ko’rinishda bizga kerakli page uchun o’zgaruvchi ochib olamiz va u String tipida bo’ladi.



Yuqorida bizning dasturimiz uchun kerak bo’ladigan fayllar to’plami va turli hildagi color va Text lar uchun kerakli fayllar fayl structure ga amal qilgan holda, clean architecture ga mos ravishga papka va fayllarga bo’linadi.



Bu dasturimizning asosiy sahifasi hisoblanib, bu yerda biz rashot va daxotlarni hisoblashimiz mumkin.

class HomePage extends StatefulWidget {


const HomePage({Key? key}) : super(key: key);

@override


State createState() => _HomePageState();
}

class _HomePageState extends State


with SingleTickerProviderStateMixin {
late List _userList = [];
final _userService = IncomeService();
@override
void initState() {
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 260),
);

final curvedAnimation =


CurvedAnimation(curve: Curves.easeInOut, parent: _animationController!);
_animation = Tween(begin: 0, end: 1).animate(curvedAnimation);

super.initState();


}

getAllUserDetails() async {


var users = await _userService.readAllData();
_userList = [];
users.forEach((user) {
setState(() {
var userModel = IncomeExpensesModel();
userModel.id = user['id'];
userModel.desc = user['desc'];
userModel.price = user['price'];
_userList.add(userModel);
});
});
}

Animation? _animation;


AnimationController? _animationController;
@override
void dispose() {
_animationController!.dispose();
super.dispose();
}

@override


Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.menu),
onPressed: () {
ZoomDrawer.of(context)!.toggle();
},
),
title: const Text("Wallefy"),
actions: [
IconButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (_) => HistoryPage()));
},
icon: Icon(Icons.history))
],
),
body: ListView(
children: [
TableWidget(),
],
),
floatingActionButton: FloatActionButton(
animationController: _animationController, animation: _animation),
);
}
}

Bu pageda bizning yuqoridagi rasmda keltirilgan sahifamizning kod qismi joylashgan bo’lib. StateFullWidget yordamida chizilgan. Bu widget yordamida biz pageimizni yangilash imokoniyatiga ega bo’lamiz. StateFullWidgetda pagelar secundiga 60 marta chizilish imkoniyatiga ega.


class DatabaseConnection {
Future setDatabase() async {
var directory = await getApplicationDocumentsDirectory();
var path = join(directory.path, 'db_crud');
var database =
await openDatabase(path, version: 1, onCreate: _createDatabase);
return database;
}

Future _createDatabase(Database database, int version) async {


String sql =
"CREATE TABLE mydb(id INTEGER PRIMARY KEY AUTOINCREMENT, type TEXT NOT NULL , desc TEXT NOT NULL, price REAL NOT NULL, datatime TEXT NOT NULL, isincome INTEGER NOT NULL)";
await database.execute(sql);
}
}

Bu kod bizni DataBase bilan bog’lash uchun kerak bo;luvchi kod hisoblanadi.


class IncomeService {
late Repository _repository;
IncomeService() {
_repository = Repository();
}

Download 140.02 Kb.

Do'stlaringiz bilan baham:
  1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling