Flutter 当我更改语言时重新加载颤振应用程序(易于本地化)

Flutter 当我更改语言时重新加载颤振应用程序(易于本地化),flutter,dart,Flutter,Dart,我现在还不熟悉颤振和飞镖,我正在使用颤振2.03,并尝试使用easy_本地化包(3.0.0)构建一个多语言应用程序。起初,当我尝试从设置页面或第一次显示的页面更改应用程序语言时,一切正常,应用程序翻译内容并保持在同一页面上,但昨天当我更改应用程序区域设置时,应用程序开始重新加载: onChanged: (newValue) async { if (newValue == 'English') { await context.setLocale(Locale('en')); } el

我现在还不熟悉颤振和飞镖,我正在使用颤振2.03,并尝试使用easy_本地化包(3.0.0)构建一个多语言应用程序。起初,当我尝试从设置页面或第一次显示的页面更改应用程序语言时,一切正常,应用程序翻译内容并保持在同一页面上,但昨天当我更改应用程序区域设置时,应用程序开始重新加载:

onChanged: (newValue) async {
 if (newValue == 'English') {
     await context.setLocale(Locale('en'));
 } else if (newValue == 'Français') {
     await context.setLocale(Locale('fr'));
 } else if (newValue == 'العربية') {
     await context.setLocale(Locale('ar'));
 }
},
我只想让应用程序进行热加载和翻译页面,并保持在同一屏幕上,而无需重新加载整个应用程序并返回主屏幕

Main.dart

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:sg_user_dash/screens/homescreen.dart';
import 'package:sg_user_dash/screens/language.dart';
import 'package:shared_preferences/shared_preferences.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();
  runApp(EasyLocalization(
      supportedLocales: [Locale('en'), Locale('fr'), Locale('ar')],
      path: 'assets/translations',
      fallbackLocale: Locale('en'),
      child: MyApp()));
}

Future<String> nextdisplay() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool _seen = (prefs.getBool('seen') ?? false);

  if (_seen) {
    return "Homepage";
  } else {
    await prefs.setBool('seen', true);
    return "walkthrough";
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Smart Government',
        theme: ThemeData(),
        localizationsDelegates: context.localizationDelegates,
        supportedLocales: context.supportedLocales,
        locale: context.locale,
        home: FutureBuilder(
            future: nextdisplay(),
            builder: (context, AsyncSnapshot<String> snapshot) {
              if (snapshot.hasData) {
                if (snapshot.data == "walkthrough") {
                  return Language();
                } else if (snapshot.data == "Homepage") {
                  return HomeScreen();
                } else {
                  return Language();
                }
              }
              return Center(
                child: CupertinoActivityIndicator(),
              );
            }));
  }
}

import'包:easy_本地化/easy_本地化.dart';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
导入“package:flatter/rendering.dart”;
导入“包:sg_用户_dash/screens/homescreen.dart”;
导入“包:sg_user_dash/screens/language.dart”;
导入“package:shared_preferences/shared_preferences.dart”;
Future main()异步{
WidgetsFlutterBinding.ensureInitialized();
等待EasyLocalization.ensureInitialized();
runApp(易本地化)(
supportedLocales:[Locale('en')、Locale('fr')、Locale('ar')],
路径:“资产/翻译”,
fallbackLocale:Locale('en'),
child:MyApp());
}
Future nextdisplay()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
bool_seen=(prefs.getBool('seen')??false);
如果(看到){
返回“主页”;
}否则{
等待参数setBool('seed',true);
返回“演练”;
}
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
标题:"智慧政府",,
主题:主题数据(),
localizationsDelegates:context.localizationDelegates,
supportedLocales:context.supportedLocales,
locale:context.locale,
家:未来建设者(
future:nextdisplay(),
生成器:(上下文,异步快照){
if(snapshot.hasData){
如果(snapshot.data==“演练”){
返回语言();
}else if(snapshot.data==“主页”){
返回主屏幕();
}否则{
返回语言();
}
}
返回中心(
子项:CupertinoActivityIndicator(),
);
}));
}
}

谢谢easy\u本地化当语言在运行时发生更改时,您无需再次保存该语言。 我有一个关于如何使用easy_本地化的视频,我想它会有所帮助


我没有提到昨天我尝试使用provider和notifierlistener实现暗模式,结果不好,所以我删除了它。这会影响轻松的本地化吗?伙计,它工作得很好,我做了和你在视频中做的一样的工作,就在几天内,当我更改语言id时,它开始重新加载,不知道为什么