init
This commit is contained in:
30
lib/global.dart
Normal file
30
lib/global.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import './utils/utils.dart';
|
||||
|
||||
class Global {
|
||||
static String appName = "Flutter Demo";
|
||||
|
||||
// 是否 release
|
||||
static bool get isRelease => const bool.fromEnvironment("dart.vm.product");
|
||||
|
||||
// 全局路由观察者
|
||||
static RouteObserver<Route> routeObserver = RouteObserver();
|
||||
|
||||
static Future init() async {
|
||||
// 运行初始
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// 本地存储初始化
|
||||
await StorageUtil().init();
|
||||
|
||||
// android 状态栏为透明的沉浸
|
||||
if (Platform.isAndroid) {
|
||||
SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
|
||||
statusBarColor: Colors.transparent,
|
||||
);
|
||||
SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
|
||||
}
|
||||
}
|
||||
}
|
||||
6
lib/initial_binding.dart
Normal file
6
lib/initial_binding.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class InitialBinding implements Bindings {
|
||||
@override
|
||||
void dependencies() {}
|
||||
}
|
||||
59
lib/main.dart
Normal file
59
lib/main.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:flutter_jdt_store/router/app_pages.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:statusbarz/statusbarz.dart';
|
||||
|
||||
import 'global.dart';
|
||||
import 'utils/utils.dart';
|
||||
import 'initial_binding.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
debugPrint('应用初始化中...');
|
||||
await Global.init();
|
||||
debugPrint('应用初始化完成...');
|
||||
// 强制竖屏
|
||||
StatusBarKit.setPortrait().then((_) {
|
||||
runApp(const StatusbarzCapturer(
|
||||
child: MainApp(),
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
class MainApp extends StatelessWidget {
|
||||
const MainApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ScreenUtilInit(
|
||||
designSize: const Size(390, 844), // iPhone 13尺寸
|
||||
builder: ([BuildContext? _, __]) {
|
||||
return GetMaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
// 日志
|
||||
enableLog: true,
|
||||
logWriterCallback: Logger.write,
|
||||
defaultTransition: Transition.cupertino,
|
||||
// 路由
|
||||
getPages: AppPages.routes,
|
||||
navigatorObservers: [
|
||||
Global.routeObserver,
|
||||
Statusbarz.instance.observer
|
||||
],
|
||||
// 启动页面
|
||||
initialRoute: AppPages.initial,
|
||||
initialBinding: InitialBinding(),
|
||||
builder: (context, widget) {
|
||||
return MediaQuery(
|
||||
///设置文字大小不随系统设置改变
|
||||
data: MediaQuery.of(context)
|
||||
.copyWith(textScaler: const TextScaler.linear(1.0)),
|
||||
child: FlutterEasyLoading(child: widget),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
6
lib/pages/splash/index.dart
Normal file
6
lib/pages/splash/index.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
library splash;
|
||||
|
||||
export './splash_binding.dart';
|
||||
export './splash_logic.dart';
|
||||
export './splash_state.dart';
|
||||
export './splash_view.dart';
|
||||
10
lib/pages/splash/splash_binding.dart
Normal file
10
lib/pages/splash/splash_binding.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import 'splash_logic.dart';
|
||||
|
||||
class SplashBinding extends Bindings {
|
||||
@override
|
||||
void dependencies() {
|
||||
Get.lazyPut(() => SplashLogic());
|
||||
}
|
||||
}
|
||||
24
lib/pages/splash/splash_logic.dart
Normal file
24
lib/pages/splash/splash_logic.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:tdesign_flutter/tdesign_flutter.dart';
|
||||
|
||||
import 'splash_state.dart';
|
||||
|
||||
class SplashLogic extends GetxController {
|
||||
final SplashState state = SplashState();
|
||||
|
||||
@override
|
||||
void onReady() {
|
||||
// TODO: implement onReady
|
||||
super.onReady();
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
// TODO: implement onClose
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
void showText(context) {
|
||||
TDToast.showText('轻提示文字内容', context: context);
|
||||
}
|
||||
}
|
||||
6
lib/pages/splash/splash_state.dart
Normal file
6
lib/pages/splash/splash_state.dart
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
class SplashState {
|
||||
SplashState() {
|
||||
///Initialize variables
|
||||
}
|
||||
}
|
||||
43
lib/pages/splash/splash_view.dart
Normal file
43
lib/pages/splash/splash_view.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:tdesign_flutter/tdesign_flutter.dart';
|
||||
import 'splash_logic.dart';
|
||||
|
||||
class SplashPage extends StatefulWidget {
|
||||
const SplashPage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<SplashPage> createState() => _SplashPageState();
|
||||
}
|
||||
|
||||
class _SplashPageState extends State<SplashPage> {
|
||||
final logic = Get.find<SplashLogic>();
|
||||
final state = Get.find<SplashLogic>().state;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text("Splash"),
|
||||
TDButton(
|
||||
theme: TDButtonTheme.primary,
|
||||
text: "立即登录",
|
||||
onTap: () {
|
||||
logic.showText(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
Get.delete<SplashLogic>();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
17
lib/router/app_pages.dart
Normal file
17
lib/router/app_pages.dart
Normal file
@@ -0,0 +1,17 @@
|
||||
import 'package:get/get.dart';
|
||||
import '../pages/splash/index.dart';
|
||||
|
||||
class AppPages {
|
||||
static const String initial = '/';
|
||||
static const String login = '/login';
|
||||
static const String home = '/home';
|
||||
|
||||
static final List<GetPage> routes = [
|
||||
GetPage(
|
||||
name: AppPages.initial,
|
||||
page: () => const SplashPage(),
|
||||
binding: SplashBinding()),
|
||||
// GetPage(name: AppPages.login, page: () => LoginPage()),
|
||||
// GetPage(name: AppPages.home, page: () => HomePage()),
|
||||
];
|
||||
}
|
||||
8
lib/utils/logger.dart
Normal file
8
lib/utils/logger.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Logger {
|
||||
// Sample of abstract logging function
|
||||
static void write(String text, {bool isError = false}) {
|
||||
Future.microtask(() => debugPrint('** $text. isError: [$isError]'));
|
||||
}
|
||||
}
|
||||
29
lib/utils/status_bar.dart
Normal file
29
lib/utils/status_bar.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
// 状态栏设置工具类
|
||||
class StatusBarKit {
|
||||
// 设置沉浸式状态栏
|
||||
static void setStatusBarDark({bool dark = false, Color? darkColor}) {
|
||||
if (Platform.isAndroid) {
|
||||
SystemChrome.setSystemUIOverlayStyle(
|
||||
SystemUiOverlayStyle(
|
||||
statusBarColor: dark ? darkColor : Colors.transparent,
|
||||
systemNavigationBarIconBrightness:
|
||||
dark ? Brightness.light : Brightness.dark,
|
||||
statusBarIconBrightness: dark ? Brightness.light : Brightness.dark,
|
||||
statusBarBrightness: dark ? Brightness.light : Brightness.dark,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置竖屏
|
||||
static Future setPortrait() {
|
||||
return SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitDown,
|
||||
DeviceOrientation.portraitUp,
|
||||
]);
|
||||
}
|
||||
}
|
||||
69
lib/utils/storage.dart
Normal file
69
lib/utils/storage.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
// 本地存储
|
||||
class StorageUtil {
|
||||
static final StorageUtil _singleton = StorageUtil._internal();
|
||||
late SharedPreferences _prefs;
|
||||
|
||||
factory StorageUtil() => _singleton;
|
||||
|
||||
StorageUtil._internal();
|
||||
|
||||
Future init() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
Future<bool> putJSON(String key, dynamic jsonVal) {
|
||||
String jsonString = jsonEncode(jsonVal);
|
||||
return _prefs.setString(key, jsonString);
|
||||
}
|
||||
|
||||
dynamic getJSON(String key) {
|
||||
String? jsonString = _prefs.getString(key);
|
||||
return jsonString == null ? null : jsonDecode(jsonString);
|
||||
}
|
||||
|
||||
String getString(String key, {String defValue = ''}) {
|
||||
if (!_prefs.containsKey(key)) return defValue;
|
||||
return _prefs.getString(key) ?? defValue;
|
||||
}
|
||||
|
||||
Future<bool>? putString(String key, String value) {
|
||||
return _prefs.setString(key, value);
|
||||
}
|
||||
|
||||
bool getBool(String key, {bool defValue = false}) {
|
||||
if (!_prefs.containsKey(key)) return defValue;
|
||||
return _prefs.getBool(key) ?? defValue;
|
||||
}
|
||||
|
||||
Future<bool>? putBool(String key, bool value) {
|
||||
return _prefs.setBool(key, value);
|
||||
}
|
||||
|
||||
int getInt(String key, {int defValue = 0}) {
|
||||
if (!_prefs.containsKey(key)) return defValue;
|
||||
return _prefs.getInt(key) ?? defValue;
|
||||
}
|
||||
|
||||
Future<bool>? putInt(String key, int value) {
|
||||
return _prefs.setInt(key, value);
|
||||
}
|
||||
|
||||
bool? haveKey(String key) {
|
||||
return getKeys().contains(key);
|
||||
}
|
||||
|
||||
Set<String> getKeys() {
|
||||
return _prefs.getKeys();
|
||||
}
|
||||
|
||||
Future<bool>? remove(String key) {
|
||||
return _prefs.remove(key);
|
||||
}
|
||||
|
||||
Future<bool>? clear() {
|
||||
return _prefs.clear();
|
||||
}
|
||||
}
|
||||
5
lib/utils/utils.dart
Normal file
5
lib/utils/utils.dart
Normal file
@@ -0,0 +1,5 @@
|
||||
library utils;
|
||||
|
||||
export 'logger.dart';
|
||||
export 'status_bar.dart';
|
||||
export 'storage.dart';
|
||||
Reference in New Issue
Block a user