Jelajahi Sumber

添加服务,接入数据库

lailin 4 tahun lalu
induk
melakukan
7da58fdbd0

+ 1 - 2
taskservice/lib/Model/JXServiceModel.dart

@@ -25,11 +25,10 @@ class JXServiceModel {
 
   Map<String, dynamic> toMap() {
     return {
-      'id': id,
       'kind': kind,
       'cycle': '$cycle',
       'cycleUnit': cycleUnit,
-      'remindDay': remindDay,
+      'remindDay': '$remindDay',
       'remark': remark,
     };
   }

+ 49 - 9
taskservice/lib/database/DataBase.dart

@@ -14,6 +14,8 @@ class MyDataBase {
   static Future<MyDataBase> get database async {
     if(_instance == null){
       _instance = MyDataBase();
+      String path = join((await Sqlite.getDatabasesPath()), 'data.db');
+      print('dbPath:${path}');
       await _instance.init();
     }
     return _instance;
@@ -21,16 +23,37 @@ class MyDataBase {
 
  Future init() async {
     _database = await Sqlite.openDatabase(join((await Sqlite.getDatabasesPath()), 'data.db'));
-    List<Map<String, dynamic>> result = await _database.rawQuery("CREATE TABLE IF NOT EXISTS $tableMembers(id INTEGER PRIMARY KEY, "
-        "name TEXT, tel TEXT, birth TEXT, prov TEXT, city TEXT, area TEXT, addr TEXT, remark TEXT)");
+    List<Map<String, dynamic>> result = await _database.rawQuery("CREATE TABLE IF NOT EXISTS $tableMembers(id INTEGER PRIMARY KEY,"
+        "name TEXT, tel TEXT, birth TEXT, prov TEXT, city TEXT, area TEXT, addr TEXT, remark TEXT, services TEXT,"
+        "createtime DATETIME DEFAULT CURRENT_TIMESTAMP,"
+        "updatetime DATETIME DEFAULT CURRENT_TIMESTAMP);"
+        "CREATE TRIGGER [updatetime]"
+        "AFTER UPDATE"
+        "ON members"
+        "FOR EACH ROW"
+        "BEGIN"
+        "UPDATE members SET updatetime = CURRENT_TIMESTAMP WHERE id = old.id;"
+        "END;");
+    List<Map<String, dynamic>> services = await _database.rawQuery("CREATE TABLE IF NOT EXISTS $tableServices(id INTEGER PRIMARY KEY, "
+        "kind TEXT, cycle INTEGER, cycleUnit TEXT, remindDay INTEGER, remark TEXT, "
+        "createtime DATETIME DEFAULT CURRENT_TIMESTAMP, "
+        "updatetime DATETIME DEFAULT CURRENT_TIMESTAMP);"
+        "CREATE TRIGGER [updatetime]"
+        "AFTER UPDATE"
+        "ON members"
+        "FOR EACH ROW"
+        "BEGIN"
+        "UPDATE members SET updatetime = CURRENT_TIMESTAMP WHERE id = old.id;"
+        "END;");
   }
 
   Future<List<JXMemberModel>> queryAllMembers() async {
     List<Map<String , dynamic>> data =  await _database.query("$tableMembers");
-    return data.map((e) =>
-       JXMemberModel()..id = e['id']..name = e['name']..tel = e['tel']
-    ).toList();
-
+    return data.map((e) => JXMemberModel()
+          ..id = e['id']
+          ..name = e['name']
+          ..tel = e['tel'])
+        .toList();
   }
 
   Future<void> saveMember(JXMemberModel jxMemberModel) async {
@@ -41,8 +64,25 @@ class MyDataBase {
     );
   }
 
-
-  Future<void> saveService(JXMemberModel jxMemberModel, JXServiceModel jxServiceModel) {
-
+  /*服务*/
+  Future<List <JXServiceModel>> queryAllServices() async {
+    List<Map<String , dynamic>> data = await _database.query("$tableServices");
+    return data.map((e) => JXServiceModel()
+          ..id = e['id']
+          ..kind = e['kind']
+          ..cycle = e['cycle']
+          ..cycleUnit = e['cycleUnit']
+          ..remindDay = e['remindDay']
+          ..remark = e['remark'])
+        .toList();
+  }
+  /*添加新服务*/
+  Future<JXServiceModel> saveService(JXServiceModel jxServiceModel) async {
+    await _database.insert(
+      tableServices,
+      jxServiceModel.toMap(),
+      conflictAlgorithm: ConflictAlgorithm.replace,
+    );
+    return null;
   }
 }

+ 22 - 6
taskservice/lib/src/service/page_addservice.dart

@@ -1,9 +1,11 @@
 import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
+import 'package:fluttertoast/fluttertoast.dart';
 import 'package:taskservice/Common/JXColors.dart';
 import 'package:taskservice/Common/JXCommon.dart';
 import 'package:taskservice/Model/JXServiceModel.dart';
+import 'package:taskservice/database/DataBase.dart';
 import 'package:taskservice/src/custom_widgets.dart';
 
 class CreateAddService extends StatefulWidget {
@@ -21,11 +23,11 @@ class _CreateAddService extends State<CreateAddService> {
   /*类型*/
   TextEditingController _kindController = TextEditingController(text: '');
   /*周期*/
-  final TextEditingController _cycleController = TextEditingController(text: '');
+  TextEditingController _cycleController = TextEditingController(text: '0');
   /*提醒*/
-  final TextEditingController _remandController = TextEditingController(text: '0');
+  TextEditingController _remandController = TextEditingController(text: '0');
   /*备注*/
-  final TextEditingController _remarkConotroller = TextEditingController(text: '');
+  TextEditingController _remarkConotroller = TextEditingController(text: '');
 
   @override
   void initState() {
@@ -74,7 +76,7 @@ class _CreateAddService extends State<CreateAddService> {
                         Expanded(
                           child: SizedBox(width: 0,),
                         ),
-                        theInputDayView('周期', '月', _cycleController),
+                        theInputDayView('周期', '月', _cycleController),
                         theInputDayView('提醒', '日', _remandController),
                       ],
                     ),
@@ -198,13 +200,27 @@ class _CreateAddService extends State<CreateAddService> {
 
   /**点击事件**/
   /*保存*/
-  void saveBtnClickAction() {
+  Future<void> saveBtnClickAction() async {
     _service.kind = _kindController.text;
     _service.cycle = int.parse(_cycleController.text);
     _service.remindDay = int.parse(_remandController.text);
     _service.remark = _remarkConotroller.text;
     _service.cycleUnit = '月';
-    _service.id = 12;
+
+    /*检查数据*/
+    if (_service.kind.length == 0) {
+      Fluttertoast.showToast(
+        msg: "This is Center Short Toast",
+        toastLength: Toast.LENGTH_SHORT,
+        gravity: ToastGravity.CENTER,
+        timeInSecForIos: 1,
+        backgroundColor: JXColors.k1F2529,
+        textColor: JXColors.kFFFFFF,
+      );
+      return;
+    }
+    /*添加到数据库中,从数据库中取值返回*/
+    (await MyDataBase.database).saveService(_service);
     print(_service.toMap());
     Navigator.of(context).pop(_service);
   }

+ 22 - 36
taskservice/lib/src/service/page_servicelist.dart

@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 import 'package:taskservice/Common/JXColors.dart';
 import 'package:taskservice/Model/JXServiceModel.dart';
+import 'package:taskservice/database/DataBase.dart';
 import 'package:taskservice/src/service/page_addservice.dart';
 import '../custom_widgets.dart';
 
@@ -23,39 +24,7 @@ class _CreateServiceList extends State<CreateServiceList> {
   void initState() {
     // TODO: implement initState
     super.initState();
-    _baseServices = [
-      JXServiceModel()
-        ..id = 1
-        ..kind = '清洗'
-        ..cycle = 2
-        ..cycleUnit = '月'
-        ..remark = '带清洁工具',
-      JXServiceModel()
-        ..id = 2
-        ..kind = '除胶'
-        ..cycle = 1
-        ..cycleUnit = '月'
-        ..remark = '带清洁工具',
-      JXServiceModel()
-        ..id = 3
-        ..kind = '换水'
-        ..cycle = 1
-        ..cycleUnit = '月'
-        ..remark = '带清洁工具',
-      JXServiceModel()
-        ..id = 4
-        ..kind = '换滤芯'
-        ..cycle = 4
-        ..cycleUnit = '月'
-        ..remark = '带清洁工具',
-      JXServiceModel()
-        ..id = 5
-        ..kind = '换电机'
-        ..cycle = 5
-        ..cycleUnit = '月'
-        ..remark = '带清洁工具',
-    ];
-    filterSevice();
+    updateServices();
   }
   @override
   Widget build(BuildContext context) {
@@ -188,6 +157,24 @@ class _CreateServiceList extends State<CreateServiceList> {
     );
   }
 
+  /*刷新服务项列表*/
+  void updateServices() {
+    MyDataBase.database.then((db) async {
+      List <JXServiceModel> tmpItems = (await db.queryAllServices());
+      for (JXServiceModel service in tmpItems) {
+        for (JXServiceModel sub in _baseServices) {
+          if (service.id == sub.id) {
+            service.isCheck = sub.isCheck;
+          }
+        }
+      }
+
+      _baseServices.clear();
+      _baseServices.addAll(tmpItems);
+      filterSevice();
+    });
+  }
+
   /*筛选服务*/
   void filterSevice() {
     String text = _searchBar.text;
@@ -225,9 +212,8 @@ class _CreateServiceList extends State<CreateServiceList> {
   void addBtnClickAction() {
     Navigator.of(context).pushNamed('$CreateAddService', arguments: '').then((data) {
       if (data != null) {
-        setState(() {
-          _baseServices.add(data);
-        });
+        /*刷新数据显示*/
+        updateServices();
       }
     });
   }

+ 1 - 0
taskservice/pubspec.yaml

@@ -29,6 +29,7 @@ dependencies:
   # The following adds the Cupertino Icons font to your application.
   # Use with the CupertinoIcons class for iOS style icons.
   cupertino_icons: ^0.1.3
+  fluttertoast: ^2.1.1
 
 dev_dependencies:
   flutter_test: