Flutter 使用MaterialApp上的MultiProvider定义的服务中的访问本地化
当这些服务在MaterialApp上方的多提供者中定义时,我如何访问服务中的本地化(翻译)Flutter 使用MaterialApp上的MultiProvider定义的服务中的访问本地化,flutter,Flutter,当这些服务在MaterialApp上方的多提供者中定义时,我如何访问服务中的本地化(翻译) 现在我得到了“错误:在这个上面找不到正确的提供程序”\u DefaultInheritedProviderScope我最终将MultiProvider移动到MaterialApp的builder方法。这基本上就是我问题的答案 不幸的是,RouteObserver必须位于MaterialApp之上,因为它需要传递给MaterialApp的NavigatorObserver参数 class MyApp ext
现在我得到了“错误:在这个上面找不到正确的提供程序”\u DefaultInheritedProviderScope我最终将MultiProvider移动到MaterialApp的builder方法。这基本上就是我问题的答案 不幸的是,RouteObserver必须位于MaterialApp之上,因为它需要传递给MaterialApp的NavigatorObserver参数
class MyApp extends StatelessWidget {
MyApp({
this.isZebra,
this.scanWithCamera,
});
final bool isZebra;
final bool scanWithCamera;
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: providers(isZebra: isZebra, scanWithCamera: scanWithCamera),
child: Builder(
builder: (BuildContext context) => MaterialApp(
localizationsDelegates: [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
theme: companyTheme(context),
onGenerateTitle: (context) => S.of(context).title,
navigatorObservers: [
Provider.of<RouteObserver>(
context,
listen: false,
),
],
home: S005StartupPage(),
),
),
);
}
}
现在可以访问TranslationService中的翻译代码[s.of(_context)],以便其他服务可以使用翻译
class MyApp extends StatelessWidget {
MyApp({
this.isZebra,
this.scanWithCamera,
});
final bool isZebra;
final bool scanWithCamera;
@override
Widget build(BuildContext context) {
return Provider<RouteObserver>(
create: (_) => RouteObserver(),
child: Builder(builder: (context) {
return MaterialApp(
builder: (context, child) => MultiProvider(
providers: providers(
isZebra: isZebra,
scanWithCamera: scanWithCamera,
),
child: child,
),
localizationsDelegates: [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
theme: companyTheme(context),
onGenerateTitle: (context) => S.of(context).title,
navigatorObservers: [
Provider.of<RouteObserver>(
context,
listen: false,
),
],
home: S005StartupPage(),
);
}),
);
}
}
类MyApp扩展了无状态小部件{
MyApp({
这是斑马,
这是用相机扫描的,
});
最后的布尔伊斯斑马;
带摄像头的最终bool扫描;
@凌驾
小部件构建(构建上下文){
返回提供者(
创建:()=>RouteObserver(),
子:生成器(生成器:(上下文){
返回材料PP(
生成器:(上下文,子项)=>MultiProvider(
提供者:提供者(
伊斯斑马:伊斯斑马,
用相机扫描:用相机扫描,
),
孩子:孩子,
),
本地化授权:[
美国代表,
GlobalMaterialAllocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalSuperTinoLocalizations.delegate,
],
supportedLocales:S.delegate.supportedLocales,
主题:公司主题(上下文),
onGenerateTitle:(上下文)=>S.of(上下文).title,
导航观察者:[
提供者(
上下文
听着:错,
),
],
主页:S005StartupPage(),
);
}),
);
}
}