Browse Source

优化代码

git@h5.yoqi.me 1 year ago
parent
commit
aefdb2b3e3

+ 1 - 1
lib/main.dart

@@ -30,7 +30,7 @@ class MyApp extends StatelessWidget {
       title: '食堂App',
       debugShowCheckedModeBanner: false,
       theme: Themes.defaultTheme,
-      home: HomePage(),
+      home: const HomePage(),
     );
   }
 }

+ 4 - 6
lib/model/themes.dart

@@ -28,20 +28,18 @@ class Themes {
     indicatorColor: Colors.green,
     errorColor: const Color(0xffb00020),
     toggleableActiveColor: const Color(0xFF038873),
-    colorScheme: ColorScheme(
-      primary: const Color(0xFF038873),
-      secondary: const Color(0xFF038873),
+    colorScheme: const ColorScheme(
+      primary: Color(0xFF038873),
+      secondary: Color(0xFF038873),
       surface: Colors.white,
       background: Colors.white,
-      error: const Color(0xffb00020),
+      error: Color(0xffb00020),
       onPrimary: Colors.white,
       onSecondary: Colors.white,
       onSurface: Colors.black,
       onBackground: Colors.black,
       onError: Colors.white,
       brightness: Brightness.light,
-      primaryVariant: const Color(0xFF038873),
-      secondaryVariant: const Color(0xFF038873),
     ),
   );
   static final ThemeData lightTheme = ThemeData.light();

+ 2 - 0
lib/pages/food_list_page.dart

@@ -18,6 +18,8 @@ class FoodListPage extends StatelessWidget {
     DishModel("凑数", "元/10份", "//", "食堂窗口"),
   ];
 
+   FoodListPage({Key? key}) : super(key: key);
+
   @override
   Widget build(BuildContext context) {
     return Scaffold(

+ 1 - 3
lib/pages/search_page.dart

@@ -12,8 +12,6 @@ class SearchPage extends StatefulWidget {
 class _SearchPageState extends State<SearchPage> {
   @override
   Widget build(BuildContext context) {
-    return Container(
-      child: Text("Search Page."),
-    );
+    return const Text("Search Page.");
   }
 }

+ 8 - 6
lib/utils/sp_utils.dart

@@ -1,17 +1,20 @@
 import 'package:shared_preferences/shared_preferences.dart';
 
 class SpUtil {
-  static Future<SharedPreferences> sharedPreferences = SharedPreferences.getInstance();
+  static Future<SharedPreferences> sharedPreferences =
+      SharedPreferences.getInstance();
 
-  static Future<T> get<T>(String key) {
-    key ??= '';
+  static Future<T> get<T>(String key, T defaultValue) {
     return sharedPreferences.then((s) {
-      return s.get(key) as T;
+      if (s.get(key) == null) {
+        return defaultValue;
+      } else {
+        return s.get(key) as T;
+      }
     });
   }
 
   static Future<bool> save<T>(String key, T value) async {
-    key ??= '';
     return sharedPreferences.then((s) {
       if (value == null) {
         return s.remove(key);
@@ -37,7 +40,6 @@ class SpUtil {
   }
 
   static Future<bool> remove(String key) {
-    key ??= '';
     return sharedPreferences.then((s) {
       return s.remove(key);
     });

+ 2 - 0
lib/views/campus_listview.dart

@@ -18,6 +18,8 @@ class CampusListView extends StatefulWidget {
   ];
   late final _campusList;
 
+  CampusListView({Key? key}) : super(key: key);
+
   @override
   State<CampusListView> createState() {
     _campusList = _createCampus(campus, campusImage);

+ 9 - 8
lib/views/canteen_dish_listview.dart

@@ -8,8 +8,9 @@ class CanteenDishListView extends StatelessWidget {
   final List<DishModel> dishes;
   final void Function(int index)? onLikePressed;
   final void Function(int index)? onStarPressed;
-  CanteenDishListView(this.dishes,
-      {this.onLikePressed = null, this.onStarPressed = null});
+  const CanteenDishListView(this.dishes,
+      {Key? key, this.onLikePressed, this.onStarPressed})
+      : super(key: key);
 
   @override
   Widget build(BuildContext context) {
@@ -64,12 +65,12 @@ class _DishListViewItem extends StatelessWidget {
                                 dish.cost,
                                 textScaleFactor: 1.2,
                               ),
-                              padding: EdgeInsets.fromLTRB(0, 0, 16, 0),
+                              padding: const EdgeInsets.fromLTRB(0, 0, 16, 0),
                             )
                           ]))
                     ],
                   ),
-                  padding: EdgeInsets.fromLTRB(6, 4, 4, 4)),
+                  padding: const EdgeInsets.fromLTRB(6, 4, 4, 4)),
               Padding(
                 child: Row(
                   children: [
@@ -86,25 +87,25 @@ class _DishListViewItem extends StatelessWidget {
                               dish.time,
                               textScaleFactor: 0.9,
                             ),
-                            padding: EdgeInsets.fromLTRB(0, 0, 20, 0),
+                            padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
                           )
                         ]))
                   ],
                 ),
-                padding: EdgeInsets.fromLTRB(6, 0, 0, 6),
+                padding: const EdgeInsets.fromLTRB(6, 0, 0, 6),
               )
             ],
           ),
         ),
         IconButton(
             onPressed: () {
-              if (onLikePressed != null) onLikePressed!(this.id);
+              if (onLikePressed != null) onLikePressed!(id);
             },
             icon: Icon(dish.like ? Icons.favorite : Icons.favorite_border,
                 color: dish.like ? Colors.red : null)),
         IconButton(
             onPressed: () {
-              if (onStarPressed != null) onStarPressed!(this.id);
+              if (onStarPressed != null) onStarPressed!(id);
             },
             icon: Icon(dish.star ? Icons.star : Icons.star_border,
                 color: dish.star ? Colors.amber : null)),

+ 5 - 9
lib/views/chzn_expansion_panel_list.dart

@@ -3,7 +3,6 @@
 // 原版会在展开的项的上下添加及其难看的鸿沟
 
 import 'package:flutter/material.dart';
-import 'package:flutter/widgets.dart';
 
 const double _kPanelHeaderCollapsedHeight = kMinInteractiveDimension;
 const EdgeInsets _kPanelHeaderExpandedDefaultPadding = EdgeInsets.symmetric(
@@ -49,9 +48,7 @@ class ChznExpansionPanelList extends StatefulWidget {
     this.expandedHeaderPadding = _kPanelHeaderExpandedDefaultPadding,
     this.dividerColor,
     this.elevation = 2,
-  })  : assert(children != null),
-        assert(animationDuration != null),
-        _allowOnlyOnePanelOpen = false,
+  })  : _allowOnlyOnePanelOpen = false,
         initialOpenPanelValue = null,
         super(key: key);
   const ChznExpansionPanelList.radio({
@@ -63,9 +60,7 @@ class ChznExpansionPanelList extends StatefulWidget {
     this.expandedHeaderPadding = _kPanelHeaderExpandedDefaultPadding,
     this.dividerColor,
     this.elevation = 2,
-  })  : assert(children != null),
-        assert(animationDuration != null),
-        _allowOnlyOnePanelOpen = true,
+  })  : _allowOnlyOnePanelOpen = true,
         super(key: key);
   final List<ExpansionPanel> children;
   final ExpansionPanelCallback? expansionCallback;
@@ -148,8 +143,9 @@ class _ExpansionPanelListState extends State<ChznExpansionPanelList> {
             widget.children[childIndex] as ExpansionPanelRadio;
         if (widget.expansionCallback != null &&
             childIndex != index &&
-            child.value == _currentOpenPanel?.value)
+            child.value == _currentOpenPanel?.value) {
           widget.expansionCallback!(childIndex, false);
+        }
       }
 
       setState(() {
@@ -263,7 +259,7 @@ class _ExpansionPanelListState extends State<ChznExpansionPanelList> {
     return MergeableMaterial(
       hasDividers: true,
       dividerColor: widget.dividerColor,
-      elevation: widget.elevation.toInt(),
+      elevation: widget.elevation,
       children: items,
     );
   }

+ 154 - 8
pubspec.lock

@@ -7,7 +7,7 @@ packages:
       name: async
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.6.1"
+    version: "2.8.2"
   boolean_selector:
     dependency: transitive
     description:
@@ -21,14 +21,14 @@ packages:
       name: characters
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "1.1.0"
+    version: "1.2.0"
   charcode:
     dependency: transitive
     description:
       name: charcode
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "1.2.0"
+    version: "1.3.1"
   clock:
     dependency: transitive
     description:
@@ -57,6 +57,20 @@ packages:
       url: "https://pub.dartlang.org"
     source: hosted
     version: "1.2.0"
+  ffi:
+    dependency: transitive
+    description:
+      name: ffi
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "1.2.1"
+  file:
+    dependency: transitive
+    description:
+      name: file
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "6.1.2"
   flutter:
     dependency: "direct main"
     description: flutter
@@ -74,6 +88,18 @@ packages:
     description: flutter
     source: sdk
     version: "0.0.0"
+  flutter_web_plugins:
+    dependency: transitive
+    description: flutter
+    source: sdk
+    version: "0.0.0"
+  js:
+    dependency: transitive
+    description:
+      name: js
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "0.6.3"
   lints:
     dependency: transitive
     description:
@@ -87,14 +113,21 @@ packages:
       name: matcher
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "0.12.10"
+    version: "0.12.11"
+  material_color_utilities:
+    dependency: transitive
+    description:
+      name: material_color_utilities
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "0.1.3"
   meta:
     dependency: transitive
     description:
       name: meta
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "1.3.0"
+    version: "1.7.0"
   path:
     dependency: transitive
     description:
@@ -102,6 +135,104 @@ packages:
       url: "https://pub.dartlang.org"
     source: hosted
     version: "1.8.0"
+  path_provider_linux:
+    dependency: transitive
+    description:
+      name: path_provider_linux
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.1.7"
+  path_provider_platform_interface:
+    dependency: transitive
+    description:
+      name: path_provider_platform_interface
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.4"
+  path_provider_windows:
+    dependency: transitive
+    description:
+      name: path_provider_windows
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.7"
+  platform:
+    dependency: transitive
+    description:
+      name: platform
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "3.1.0"
+  plugin_platform_interface:
+    dependency: transitive
+    description:
+      name: plugin_platform_interface
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.1.2"
+  process:
+    dependency: transitive
+    description:
+      name: process
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "4.2.4"
+  shared_preferences:
+    dependency: "direct main"
+    description:
+      name: shared_preferences
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.15"
+  shared_preferences_android:
+    dependency: transitive
+    description:
+      name: shared_preferences_android
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.12"
+  shared_preferences_ios:
+    dependency: transitive
+    description:
+      name: shared_preferences_ios
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.1.1"
+  shared_preferences_linux:
+    dependency: transitive
+    description:
+      name: shared_preferences_linux
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.1.1"
+  shared_preferences_macos:
+    dependency: transitive
+    description:
+      name: shared_preferences_macos
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.4"
+  shared_preferences_platform_interface:
+    dependency: transitive
+    description:
+      name: shared_preferences_platform_interface
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.0"
+  shared_preferences_web:
+    dependency: transitive
+    description:
+      name: shared_preferences_web
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.4"
+  shared_preferences_windows:
+    dependency: transitive
+    description:
+      name: shared_preferences_windows
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.1.1"
   sky_engine:
     dependency: transitive
     description: flutter
@@ -148,7 +279,7 @@ packages:
       name: test_api
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "0.3.0"
+    version: "0.4.8"
   typed_data:
     dependency: transitive
     description:
@@ -162,6 +293,21 @@ packages:
       name: vector_math
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.1.0"
+    version: "2.1.1"
+  win32:
+    dependency: transitive
+    description:
+      name: win32
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.5.2"
+  xdg_directories:
+    dependency: transitive
+    description:
+      name: xdg_directories
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "0.2.0+1"
 sdks:
-  dart: ">=2.12.0 <3.0.0"
+  dart: ">=2.15.0 <3.0.0"
+  flutter: ">=2.8.0"

+ 1 - 0
pubspec.yaml

@@ -9,6 +9,7 @@ dependencies:
   flutter:
     sdk: flutter
   cupertino_icons: ^1.0.3
+  shared_preferences: ^2.0.7
 
 dev_dependencies:
   flutter_test: