增加多语言支持
This commit is contained in:
parent
a7d4f1d256
commit
e650433e4c
|
@ -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<Locale> languages = [
|
||||
Locale('en', 'US'),
|
||||
|
|
|
@ -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<void> 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(),
|
||||
);
|
||||
|
||||
|
|
|
@ -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);
|
||||
// 分类对应的数据(推荐、新闻)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<String> history = [];
|
||||
|
||||
static final pages = [
|
||||
// 免登陆
|
||||
GetPage(
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
import 'package:get/get.dart';
|
||||
|
||||
import 'en_US.dart';
|
||||
import 'zh_CN.dart';
|
||||
|
||||
class AppTranslations extends Translations {
|
||||
@override
|
||||
Map<String, Map<String, String>> get keys => {
|
||||
'en_US': enUS,
|
||||
'zh_CN': zhCN,
|
||||
};
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
const Map<String, String> enUS = {
|
||||
'title': 'This is Title!',
|
||||
'login': 'logged in as @name with email @email',
|
||||
'welcome': 'welcome',
|
||||
};
|
|
@ -0,0 +1,5 @@
|
|||
const Map<String, String> zhCN = {
|
||||
'title': '这是标题',
|
||||
'login': '登录用户 @name,邮箱账号 @email',
|
||||
'Welcome': '欢迎',
|
||||
};
|
Loading…
Reference in New Issue