无需每次调用​​(of(context)`的本地化 [英] Localizations without calling `of(context)` every time

查看:106
本文介绍了无需每次调用​​(of(context)`的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用官方Flutter本地化插件找到了本地化过程麻烦的要显示本地化的字符串,我必须调用 Localization.of(context).myAppTitle -在具有大量本地化字符串的巨大嵌套小部件树中,它不是完全圆滑或容易浏览。更不用说它看起来丑陋了。



有没有办法使用法更好?例如,是否可以使用全局变量或带有 Localization 实例成员的静态类来简化访问?例如,声明顶级 Localization 变量

  //全球范围
本地化l;


// main.dart
类_MyAppState扩展State< MyApp> {

@override
void initState(){
super.initState();
getLocaleSomehow()。then((locale){
l = Localization(locale);
setState((){});
});
}
}

然后我可以简单地致电

 文本(l.myAppTitle)

所以从本质上讲,我想问的是不调用 Localization.of(context)有什么危险和/或缺点?



如果确实需要使用 .of(BuildContext)方法访问本地化实例-我能否至少将其存储在我的 StatefulWidget 中?我在想类似的东西

  class DetailsPage扩展了StatefulWidget {
Localization _l;

@override
小部件构建(BuildContext上下文){
_l = Localization.of(context);

// ...构建小部件...
}
}

还是有其他方法可以减少本地化的麻烦?

解决方案

Localization 对象存储在 State 内,在这种情况下,效果很好。



如果只想让它看起来更好,还可以在build方法中声明变量:



< pre class = lang-dart prettyprint-override> @override
小部件build(BuildContext context){
final l = Localization.of(context);

return Text(l.myAppTitle);
}

StatefulWidget 中,您还可以在 didChangeDependencies 或仅使用可识别空值的 ?? = 运算符分配一次,因为对象不会随着时间变化:

  class _MyStatefulWidgetState扩展State< MyStatefulWidget>使用WidgetsBindingObserver {
本地化l;

@override
didChangeDependencies(){
WidgetsBinding.instance.addObserver(this);
l ?? = Localization.of(context);
super.didChangeDependencies();
}

@override
void didChangeLocales(List< Locale> locale){
l = Localization.of(context);
super.didChangeLocales(locale);
}

@override
dispose(){
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
小部件build(BuildContext context)=>文字(l.myAppTite);
}

didChangeLocales 中,您可以每次重新分配。这样可以确保变量始终具有适当的语言环境,并在首次构建时进行初始化(使用 didChangeDependencies )。请注意,我还包括了一个 WidgetsBindingObserver ,您需要按代码所示进行处理。


I find the localization procedure using the official Flutter localization plugin cumbersome. To display a localized string I have to call Localization.of(context).myAppTitle - not exactly sleek or easy to glance over in a huge nested Widget tree with lots of localized strings. Not to mention it looks ugly.

Is there a way to make the usage nicer? For example, can I use a global variable or a static class with a Localization instance member to make the access easier? For example declaring a top level Localization variable

// Somewhere in the global scope
Localization l;


// main.dart
class _MyAppState extends State<MyApp>{

    @override
    void initState() {
        super.initState();
        getLocaleSomehow().then((locale){ 
            l = Localization(locale);
            setState((){}); 
        });
    }
}

Then I could simply call

Text(l.myAppTitle)

So in an essence what I'm asking is "what are the dangers and/or disadvantages of not calling Localization.of(context)?"

If I really do need to use the .of(BuildContext) method to access the Localization instance - can I at least store it in my StatefulWidget? I'm thinking something like

class DetailsPage extends StatefulWidget{
    Localization _l;

    @override
    Widget build(BuildContext context) {
        _l = Localization.of(context);

        // ... build widgets ...
    }
}

Or is there any other way to make the localization less cumbersome?

解决方案

It is totally fine to store the Localization object inside of your State and it works very well in that case.

If you want to only make it look nicer, you could also just declare the variable in the build method:

@override
Widget build(BuildContext context) {
  final l = Localization.of(context);

  return Text(l.myAppTitle);
}

In a StatefulWidget, you could also re-assign the variable in didChangeDependencies or just assign it once using the null-aware ??= operator because the object will not change over time:

class _MyStatefulWidgetState extends State<MyStatefulWidget> with WidgetsBindingObserver {
  Localization l;

  @override
  didChangeDependencies() {
    WidgetsBinding.instance.addObserver(this);
    l ??= Localization.of(context);
    super.didChangeDependencies();
  }

  @override
  void didChangeLocales(List<Locale> locale) {
    l = Localization.of(context);
    super.didChangeLocales(locale);
  }

  @override
  dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Text(l.myAppTite);
}

In didChangeLocales, you can re-assign every time. This makes sure that the variable always holds the appropriate locale and is initialized at first build (with didChangeDependencies). Notice that I also included a WidgetsBindingObserver, which you need to handle as shown in the code.

这篇关于无需每次调用​​(of(context)`的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆