diff --git a/lib/data/services/config.dart b/lib/data/services/config.dart index 2d057e0..921a9a8 100644 --- a/lib/data/services/config.dart +++ b/lib/data/services/config.dart @@ -15,7 +15,8 @@ class ConfigService extends GetxService { bool get isRelease => bool.fromEnvironment("dart.vm.product"); - Locale locale = Locale("en", "US"); + // Locale locale = Locale("en", "US"); + Locale locale = Locale("zh", "CN"); List languages = [ Locale('en', 'US'), diff --git a/lib/main.dart b/lib/main.dart index 405640e..9be90ac 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -6,7 +7,9 @@ import 'package:news_getx/data/services/config.dart'; import 'package:news_getx/global.dart'; import 'package:news_getx/routes/app_pages.dart'; import 'package:news_getx/theme/app_theme.dart'; +import 'package:news_getx/translations/app_translations.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; Future main() async { await Global.init(); @@ -24,12 +27,24 @@ class MyApp extends StatelessWidget { // 如果不把这歌参数设为true 那么column就不会随着软键盘而变化 // useInheritedMediaQuery: true, builder: (BuildContext context, Widget? child) { + var app = GetMaterialApp( title: 'News', debugShowCheckedModeBanner: false, theme: AppTheme.light, - initialRoute: AppRoutes.Initial, + initialRoute: AppPages.Initial, getPages: AppPages.pages, + navigatorObservers: [AppPages.observer], + translations: AppTranslations(), + // 设置本地化 + localizationsDelegates: [ + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: ConfigService.to.languages, + locale: ConfigService.to.locale, + fallbackLocale: Locale('en', 'US'), builder: EasyLoading.init(), ); diff --git a/lib/modules/main/main_controller.dart b/lib/modules/main/main_controller.dart index f17ae96..c319493 100644 --- a/lib/modules/main/main_controller.dart +++ b/lib/modules/main/main_controller.dart @@ -19,7 +19,6 @@ class MainController extends GetxController { /// 方法 // 拉取数据 asyncLoadAllData() async { - print('开始拉取数据'); state.categories = await newsRepository.categories(cacheDisk: true); state.channels = await newsRepository.channels(cacheDisk: true); // 分类对应的数据(推荐、新闻) diff --git a/lib/routes/app_observer.dart b/lib/routes/app_observer.dart new file mode 100644 index 0000000..293ae0f --- /dev/null +++ b/lib/routes/app_observer.dart @@ -0,0 +1,51 @@ +import 'package:flutter/material.dart'; +import 'package:news_getx/routes/app_pages.dart'; + + +/// 监听路由导航 +class AppNavigatorObserver extends NavigatorObserver { + @override + void didPush(Route route, Route? previousRoute) { + super.didPush(route, previousRoute); + + var name = route.settings.name ?? ""; + if (name.isNotEmpty) { + AppPages.history.add(name); + } + } + + + @override + void didPop(Route route, Route? previousRoute) { + super.didPop(route, previousRoute); + + AppPages.history.remove(route.settings.name); + } + + + @override + void didRemove(Route route, Route? previousRoute) { + super.didRemove(route, previousRoute); + AppPages.history.remove(route.settings.name); + } + + @override + void didReplace({Route? newRoute, Route? oldRoute}) { + super.didReplace(newRoute: newRoute, oldRoute: oldRoute); + + if (newRoute != null) { + var index = AppPages.history.indexWhere((element) { + return element == oldRoute?.settings.name; + }); + + var name = newRoute.settings.name ?? ''; + if (name.isNotEmpty) { + if (index > 0) { + AppPages.history[index] = name; + } else { + AppPages.history.add(name); + } + } + } + } +} \ No newline at end of file diff --git a/lib/routes/app_pages.dart b/lib/routes/app_pages.dart index e99f0bd..5c6dce9 100644 --- a/lib/routes/app_pages.dart +++ b/lib/routes/app_pages.dart @@ -1,3 +1,4 @@ +import 'package:flutter/cupertino.dart'; import 'package:get/get.dart'; import 'package:news_getx/middlewares/router_auth.dart'; import 'package:news_getx/middlewares/router_welcome.dart'; @@ -14,10 +15,16 @@ import 'package:news_getx/modules/sign_up/sign_up_binding.dart'; import 'package:news_getx/modules/sign_up/sign_up_page.dart'; import 'package:news_getx/modules/welcome/welcome_binding.dart'; import 'package:news_getx/modules/welcome/welcome_page.dart'; +import 'package:news_getx/routes/app_observer.dart'; part './app_routes.dart'; abstract class AppPages { + static const Initial = AppRoutes.Initial; + + static final NavigatorObserver observer = AppNavigatorObserver(); + static List history = []; + static final pages = [ // 免登陆 GetPage( diff --git a/lib/translations/app_translations.dart b/lib/translations/app_translations.dart new file mode 100644 index 0000000..bd19d69 --- /dev/null +++ b/lib/translations/app_translations.dart @@ -0,0 +1,12 @@ +import 'package:get/get.dart'; + +import 'en_US.dart'; +import 'zh_CN.dart'; + +class AppTranslations extends Translations { + @override + Map> get keys => { + 'en_US': enUS, + 'zh_CN': zhCN, + }; +} diff --git a/lib/translations/en_US.dart b/lib/translations/en_US.dart new file mode 100644 index 0000000..48f0770 --- /dev/null +++ b/lib/translations/en_US.dart @@ -0,0 +1,5 @@ +const Map enUS = { + 'title': 'This is Title!', + 'login': 'logged in as @name with email @email', + 'welcome': 'welcome', +}; \ No newline at end of file diff --git a/lib/translations/zh_CN.dart b/lib/translations/zh_CN.dart new file mode 100644 index 0000000..9581ee1 --- /dev/null +++ b/lib/translations/zh_CN.dart @@ -0,0 +1,5 @@ +const Map zhCN = { + 'title': '这是标题', + 'login': '登录用户 @name,邮箱账号 @email', + 'Welcome': '欢迎', +};