Flutter - 使用GetIt和BuildContext

5
我正在使用基于Flutter文档的本地化实现。请参考以下链接:https://flutter.dev/docs/development/accessibility-and-localization/internationalization 我使用了get_it包(版本4.0.4),用来检索单例对象,例如Localization delegate。不幸的是,它需要一个BuildContext属性。有时在我的应用程序中,我没有上下文引用,所以如果可以像这样工作就好了:GetIt.I<AppLocalizations>() 而不是这样:AppLocalizations.of(context)。仍然可以通过以下方式实现: GetIt.I.registerLazySingleton(() => AppLocalizations.of(context)); 但问题是你至少需要一次上下文才能使其有效。此外,如果你想立即在初始路由中显示本地化文本,则更难在需要时获取已正确初始化的BuildContext
对我来说有点难以正确解释它,因此我在最小示例中重新创建了该问题。
我注释掉了一些会导致编译时错误的代码,但它展示了我如何想象它完成的方式。 main.dart
GetIt getIt = GetIt.instance;

void setupGetIt() {
  // How to get BuildContext properly if no context is available yet?
  // Compile time error.
  // getIt.registerLazySingleton(() => AppLocalizations.of(context));
}

void main() {
  setupGetIt();

  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    // The above line also won't work. It has BuildContext but Applocalizations.of(context) won't work
    // because it's above in the Widget tree and not yet setted up.
    getIt.registerLazySingleton(() => AppLocalizations.of(context));
    return MaterialApp(
      supportedLocales: const [
        Locale('en', 'US'),
        Locale('hu', 'HU'),
      ],
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      localeResolutionCallback: (locale, supportedLocales) {
        // check if locale is supported
        for (final supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale?.languageCode &&
              supportedLocale.countryCode == locale?.countryCode) {
            return supportedLocale;
          }
        }
        // if locale is not supported then return the first (default) one
        return supportedLocales.first;
      },
      // You may pass the BuildContext here for Page1 in it's constructor 
      // but in a more advanced routing case it's not a maintanable solution.
      home: Page1(),
    );
  }
}

初始路线。
class PageBase extends StatelessWidget {
  final String title;
  final Widget content;

  PageBase(this.title, this.content);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: content,
    );
  }
}

class Page1 extends PageBase {
  // It won't run because I need the context but clearly I don't have it.
  // And in a real app you also don't want to pass the context all over the place 
     if you have many routes to manage.
  Page1(String title)
      : super(AppLocalizations.of(context).title, Center(child: Text('Hello')));

  // Intended solution
  // I don't know how to properly initialize getIt AppLocalizations singleton by the time
  // it tries to retrieve it
  Page1.withGetIt(String title)
      : super(getIt<AppLocalizations>().title, Center(child: Text('Hello')));
}

locales.dart

String globalLocaleName;

class AppLocalizations {
  //AppLocalizations(this.localeName);

  static AppLocalizations of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations);
  }

  static const LocalizationsDelegate<AppLocalizations> delegate =
      _AppLocalizationsDelegate();

  static Future<AppLocalizations> load(Locale locale) async {
    final String name =
        locale.countryCode.isEmpty ? locale.languageCode : locale.toString();

    final String localeName = Intl.canonicalizedLocale(name);

    return initializeMessages(localeName).then((_) {
      globalLocaleName = localeName;
      return AppLocalizations();
    });
  }

  String get title => Intl.message(
        'This is the title.',
        name: 'title',
      );
}

class _AppLocalizationsDelegate
    extends LocalizationsDelegate<AppLocalizations> {
  // This delegate instance will never change (it doesn't even have fields!)
  // It can provide a constant constructor.
  const _AppLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'hu'].contains(locale.languageCode);
  }

  @override
  Future<AppLocalizations> load(Locale locale) => AppLocalizations.load(locale);

  @override
  bool shouldReload(_AppLocalizationsDelegate old) => false;
}

一些国际化生成的Dart代码和.ARb文件并不重要,无法说明问题。
总之,我该如何实现将我的AppLocalizations类作为单例使用,而不需要像在这样的情况下使用上下文?也许我的初始方法不好,可以用其他方式完成。如果您有解决方案,请告诉我。
谢谢。
3个回答

3
要实现您所描述的功能,您需要首先使用get_it创建导航服务。按照以下步骤实现结果:

1. 创建导航服务

import 'package:flutter/material.dart';

class NavigationService {
  final GlobalKey<NavigatorState> navigatorKey =
      new GlobalKey<NavigatorState>();

  Future<dynamic> navigateTo(String routeName) {
    return navigatorKey.currentState!
        .push(routeName);
  }

  goBack() {
    return navigatorKey.currentState!.pop();
  }
}


这使您能够从应用程序的任何位置导航,而无需构建上下文。这个导航键是您可以使用的,以获得当前上下文的AppLocalization实例。 请参考FilledStacks教程,了解如何在没有构建上下文的情况下进行导航。

https://www.filledstacks.com/post/navigate-without-build-context-in-flutter-using-a-navigation-service/

2. 注册

GetIt locator = GetIt.instance;

void setupLocator() {
  ...
  locator.registerLazySingleton(() => NavigationService());
  ...
}

3. 在Material App中分配导航键

return MaterialApp(
    ...
    navigatorKey: navigationService.navigatorKey,
    ...
  ),

3. 为 AppLocalizations 创建一个实例,并在你想要使用的地方导入它

localeInstance() => AppLocalizations.of(locator<NavigationService>().navigatorKey.currentContext!)!;

3. 实际应用案例

import 'package:{your_app_name}/{location_to_this_instace}/{file_name}.dart';

localeInstance().your_localization_variable

2
您可以将构建器添加到您的MaterialApp中,并使用可用的上下文设置其中的服务定位器。例如:
  Widget build(BuildContext context) {
    return MaterialApp(
          builder: (context, widget) {
            setUpServiceLocator(context);
            return FutureBuilder(
                future: getIt.allReady(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (snapshot.hasData) {
                    return widget;
                  } else {
                    return Container(color: Colors.white);
                  }
                });
          },
        );
}

服务定位器设置:

void setUpServiceLocator(BuildContext context) {
  getIt.registerSingleton<AppLocalizations>(AppLocalizations.of(context));
}

请添加一个调用它的示例。 - genericUser

0

您可以使用一些不可本地化的启动画面,结合FutureBuildergetIt.allReady()

例如:

  class SplashScreen extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return FutureBuilder<void>(
        future: getIt.allReady(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            // Navigate to main page (with replace)
          } else if (snapshot.hasError) {
            // Error handling
          } else {
            // Some pretty loading indicator
          }
        },
      );
    }

我想推荐 injectable 包,用于处理 get_it。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接