Browse Source

add home_page.dart

liuyuqi-dellpc 1 year ago
parent
commit
0fef070619
7 changed files with 487 additions and 10 deletions
  1. 3 2
      lib/main.dart
  2. 1 1
      lib/models/api.dart
  3. 30 1
      lib/models/config.dart
  4. 0 2
      lib/pages/home_page.dart
  5. 172 0
      lib/utils/sp_util.dart
  6. 270 0
      pubspec.lock
  7. 11 4
      pubspec.yaml

+ 3 - 2
lib/main.dart

@@ -1,11 +1,12 @@
 import 'package:flutter/material.dart';
+import 'package:flutter_news/models/config.dart';
 import 'package:flutter_news/pages/home_page.dart';
 
 /// Description: 主页
 /// Time       : 07/05/2023 Wednesday
 /// Author     : liuyuqi.gov@msn.cn
 void main() {
-  runApp(const MyApp());
+  Config.init(() => runApp(const MyApp()));
 }
 
 class MyApp extends StatelessWidget {
@@ -16,7 +17,7 @@ class MyApp extends StatelessWidget {
   Widget build(BuildContext context) {
     return MaterialApp(
       debugShowCheckedModeBanner: false,
-      title: 'Flutter Demo',
+      title: 'News',
       theme: ThemeData(
         primarySwatch: Colors.blue,
       ),

+ 1 - 1
lib/models/api.dart

@@ -2,5 +2,5 @@ class Api {
   static const String _host = "https://xx.com/news";
 
   // 登录
-  static const String login = _host + "login";
+  static const String login = "${_host}login";
 }

+ 30 - 1
lib/models/config.dart

@@ -1,6 +1,35 @@
+import 'dart:io';
+
+import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
+import 'package:flutter_news/utils/sp_util.dart';
+
 /// Description: 全局配置
 /// Time       : 07/05/2023 Wednesday
 /// Author     : liuyuqi.gov@msn.cn
 class Config {
-  //
+  static late String cachePath;
+
+  // 设计稿尺寸
+  static const UI_W = 375.0;
+  static const UI_H = 812.0;
+
+  static Future init(Function() runApp) async {
+    WidgetsFlutterBinding.ensureInitialized();
+    // cachePath= await getApplicationDocumentsDirectory().then((value) => value.path);
+    await SpUtil.getInstance();
+
+    runApp();
+
+    SystemChrome.setPreferredOrientations(
+        [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
+
+    // 状态栏透明(Android)
+    var brightness = Platform.isAndroid ? Brightness.dark : Brightness.light;
+    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
+      statusBarColor: Colors.transparent,
+      statusBarBrightness: brightness,
+      statusBarIconBrightness: brightness,
+    ));
+  }
 }

+ 0 - 2
lib/pages/home_page.dart

@@ -1,6 +1,4 @@
 import 'package:flutter/material.dart';
-import 'package:flutter/src/widgets/framework.dart';
-import 'package:flutter/src/widgets/placeholder.dart';
 
 class HomePage extends StatefulWidget {
   const HomePage({super.key});

+ 172 - 0
lib/utils/sp_util.dart

@@ -0,0 +1,172 @@
+import 'dart:async';
+import 'dart:convert';
+
+import 'package:shared_preferences/shared_preferences.dart';
+import 'package:synchronized/synchronized.dart';
+
+class SpUtil {
+  static SpUtil? _singleton;
+  static SharedPreferences? _prefs;
+  static Lock _lock = Lock();
+
+  static Future<SpUtil?> getInstance() async {
+    if (_singleton == null) {
+      await _lock.synchronized(() async {
+        if (_singleton == null) {
+          // keep local instance till it is fully initialized.
+          // 保持本地实例直到完全初始化。
+          var singleton = SpUtil._();
+          await singleton._init();
+          _singleton = singleton;
+        }
+      });
+    }
+    return _singleton;
+  }
+
+  SpUtil._();
+
+  Future _init() async {
+    _prefs = await SharedPreferences.getInstance();
+  }
+
+  /// put object.
+  static Future<bool>? putObject(String key, Object value) {
+    return _prefs?.setString(key, json.encode(value));
+  }
+
+  /// get obj.
+  static T? getObj<T>(String key, T f(Map v), {T? defValue}) {
+    Map? map = getObject(key);
+    return map == null ? defValue : f(map);
+  }
+
+  /// get object.
+  static Map? getObject(String key) {
+    String? _data = _prefs?.getString(key);
+    return (_data == null || _data.isEmpty) ? null : json.decode(_data);
+  }
+
+  /// put object list.
+  static Future<bool>? putObjectList(String key, List<Object> list) {
+    List<String>? _dataList = list.map((value) {
+      return json.encode(value);
+    }).toList();
+    return _prefs?.setStringList(key, _dataList);
+  }
+
+  /// get obj list.
+  static List<T>? getObjList<T>(String key, T f(Map v),
+      {List<T>? defValue = const []}) {
+    List<Map>? dataList = getObjectList(key);
+    List<T>? list = dataList?.map((value) {
+      return f(value);
+    }).toList();
+    return list ?? defValue;
+  }
+
+  /// get object list.
+  static List<Map>? getObjectList(String key) {
+    List<String>? dataLis = _prefs?.getStringList(key);
+    return dataLis?.map((value) {
+      Map _dataMap = json.decode(value);
+      return _dataMap;
+    }).toList();
+  }
+
+  /// get string.
+  static String? getString(String key, {String? defValue = ''}) {
+    return _prefs?.getString(key) ?? defValue;
+  }
+
+  /// put string.
+  static Future<bool>? putString(String key, String value) {
+    return _prefs?.setString(key, value);
+  }
+
+  /// get bool.
+  static bool? getBool(String key, {bool? defValue = false}) {
+    return _prefs?.getBool(key) ?? defValue;
+  }
+
+  /// put bool.
+  static Future<bool>? putBool(String key, bool value) {
+    return _prefs?.setBool(key, value);
+  }
+
+  /// get int.
+  static int? getInt(String key, {int? defValue = 0}) {
+    return _prefs?.getInt(key) ?? defValue;
+  }
+
+  /// put int.
+  static Future<bool>? putInt(String key, int value) {
+    return _prefs?.setInt(key, value);
+  }
+
+  /// get double.
+  static double? getDouble(String key, {double? defValue = 0.0}) {
+    return _prefs?.getDouble(key) ?? defValue;
+  }
+
+  /// put double.
+  static Future<bool>? putDouble(String key, double value) {
+    return _prefs?.setDouble(key, value);
+  }
+
+  /// get string list.
+  static List<String>? getStringList(String key,
+      {List<String>? defValue = const []}) {
+    return _prefs?.getStringList(key) ?? defValue;
+  }
+
+  /// put string list.
+  static Future<bool>? putStringList(String key, List<String> value) {
+    return _prefs?.setStringList(key, value);
+  }
+
+  /// get dynamic.
+  static dynamic getDynamic(String key, {Object? defValue}) {
+    return _prefs?.get(key) ?? defValue;
+  }
+
+  /// have key.
+  static bool? haveKey(String key) {
+    return _prefs?.getKeys().contains(key);
+  }
+
+  /// contains Key.
+  static bool? containsKey(String key) {
+    return _prefs?.containsKey(key);
+  }
+
+  /// get keys.
+  static Set<String>? getKeys() {
+    return _prefs?.getKeys();
+  }
+
+  /// remove.
+  static Future<bool>? remove(String key) {
+    return _prefs?.remove(key);
+  }
+
+  /// clear.
+  static Future<bool>? clear() {
+    return _prefs?.clear();
+  }
+
+  /// Fetches the latest values from the host platform.
+  static Future<void>? reload() {
+    return _prefs?.reload();
+  }
+
+  ///Sp is initialized.
+  static bool isInitialized() {
+    return _prefs != null;
+  }
+
+  /// get Sp.
+  static SharedPreferences? getSp() {
+    return _prefs;
+  }
+}

+ 270 - 0
pubspec.lock

@@ -57,6 +57,22 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "1.3.1"
+  ffi:
+    dependency: transitive
+    description:
+      name: ffi
+      sha256: ed5337a5660c506388a9f012be0288fb38b49020ce2b45fe1f8b8323fe429f99
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.2"
+  file:
+    dependency: transitive
+    description:
+      name: file
+      sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "6.1.4"
   flutter:
     dependency: "direct main"
     description: flutter
@@ -70,11 +86,32 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "2.0.2"
+  flutter_screenutil:
+    dependency: "direct main"
+    description:
+      name: flutter_screenutil
+      sha256: "5e8f5197b4d0ec4b70d1c7afe055b18bba6ac27c00b309a98c25c11ba64eedf1"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "5.5.4"
   flutter_test:
     dependency: "direct dev"
     description: flutter
     source: sdk
     version: "0.0.0"
+  flutter_web_plugins:
+    dependency: transitive
+    description: flutter
+    source: sdk
+    version: "0.0.0"
+  get:
+    dependency: "direct main"
+    description:
+      name: get
+      sha256: "2ba20a47c8f1f233bed775ba2dd0d3ac97b4cf32fc17731b3dfc672b06b0e92a"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "4.6.5"
   js:
     dependency: transitive
     description:
@@ -123,6 +160,206 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "1.8.2"
+  path_provider:
+    dependency: "direct main"
+    description:
+      name: path_provider
+      sha256: "050e8e85e4b7fecdf2bb3682c1c64c4887a183720c802d323de8a5fd76d372dd"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.11"
+  path_provider_android:
+    dependency: transitive
+    description:
+      name: path_provider_android
+      sha256: "2cec049d282c7f13c594b4a73976b0b4f2d7a1838a6dd5aaf7bd9719196bee86"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.27"
+  path_provider_ios:
+    dependency: transitive
+    description:
+      name: path_provider_ios
+      sha256: "03d639406f5343478352433f00d3c4394d52dac8df3d847869c5e2333e0bbce8"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.11"
+  path_provider_linux:
+    dependency: transitive
+    description:
+      name: path_provider_linux
+      sha256: ffbb8cc9ed2c9ec0e4b7a541e56fd79b138e8f47d2fb86815f15358a349b3b57
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.1.11"
+  path_provider_macos:
+    dependency: transitive
+    description:
+      name: path_provider_macos
+      sha256: cd57cb98a30ce9d12fdd1896d9d3b0517ce689f942de6ccd2708cd39b3d18a7c
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.7"
+  path_provider_platform_interface:
+    dependency: transitive
+    description:
+      name: path_provider_platform_interface
+      sha256: "57585299a729335f1298b43245842678cb9f43a6310351b18fb577d6e33165ec"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.6"
+  path_provider_windows:
+    dependency: transitive
+    description:
+      name: path_provider_windows
+      sha256: "1cb68ba4cd3a795033de62ba1b7b4564dace301f952de6bfb3cd91b202b6ee96"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.1.7"
+  permission_handler:
+    dependency: "direct main"
+    description:
+      name: permission_handler
+      sha256: "551f8c3398f1ff26aed2ccc8d0b4e4f905cd7b1714e6a688e0b870621ad33524"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "10.1.0"
+  permission_handler_android:
+    dependency: transitive
+    description:
+      name: permission_handler_android
+      sha256: "3b61f3da3b1c83bc3fb6a2b431e8dab01d0e5b45f6a3d9c7609770ec88b2a89e"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "10.3.0"
+  permission_handler_apple:
+    dependency: transitive
+    description:
+      name: permission_handler_apple
+      sha256: "0d1f8007b17573ff1fbeae0f04b6c8e83e1d2f6c4fe8e8226d4d2456aa8ecffe"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "9.1.2"
+  permission_handler_platform_interface:
+    dependency: transitive
+    description:
+      name: permission_handler_platform_interface
+      sha256: "79b36d93a41a4aecfd0d635d77552f327cb84227c718ce5e68b5f7b85546fe7e"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "3.11.0+1"
+  permission_handler_windows:
+    dependency: transitive
+    description:
+      name: permission_handler_windows
+      sha256: cc074aace208760f1eee6aa4fae766b45d947df85bc831cde77009cdb4720098
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "0.1.3"
+  platform:
+    dependency: transitive
+    description:
+      name: platform
+      sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "3.1.0"
+  plugin_platform_interface:
+    dependency: transitive
+    description:
+      name: plugin_platform_interface
+      sha256: "6a2128648c854906c53fa8e33986fc0247a1116122f9534dd20e3ab9e16a32bc"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.1.4"
+  process:
+    dependency: transitive
+    description:
+      name: process
+      sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "4.2.4"
+  pull_to_refresh:
+    dependency: "direct main"
+    description:
+      name: pull_to_refresh
+      sha256: bbadd5a931837b57739cf08736bea63167e284e71fb23b218c8c9a6e042aad12
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.0"
+  rxdart:
+    dependency: "direct main"
+    description:
+      name: rxdart
+      sha256: "5d22055fd443806c03ef24a02000637cf51eae49c2a0168d38a43fc166b0209c"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "0.27.5"
+  shared_preferences:
+    dependency: "direct main"
+    description:
+      name: shared_preferences
+      sha256: "76917b7d4b9526b2ba416808a7eb9fb2863c1a09cf63ec85f1453da240fa818a"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.15"
+  shared_preferences_android:
+    dependency: transitive
+    description:
+      name: shared_preferences_android
+      sha256: fe8401ec5b6dcd739a0fe9588802069e608c3fdbfd3c3c93e546cf2f90438076
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.2.0"
+  shared_preferences_ios:
+    dependency: transitive
+    description:
+      name: shared_preferences_ios
+      sha256: "585a14cefec7da8c9c2fb8cd283a3bb726b4155c0952afe6a0caaa7b2272de34"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.1.1"
+  shared_preferences_linux:
+    dependency: transitive
+    description:
+      name: shared_preferences_linux
+      sha256: "71d6806d1449b0a9d4e85e0c7a917771e672a3d5dc61149cc9fac871115018e1"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.3.0"
+  shared_preferences_macos:
+    dependency: transitive
+    description:
+      name: shared_preferences_macos
+      sha256: "81b6a60b2d27020eb0fc41f4cebc91353047309967901a79ee8203e40c42ed46"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.5"
+  shared_preferences_platform_interface:
+    dependency: transitive
+    description:
+      name: shared_preferences_platform_interface
+      sha256: "23b052f17a25b90ff2b61aad4cc962154da76fb62848a9ce088efe30d7c50ab1"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.3.0"
+  shared_preferences_web:
+    dependency: transitive
+    description:
+      name: shared_preferences_web
+      sha256: "7347b194fb0bbeb4058e6a4e87ee70350b6b2b90f8ac5f8bd5b3a01548f6d33a"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.2.0"
+  shared_preferences_windows:
+    dependency: transitive
+    description:
+      name: shared_preferences_windows
+      sha256: f95e6a43162bce43c9c3405f3eb6f39e5b5d11f65fab19196cf8225e2777624d
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.3.0"
   sky_engine:
     dependency: transitive
     description: flutter
@@ -136,6 +373,14 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "1.9.1"
+  sp_util:
+    dependency: "direct main"
+    description:
+      name: sp_util
+      sha256: "9da43dce5de79c17a787d0626bf01538d63090ca32521200d22a232171c495dc"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "2.0.3"
   stack_trace:
     dependency: transitive
     description:
@@ -160,6 +405,14 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "1.2.0"
+  synchronized:
+    dependency: transitive
+    description:
+      name: synchronized
+      sha256: "5fcbd27688af6082f5abd611af56ee575342c30e87541d0245f7ff99faa02c60"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "3.1.0"
   term_glyph:
     dependency: transitive
     description:
@@ -184,5 +437,22 @@ packages:
       url: "https://pub.flutter-io.cn"
     source: hosted
     version: "2.1.4"
+  win32:
+    dependency: transitive
+    description:
+      name: win32
+      sha256: "5a751eddf9db89b3e5f9d50c20ab8612296e4e8db69009788d6c8b060a84191c"
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "4.1.4"
+  xdg_directories:
+    dependency: transitive
+    description:
+      name: xdg_directories
+      sha256: ee1505df1426458f7f60aac270645098d318a8b4766d85fde75f76f2e21807d1
+      url: "https://pub.flutter-io.cn"
+    source: hosted
+    version: "1.0.0"
 sdks:
   dart: ">=2.19.2 <3.0.0"
+  flutter: ">=3.3.0"

+ 11 - 4
pubspec.yaml

@@ -1,5 +1,5 @@
 name: flutter_news
-description: A new Flutter project.
+description: 新闻客户端app.
 publish_to: 'none' # Remove this line if you wish to publish to pub.dev
 version: 1.0.0+1
 
@@ -9,8 +9,15 @@ environment:
 dependencies:
   flutter:
     sdk: flutter
-  cupertino_icons: ^1.0.2
-
+  cupertino_icons: ^1.0.5
+  flutter_screenutil: ^5.5.4
+  get: ^4.6.5
+  shared_preferences: ^2.0.15
+  path_provider: ^2.0.11
+  rxdart: ^0.27.5
+  permission_handler: ^10.1.0
+  pull_to_refresh: ^2.0.0
+  
 dev_dependencies:
   flutter_test:
     sdk: flutter
@@ -19,4 +26,4 @@ dev_dependencies:
 flutter:
   uses-material-design: true
   assets:
-    - assets/
+    - assets/