【问题标题】:l10n called before initStatel10n 在 initState 之前调用
【发布时间】:2019-02-25 10:26:22
【问题描述】:

发现 Flutter 对 l10n 和 void initState() 的处理存在问题。

l10n 工作得很好,因为它是自己的。完成这项工作,但我发现了一个我认为应该标记的特定场景。 我将通过显示example 来更具体。 以下代码取自gallery app

...
  @override
  void initState() {
    super.initState();
    _navigationViews = <NavigationIconView>[
      NavigationIconView(
        icon: const Icon(Icons.access_alarm),
        title: 'Alarm',
        color: Colors.deepPurple,
        vsync: this,
      ),
      NavigationIconView(
        activeIcon: CustomIcon(),
        icon: CustomInactiveIcon(),
        title: 'Box',
        color: Colors.deepOrange,
        vsync: this,
      ),
      NavigationIconView(
        activeIcon: const Icon(Icons.cloud),
        icon: const Icon(Icons.cloud_queue),
        title: 'Cloud',
        color: Colors.teal,
        vsync: this,
      ),
      NavigationIconView(
        activeIcon: const Icon(Icons.favorite),
        icon: const Icon(Icons.favorite_border),
        title: 'Favorites',
        color: Colors.indigo,
        vsync: this,
      ),
      NavigationIconView(
        icon: const Icon(Icons.event_available),
        title: 'Event',
        color: Colors.pink,
        vsync: this,
      )
    ];

    _navigationViews[_currentIndex].controller.value = 1.0;
  }
...

我正在努力在 initState 函数中使用本地化。 您可以(在某种程度上)通过使用用户的语言代码并手动翻译而不是使用locales.dart 文件来绕过这个问题,但我认为这是一个应该解决的持续问题。

注意:当我写这篇文章时,我想到了一个想法,通过调用前一课的 l10n 字符串来绕过这个 - 测试这个理论。

【问题讨论】:

    标签: dart flutter flutter-layout


    【解决方案1】:

    所以我最终“传递”了以前班级的价值观。这个“问题”可以通过使用 ether InheritedWidget 或“传递值”来解决。

    【讨论】:

    • 如果initState 不起作用,didChangeDependencies 也可能起作用。
    • 这是针对除gallery app (bottomnavbar) example 之外的任何其他示例的解决方案。
    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 2021-08-16
    • 2021-02-08
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    相关资源
    最近更新 更多