如何在Flutter中将国际化对象传递给子小部件 [英] How do I pass an Internationalization Object to Child Widgets in Flutter

查看:72
本文介绍了如何在Flutter中将国际化对象传递给子小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是从Flutter / dart入门,为PHP过渡,并且努力弄清楚如何将类传递到小部件中。

Just getting started with Flutter/dart, transitioning for PHP, and struggling to figure out how to pass classes into widgets.

我正在使用Flutter创建我的第一个android和iOS应用程序。

I am working on creating my first android and iOS applications using flutter.

我正在致力于国际化和使用我拥有的国际化类,在我的初始构建页面上一切正常。但是,当将其传递给另一个小部件时,我得到:

I am working with internationalization and everything works fine at my initial build page using the internationalization class I have. However, when passing it on to another widget I get:


NoSuchMethodError:吸气剂textTitle被调用为null。

接收方:null

尝试调用:textTitle

NoSuchMethodError: The getter textTitle was called on null.
Receiver: null
tried calling: textTitle

处理此问题的最佳方法是什么?

What is the best way of handling this?

Flutter Doctor

  [✓] Flutter (Channel beta, v0.1.5, on Mac OS X 10.13.3 17D47, locale en-US)
  [✓] Android toolchain - develop for Android devices (Android SDK 27.0.1)
  [✓] Android Studio (version 3.0)
  [✓] Connected devices (1 available)

class HnLocalizations{
        HnLocalizations(this.locale);

        final Locale locale;

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

        static Map<String, Map<String, String>> _localizedValues = {
            'en': {
                'btnLabelLoginS1': 'Login',
                'btnLabelRegisterS1': 'Sign Up'
            },
        ;

        String get s1ButtonLabelLogin =>
            _localizedValues[locale.languageCode]['btnLabelLoginS1'];

class HnLocalizationsDelegate extends LocalizationsDelegate<HnLocalizations> {
        const HnLocalizationsDelegate();

        @override
        bool isSupported(Locale locale) => ['en', 'es'].contains(locale.languageCode);

        @override
        Future<HnLocalizations> load(Locale locale) =>
            new SynchronousFuture<HnLocalizations>(new HnLocalizations(locale)); //HnLocalizations.load(locale);

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

主飞镖

void main() {

        runApp(new MaterialApp(
            localizationsDelegates: [
                const HnLocalizationsDelegate(),
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
            ],
            supportedLocales: [
                const Locale('en', 'US'), /// Americans
                const Locale('en', 'GB') /// Brits
            ],
            title: 'HN',
            home: new EntryPage(),
        ));

    }

    class EntryPage extends StatelessWidget {

       final HnColors _hnColors = new HnColors();

        @override
        Widget build(BuildContext context) {

            return new Scaffold(
                appBar: new AppBar(
                    // !!!**** THIS WORKS AS EXPECTED ****!!!!
                    title: new Text(HnLocalizations.of(context).s1ButtonLabelLogin),
                    backgroundColor: _hnColors.transparent(),
                    elevation: 0.0,
                ),
                backgroundColor: _hnColors.accent(),
                body: new Container(
                    decoration: new BoxDecoration(
                        image: new DecorationImage(
                            image: new AssetImage("assets/Background_World.png"),
                            fit: BoxFit.fitWidth,
                        ),
                    ),
                    child: new PanelArea(),
                )
            );
        }
    }


    class PanelArea extends StatelessWidget {

        @override
        Widget build(BuildContext context) {

            HnColors _hnColors = new HnColors();

            return new Container(
                child: new Center(
                    child: new Container(
                        decoration: new BoxDecoration(
                            borderRadius: new BorderRadius.circular(15.0),
                            color: _hnColors.transparent()
                        ),
                        child: new Column(
                            children: [
                                new Image.asset('assets/Icon_Intro_login'),
                                new Text(
                                    // !!!**** ERROR IS HERE ****!!!!
                                    HnLocalizations.of(context).s1M1HeaderTitle,
                                    style: new TextStyle(
                                        color: _haillioColors.white()
                                    ),
                                ),

处理此问题的最佳方法是什么?

What is the best way of handling this?

更新时间:2018年3月11日

我ve发现,如果我将所有代码都移到main.dart文件中。所有本地化工作正常。但是,当我将小部件移动到单独的dart文件中时,即使所有代码都相同,也会再次出现错误。

Update: Mar 11 2018
I've discovered that if I move all of the code into the main.dart file. All localization works fine. However, when I move my widgets into a separate dart file, the errors come back, even though all of the code is the same.

更新:2018年3月12日
nicolás-carrasco通过这篇文章中的引用指向正确的方向(谢谢)。该问题与导入有关,在本文中的中解决了,最终成为了对我有用的解决方案。答案中添加了该示例。

Update: Mar 12 2018 nicolás-carrasco pointed in the right direction by a reference in this post (Thank you). The problem had to do with imports which is addressed in this post here which ended up being the solution that worked for me. The example is added below in answers.

推荐答案

NicolásCarrasco 指出了一种与导致我问题的原因有关的解决方案,通过链接到此帖子

Nicolás Carrasco pointed towards a solution which was related to what was causing the problem for me by way of link to this post.

localization.dart 文件导入中,我有:

import 'package:hn/hnLocalization.dart';

main.dart 文件导入中我有:

import 'hnLocalization.dart';

这些与在此处

确保使用相对路径vs软件包导入了所有文件,从而解决了该问题。区别在于我的文件而不是依赖项使用相对路径。最初,这部分卡住了。

Making sure that all files are imported using relative paths vs packages resolved the problem. The distinction is that my files, not the dependencies use relative path. That part stumped at first.

现在我的 localization.dart 文件具有以下内容。

Now my localization.dart file has the following.

import 'hnLocalization.dart'; // <--- Using relative Path

class PanelArea extends StatelessWidget {

        @override
        Widget build(BuildContext context) { ...

        child: new Column(
            children: [
                new Image.asset('assets/Icon_Intro_login'),

                // This Now Works --->
                new Text(HnLocalizations.of(context).s1M1HeaderTitle,
            ] 
       ...

,现在一切就绪。

这篇关于如何在Flutter中将国际化对象传递给子小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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