使用 easy_localization 和 go_router 更改语言环境时防止 Flutter 应用程序重启

Prevent Flutter app restart when changing locale using easy_localization with go_router

提问人:KT AT CBX 提问时间:11/14/2023 最后编辑:KT AT CBX 更新时间:11/14/2023 访问量:24

问:

我通过以下方式更改应用程序区域设置

      onChanged: (String? value) async {
        if (value != null) {
          await context.setLocale(Locale(value));
        }
      },

但它会重新启动整个应用程序,这不是我想要的。

更改区域设置后如何保持相同的屏幕?

谢谢

附言。

我正在使用 go 路由器,如下所示:

MaterialApp.router(
          routerConfig: RouteGenerator.goRouterConfig(
          ),
          localizationsDelegates: context.localizationDelegates,
          supportedLocales: context.supportedLocales,
          locale: context.locale,
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true,
          ),
        ), 

设置区域设置时,似乎使用会重新启动整个应用程序。MaterialApp.router

我尝试过传统的,它效果很好。 但是,当我更改为使用 go router 时,它将重新启动应用程序。Navigator.push

我做了一个最小的可重现代码供您参考:

go router 版本,这将重新启动整个应用程序:


            MaterialApp.router(
          routerConfig: GoRouter(
            routes: [
              GoRoute(
                path: '/lang',
                builder: (context, state) => Scaffold(
                  body: Container(
                    margin: EdgeInsets.all(100),
                    child: SizedBox(
                        height: 500,
                        child: Column(children: [
                          DropdownButton(
                            value: context.locale.toString(),
                            items: const [
                              DropdownMenuItem(
                                value: "en",
                                child: Text("English"),
                              ),
                              DropdownMenuItem(
                                value: "zh",
                                child: Text("繁體中文"),
                              ),
                            ],
                            onChanged: (String? value) async {
                              if (value != null) {
                                await context.setLocale(Locale(value));
                              }
                            },
                          ),
                          InkWell(
                              onTap: () =>
                                  {},
                              child: Text("edit".tr()))
                        ])),
                  ),
                ),
              ),
              GoRoute(
                path: '/',
                builder: (context, state) => Scaffold(
                  body: Container(
                    margin: EdgeInsets.all(100),
                    child: SizedBox(
                        height: 500,
                        child: Column(children: [
                          InkWell(
                              onTap: () => context.go('/lang'),
                              child: Text("edit".tr()))
                        ])),
                  ),
                ),
              ),
            ],
          ),
          localizationsDelegates: context.localizationDelegates,
          supportedLocales: context.supportedLocales,
          locale: context.locale,
        ),

如果没有 Go 路由器版本,它不会重新启动整个应用程序:

MaterialApp(
          localizationsDelegates: context.localizationDelegates,
          supportedLocales: context.supportedLocales,
          locale: context.locale,
          home: Builder(builder: (context) {
            return Scaffold(
              body: Container(
                margin: EdgeInsets.all(100),
                child: SizedBox(
                    height: 500,
                    child: Column(children: [
                      InkWell(
                          onTap: () => Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => Scaffold(
                                  body: Container(
                                    margin: EdgeInsets.all(100),
                                    child: SizedBox(
                                        height: 500,
                                        child: Column(children: [
                                          DropdownButton(
                                            value: context.locale.toString(),
                                            items: const [
                                              DropdownMenuItem(
                                                value: "en",
                                                child: Text("English"),
                                              ),
                                              DropdownMenuItem(
                                                value: "zh",
                                                child: Text("繁體中文"),
                                              ),
                                            ],
                                            onChanged: (String? value) async {
                                              if (value != null) {
                                                await context
                                                    .setLocale(Locale(value));
                                              }
                                            },
                                          ),
                                          InkWell(
                                              onTap: () => {},
                                              child: Text("edit".tr()))
                                        ])),
                                  ),
                                ),
                              )),
                          child: Text("edit".tr()))
                    ])),
              ),
            );
          }),
        ),
Flutter Dart 本地化

评论


答: 暂无答案