liuyuqi-dellpc 1 year ago
parent
commit
dbb9a4dd07
4 changed files with 124 additions and 2 deletions
  1. 82 0
      lib/pages/addpage_page.dart
  2. 10 1
      lib/pages/home_page.dart
  3. 21 0
      lib/provider/car_provider.dart
  4. 11 1
      lib/routes.dart

+ 82 - 0
lib/pages/addpage_page.dart

@@ -0,0 +1,82 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_provider_demo/model/car_model.dart';
+import 'package:flutter_provider_demo/provider/car_provider.dart';
+import 'package:provider/provider.dart';
+
+/// Description: add cars
+/// Time       : 09/04/2023 Monday
+/// Author     : liuyuqi.gov@msn.cn
+class AddcardPage extends StatefulWidget {
+  const AddcardPage({super.key});
+
+  @override
+  State<AddcardPage> createState() => _AddcardPageState();
+}
+
+class _AddcardPageState extends State<AddcardPage> {
+  final carBrandController = TextEditingController();
+  final carTypeController = TextEditingController();
+  bool started = false;
+
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      appBar: AppBar(
+        actions: [],
+        title: Text("增加车辆"),
+      ),
+      body: ListView(children: [
+        Padding(
+          padding: EdgeInsets.all(25),
+          child: Column(
+            children: [
+              TextField(
+                controller: carBrandController,
+              ),
+              TextField(
+                controller: carTypeController,
+              ),
+              CheckboxListTile(
+                value: started,
+                onChanged: (checked) {
+                  setState(() {
+                    started = checked;
+                  });
+                },
+                title: const Text('Start ?'),
+              ),
+              RaisedButton(
+                elevation: 5.0,
+                child: Text('Add'),
+                onPressed: onAdd,
+                shape: RoundedRectangleBorder(
+                    borderRadius: BorderRadius.circular(30.0)),
+              ),
+            ],
+            crossAxisAlignment: CrossAxisAlignment.stretch,
+          ),
+        )
+      ]),
+    );
+  }
+
+  @override
+  void dispose() {
+    super.dispose();
+    carBrandController.dispose();
+  }
+
+  /// add car
+  void addCar(int id) {
+    if (carBrandController.text.isNotEmpty &&
+        carTypeController.text.isNotEmpty) {
+      final CarModel car = CarModel(
+          id: id,
+          brand: carBrandController.text,
+          type: carTypeController.text,
+          start: started);
+      Provider.of<CarProvider>(context, listen: false).addCar(car);
+      Navigator.pop(context);
+    }
+  }
+}

+ 10 - 1
lib/pages/home_page.dart

@@ -1,4 +1,5 @@
 import 'package:flutter/material.dart';
+import 'package:flutter_provider_demo/routes.dart';
 
 /// Description: home page
 /// Time       : 09/03/2023 Sunday
@@ -13,6 +14,14 @@ class MyWidget extends StatefulWidget {
 class _MyWidgetState extends State<MyWidget> {
   @override
   Widget build(BuildContext context) {
-    return const Scaffold();
+    return const Scaffold(
+      appBar: AppBar(actions: [
+        IconButton(icon: Icon(Icons.add), onPressed: () {
+          Navigator.pushNamed(context, Routes.carAdd);
+        }),
+        }),
+      ], title: Text('汽车列表'),),
+      ], title: Text('汽车列表'),'))]),
+    );
   }
 }

+ 21 - 0
lib/provider/car_provider.dart

@@ -20,4 +20,25 @@ class CarProvider extends ChangeNotifier {
     _cars.remove(car);
     notifyListeners();
   }
+
+  /// add car
+  void addCar(CarModel car) async {
+    final id = await CarDao.addCar(car);
+    if (id > 0) {
+      _cars.add(car);
+      notifyListeners();
+    }
+  }
+
+  void _showSnackBar(BuildContext context, String message) {
+    final snackBar = SnackBar(
+      duration: const Duration(milliseconds: 500),
+      content: Text(
+        message,
+        style: const TextStyle(color: Colors.white),
+      ),
+      backgroundColor: Theme.of(context).primaryColor,
+    );
+    ScaffoldMessenger.of(context).showSnackBar(snackBar);
+  }
 }

+ 11 - 1
lib/routes.dart

@@ -8,7 +8,7 @@ class Routes {
   static const String home = '/';
   static const String index = '/index';
   static const String login = '/login';
-
+  static const String carAdd = "/carAdd";
   static MaterialPageRoute<dynamic> generateRoute(RouteSettings settings) {
     switch (settings.name) {
       case home:
@@ -21,6 +21,16 @@ class Routes {
           builder: (_) => const IndexPage(),
           settings: settings,
         );
+      case login:
+        return MaterialPageRoute<dynamic>(
+          builder: (_) => const IndexPage(),
+          settings: settings,
+        );
+      case carAdd:
+        return MaterialPageRoute<dynamic>(
+          builder: (_) => const AddcardPage(),
+          settings: settings,
+        );
       default:
         return MaterialPageRoute<dynamic>(
           builder: (_) => const Scaffold(),