feat(migrate): 将之前写好的部件迁移了过来
This commit is contained in:
103
lib/common/access_control_filter.dart
Normal file
103
lib/common/access_control_filter.dart
Normal file
@@ -0,0 +1,103 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'global.dart';
|
||||
import 'package:bistro/mine/login.dart';
|
||||
|
||||
typedef ACFCallback = Future Function(Map userInfo);
|
||||
|
||||
///
|
||||
/// 登录状态检查
|
||||
///
|
||||
class AccessControlFilter extends StatefulWidget {
|
||||
/// 头部
|
||||
final AppBar? appBar;
|
||||
|
||||
/// 标题
|
||||
final String? title;
|
||||
|
||||
/// 内容组件
|
||||
final Widget child;
|
||||
|
||||
/// 未登录的显示
|
||||
final Widget? didNotLoginWidget;
|
||||
|
||||
const AccessControlFilter({
|
||||
required Key key,
|
||||
this.appBar,
|
||||
this.title,
|
||||
required this.child,
|
||||
this.didNotLoginWidget,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
_AccessControlFilterState createState() => _AccessControlFilterState();
|
||||
}
|
||||
|
||||
class _AccessControlFilterState extends State<AccessControlFilter> {
|
||||
/// 登录状态
|
||||
bool _isLogin = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_updateUserState();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(AccessControlFilter oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_updateUserState();
|
||||
}
|
||||
|
||||
/// 更新用户信息
|
||||
void _updateUserState({data}) {
|
||||
// final Map userInfo = data != null ? data : UserInfo().userInfo;
|
||||
// final bool isLogin = userInfo != null && userInfo.length != 0;
|
||||
// setState(() {
|
||||
// _isLogin = isLogin;
|
||||
// });
|
||||
}
|
||||
|
||||
/// 未登录的显示
|
||||
Widget? _didNotLoginWidget() {
|
||||
// 如果传入的未登录情况下显示的组件,则采用传入的
|
||||
if (widget.didNotLoginWidget != null) return widget.didNotLoginWidget;
|
||||
|
||||
// 缺省的未登录情况下的显示
|
||||
return Center(
|
||||
child: RaisedButton(
|
||||
color: Colors.blueAccent,
|
||||
textTheme: ButtonTextTheme.primary,
|
||||
child: const Text('请先登录'),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (BuildContext context) {
|
||||
return Login(
|
||||
successCallback: (Map data) {
|
||||
_updateUserState(data: data);
|
||||
printWhenDebug('登录成功');
|
||||
},
|
||||
key: widget.key ?? const Key('acf'),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: widget.appBar ??
|
||||
AppBar(
|
||||
centerTitle: true,
|
||||
title: Text(widget.title ?? ""),
|
||||
),
|
||||
body: _isLogin ? widget.child : _didNotLoginWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
112
lib/common/global.dart
Normal file
112
lib/common/global.dart
Normal file
@@ -0,0 +1,112 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio_http_cache/dio_http_cache.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
// 提供五套可选主题色
|
||||
const _themes = <MaterialColor>[
|
||||
Colors.blue,
|
||||
Colors.cyan,
|
||||
Colors.teal,
|
||||
Colors.green,
|
||||
Colors.red,
|
||||
];
|
||||
|
||||
///
|
||||
/// 当调试模式打开时打印
|
||||
///
|
||||
void printWhenDebug(Object object) {
|
||||
if (Global.debug) {
|
||||
if (kDebugMode) {
|
||||
print(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// 当前环境
|
||||
///
|
||||
enum Env {
|
||||
dev,
|
||||
pro,
|
||||
}
|
||||
|
||||
class Global {
|
||||
static SharedPreferences? _prefs;
|
||||
static Dio? dio;
|
||||
static Map profile = {};
|
||||
static bool debug = false;
|
||||
///
|
||||
/// 当前环境
|
||||
///
|
||||
static const Env env = Env.dev;
|
||||
|
||||
|
||||
///
|
||||
/// 根域名
|
||||
///
|
||||
static const Map baseUrlList = {
|
||||
Env.dev: 'http://safe.doylee.cn',
|
||||
// Env.dev: 'http://192.168.31.189/campus_safety',
|
||||
Env.pro: 'http://10.111.2.158',
|
||||
};
|
||||
|
||||
///
|
||||
/// 接口根路由
|
||||
///
|
||||
static String baseUrl = baseUrlList[env];
|
||||
|
||||
// 网络缓存对象
|
||||
static DioCacheManager netCache = DioCacheManager(
|
||||
CacheConfig(
|
||||
baseUrl: baseUrl,
|
||||
),
|
||||
);
|
||||
|
||||
// 可选的主题列表
|
||||
static List<MaterialColor> get themes => _themes;
|
||||
|
||||
// 是否为release版
|
||||
static bool get isRelease => bool.fromEnvironment("dart.vm.product");
|
||||
|
||||
//初始化全局信息,会在APP启动时执行
|
||||
static Future init() async {
|
||||
dio = Dio();
|
||||
// 添加拦截器
|
||||
dio?.interceptors.add(netCache.interceptor);
|
||||
|
||||
// 初始化用户信息
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
var _profile = _prefs?.getString("user_info");
|
||||
if (_profile != null) {
|
||||
try {
|
||||
if (kDebugMode) {
|
||||
print('开始读取用户信息...');
|
||||
}
|
||||
profile = jsonDecode(_profile);
|
||||
if (kDebugMode) {
|
||||
print('用户信息读取成功');
|
||||
}
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 持久化Profile信息
|
||||
static Future<bool>? saveProfile({required Map userInfo}) {
|
||||
profile = userInfo;
|
||||
final userJson = jsonEncode(userInfo);
|
||||
return _prefs?.setString("user_info", userJson);
|
||||
}
|
||||
|
||||
/// 读取持久化Profile信息
|
||||
static Future<Map> getProfile() {
|
||||
return jsonDecode(_prefs?.getString("user_info") ?? '{}');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user