diff --git a/lib/main.dart b/lib/main.dart index 49cf064..3fe3b0e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:chat/configs/themes.dart'; import 'package:chat/routes/app_router.dart'; +import 'package:chat/routes/app_routes.dart'; import 'package:chat/services/auth_service.dart'; import 'package:chat/services/tabbar_service.dart'; import 'package:chat/services/tim_service.dart'; @@ -26,8 +27,7 @@ class MyApp extends StatelessWidget { debugShowCheckedModeBanner: false, theme: Themes.light, darkTheme: Themes.dark, - initialRoute: AppRouter.transitRoute, - unknownRoute: AppRouter.unknownRoute, + initialRoute: AppRoutes.transit, defaultTransition: Transition.cupertino, getPages: AppRouter.getPages, builder: EasyLoading.init(), diff --git a/lib/routes/app_router.dart b/lib/routes/app_router.dart index 6eb1823..a5c3440 100644 --- a/lib/routes/app_router.dart +++ b/lib/routes/app_router.dart @@ -1,45 +1,11 @@ import 'package:chat/routes/app_routes.dart'; import 'package:chat/routes/auth_routes.dart'; -import 'package:chat/views/home/index_page.dart'; -import 'package:chat/views/public/app_page.dart'; -import 'package:chat/views/public/scan_page.dart'; -import 'package:chat/views/public/transit_page.dart'; -import 'package:chat/views/public/unknown_page.dart'; import 'package:get/get.dart'; class AppRouter { - // 引导页 - static const String transitRoute = AppRoutes.transit; - // 未知页面,返回主页 - static final GetPage unknownRoute = GetPage( - name: AppRoutes.notfound, - page: () => const UnknownPage(), - ); - // 路由页面 static final List> getPages = [ - /// 过渡页面 - GetPage( - name: AppRoutes.transit, - page: () => const TransitPage(), - ), - - GetPage( - name: AppRoutes.app, - page: () => AppPage(), - ), - - /// 首页 - GetPage( - name: AppRoutes.home, - page: () => const HomePage(), - ), - - GetPage( - name: AppRoutes.scan, - page: () => const ScanPage(), - ), - + AppRoutes.router, AuthRoutes.router, ]; } diff --git a/lib/routes/app_routes.dart b/lib/routes/app_routes.dart index a6f677e..d6a5d6c 100644 --- a/lib/routes/app_routes.dart +++ b/lib/routes/app_routes.dart @@ -1,17 +1,34 @@ +import 'package:chat/views/home/index_page.dart'; +import 'package:chat/views/public/app_page.dart'; +import 'package:chat/views/public/scan_page.dart'; +import 'package:chat/views/public/transit_page.dart'; +import 'package:get/get.dart'; + /// 这里是为了定义别名路由的名称, /// 具体映射关系,在app_router 的 getPages 中实现 abstract class AppRoutes { - /// 过渡页 - static const String transit = '/transit'; - - /// 找不到页面的时候 - static const String notfound = '/notfound'; - - /// 根页面 static const String app = '/'; - - /// 首页 + static const String transit = '/transit'; + static const String notfound = '/notfound'; static const String home = '/home'; - static const String scan = '/scan'; + + static GetPage router = GetPage( + name: '/', + page: () => AppPage(), + children: [ + GetPage( + name: AppRoutes.transit, + page: () => const TransitPage(), + ), + GetPage( + name: AppRoutes.home, + page: () => const HomePage(), + ), + GetPage( + name: AppRoutes.scan, + page: () => const ScanPage(), + ), + ], + ); } diff --git a/lib/routes/contact_routes.dart b/lib/routes/contact_routes.dart new file mode 100644 index 0000000..563bd0d --- /dev/null +++ b/lib/routes/contact_routes.dart @@ -0,0 +1,24 @@ +import 'package:chat/middleware/auth_middleware.dart'; +import 'package:chat/views/contact/group/index_page.dart'; +import 'package:chat/views/contact/index/index_page.dart'; +import 'package:get/get.dart'; + +abstract class ContactRoutes { + /// 身份验证页面 + static const String index = '/contact'; + static const String group = '/contact/group'; + + static GetPage router = GetPage( + name: ContactRoutes.index, + middlewares: [ + EnsureAuthMiddleware(), + ], + page: () => const ContactPage(), + children: [ + GetPage( + name: '/group', + page: () => const ContactGroupPage(), + ), + ], + ); +} diff --git a/lib/services/auth_service.dart b/lib/services/auth_service.dart index 04c283e..c2c501b 100644 --- a/lib/services/auth_service.dart +++ b/lib/services/auth_service.dart @@ -1,11 +1,46 @@ import 'package:get/get.dart'; +import 'package:get_storage/get_storage.dart'; class AuthService extends GetxService { static AuthService get to => Get.find(); + final _box = GetStorage(); + /// 供外部使用的,判断是否登录的状态 get isUserLogin => isLogin.value; /// 登录状态记录,可监听的,这样ever才能监听到 final RxBool isLogin = false.obs; + + /// 登录的token,供请求时调用,载入内存,是为了每次使用的时候,不需要从磁盘获取 + late String userToken = ''; + + /// 获取存储的token,这个可以做到持久化存储 + String get _userToken => _box.read('userToken') ?? ''; + + @override + void onInit() { + super.onInit(); + + if (_userToken.isNotEmpty) { + isLogin.value = true; + userToken = _userToken; + } + + // ever(_isLogin, (_) { + // if (_ == true) { + // Get.offAllNamed(AppRoutes.app); + // } else { + // Get.offAllNamed(AuthRoutes.index); + // } + // }); + } + + Future login(String address) async { + _box.write('userToken', address); + userToken = address; + isLogin.value = true; + + return true; + } } diff --git a/lib/views/auth/import/index_page.dart b/lib/views/auth/import/index_page.dart index e209113..e10d7a3 100644 --- a/lib/views/auth/import/index_page.dart +++ b/lib/views/auth/import/index_page.dart @@ -1,6 +1,9 @@ import 'package:chat/configs/app_colors.dart'; +import 'package:chat/routes/app_routes.dart'; +import 'package:chat/services/auth_service.dart'; import 'package:chat/utils/hd_wallet.dart'; import 'package:flutter/material.dart'; +import 'package:get/get.dart'; class AuthImportPage extends StatefulWidget { const AuthImportPage({Key? key}) : super(key: key); @@ -47,7 +50,12 @@ class _AuthImportPageState extends State { const Text('支持导入所有遵循BIP标准生成的助记词'), ElevatedButton( onPressed: () { - HDWallet.mnemonicToAddress(_editingController.text); + String? address = + HDWallet.mnemonicToAddress(_editingController.text); + if (address != null) { + AuthService.to.login(address); + Get.offAllNamed(AppRoutes.app); + } }, child: const Text('开始导入'), ), diff --git a/lib/views/contact/group/index_page.dart b/lib/views/contact/group/index_page.dart new file mode 100644 index 0000000..be0e925 --- /dev/null +++ b/lib/views/contact/group/index_page.dart @@ -0,0 +1,15 @@ +import 'package:flutter/material.dart'; + +class ContactGroupPage extends StatefulWidget { + const ContactGroupPage({Key? key}) : super(key: key); + + @override + _ContactGroupPageState createState() => _ContactGroupPageState(); +} + +class _ContactGroupPageState extends State { + @override + Widget build(BuildContext context) { + return Container(); + } +} diff --git a/lib/views/contact/index/index_page.dart b/lib/views/contact/index/index_page.dart new file mode 100644 index 0000000..4eb3d21 --- /dev/null +++ b/lib/views/contact/index/index_page.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class ContactPage extends StatefulWidget { + const ContactPage({Key? key}) : super(key: key); + + @override + _ContactPageState createState() => _ContactPageState(); +} + +class _ContactPageState extends State { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('通讯录'), + ), + ); + } +} diff --git a/lib/views/home/index_page.dart b/lib/views/home/index_page.dart index d2ec073..4c1edc2 100644 --- a/lib/views/home/index_page.dart +++ b/lib/views/home/index_page.dart @@ -10,6 +10,10 @@ class HomePage extends StatefulWidget { class _HomePageState extends State { @override Widget build(BuildContext context) { - return Container(); + return Scaffold( + appBar: AppBar( + title: const Text('消息'), + ), + ); } } diff --git a/lib/views/moments/index/index_page.dart b/lib/views/moments/index/index_page.dart index 511aca3..d3ebfce 100644 --- a/lib/views/moments/index/index_page.dart +++ b/lib/views/moments/index/index_page.dart @@ -10,6 +10,10 @@ class MomentsPage extends StatefulWidget { class _MomentsPageState extends State { @override Widget build(BuildContext context) { - return Container(); + return Scaffold( + appBar: AppBar( + title: const Text('发现'), + ), + ); } } diff --git a/lib/views/public/app_page.dart b/lib/views/public/app_page.dart index 975b50e..5351231 100644 --- a/lib/views/public/app_page.dart +++ b/lib/views/public/app_page.dart @@ -1,5 +1,6 @@ import 'package:chat/configs/app_colors.dart'; import 'package:chat/services/tabbar_service.dart'; +import 'package:chat/views/contact/index/index_page.dart'; import 'package:chat/views/home/index_page.dart'; import 'package:chat/views/moments/index/index_page.dart'; import 'package:chat/views/user/index/user_page.dart'; @@ -12,6 +13,7 @@ class AppPage extends StatelessWidget { final List _tabBarPageList = [ IndexedStackChild(child: const HomePage()), + IndexedStackChild(child: const ContactPage()), IndexedStackChild(child: const MomentsPage()), IndexedStackChild(child: const UserPage()), ]; @@ -19,15 +21,19 @@ class AppPage extends StatelessWidget { final List _tabBarList = [ { 'icon': 'tabBar_03.png', - 'label': '首页', + 'label': '消息', }, { 'icon': 'tabBar_03.png', - 'label': '首页', + 'label': '通讯录', }, { 'icon': 'tabBar_03.png', - 'label': '首页', + 'label': '发现', + }, + { + 'icon': 'tabBar_03.png', + 'label': '我的', }, ]; diff --git a/lib/views/public/unknown_page.dart b/lib/views/public/unknown_page.dart deleted file mode 100644 index 1b22050..0000000 --- a/lib/views/public/unknown_page.dart +++ /dev/null @@ -1,20 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:get/get.dart'; - -class UnknownPage extends StatelessWidget { - const UnknownPage({Key? key}) : super(key: key); - - @override - Widget build(BuildContext context) { - return Scaffold( - body: Center( - child: ElevatedButton( - onPressed: () { - Get.back(); - }, - child: const Text('未知的页面'), - ), - ), - ); - } -} diff --git a/lib/views/user/index/user_page.dart b/lib/views/user/index/user_page.dart index b734820..95a0828 100644 --- a/lib/views/user/index/user_page.dart +++ b/lib/views/user/index/user_page.dart @@ -10,6 +10,10 @@ class UserPage extends StatefulWidget { class _UserPageState extends State { @override Widget build(BuildContext context) { - return Container(); + return Scaffold( + appBar: AppBar( + title: const Text('我的'), + ), + ); } }