无法在Flutter标签栏视图和列表视图中本地化字符串

时间:2019-12-18 07:09:07

标签: flutter localization



当我尝试在TabBarView中本地化字符串时,出现此错误:

NoSuchMethodError: The method 'translate' was called on null. Receiver: null. Tried calling: translate("settingsprofile").

import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:rewahub/views/tabbar.dart';
import 'package:rewahub/widgets/app_localizations.dart';
import 'package:rewahub/widgets/styles.dart' ;
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

import 'package:flutter/material.dart';




class MainHomePage extends StatefulWidget {
  MainHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MainHomePageState extends State<MainHomePage> {
  final int initialIndex = 1;
  TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);



  String textValue = 'unknown';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      //  localizationsDelegates: [
      //           AppLocalizations.delegate,
      //           GlobalMaterialLocalizations.delegate,
      //           GlobalWidgetsLocalizations.delegate,
      //         ],
      debugShowCheckedModeBanner: false,
      home: DefaultTabController(
        initialIndex: 1,
        length: 3,
        child: Scaffold(
          resizeToAvoidBottomPadding: false,
          appBar: new PreferredSize(
            preferredSize: const Size.fromHeight(150.0),
            child: new AppBar(
              centerTitle: false,
              title: Container(
                width: MediaQuery.of(context).size.width / 1.0,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      "8956",
                      textAlign: TextAlign.left,
                      style: TextStyle(color: Colors.white, fontSize: 18.0),
                    ),
                    Image.asset(
                      'assets/images/rewahub_icon.png',
                      height: 50.0,
                    ),
                    Text(
                      "RS 434.25",
                      style: TextStyle(color: Colors.white, fontSize: 18.0),
                    ),
                  ],
                ),
              ),
              flexibleSpace: Container(
                decoration: BoxDecoration(gradient:grad),
              ),
              bottom: TabBar(
                tabs: <Widget>[
                  Tab(
                    child: new Text(

                       AppLocalizations.of(context).translate('tabvouchers'),
                      style: Theme.of(context)
                          .textTheme
                          .body1
                          .apply(color: Colors.white),
                    ),
                  ),
                  Tab(
                    child: new Text(

                     AppLocalizations.of(context).translate('tabpaynow'),
                      style: Theme.of(context)
                          .textTheme
                          .body1
                          .apply(color: Colors.white),
                    ),
                  ),
                  Tab(
                    child: new Text(

                   AppLocalizations.of(context).translate('tabpoints'),
                      style: Theme.of(context)
                          .textTheme
                          .body1
                          .apply(color: Colors.white),
                    ),
                  ),
                ],
              ),
            ),
          ),
          //body: TabBody(),
        ),
      ),
    );
  }
  }



我使用以下代码行将键转换为本地化字符串: AppLocalizations.of(context).translate('settingsprofile')

此功能在我的应用程序的所有其他屏幕(除了此屏幕)上都很好用。有谁知道为什么它不起作用以及如何解决?

我已经尝试过的一些事情:

传递主屏幕(包含所有TabBarViews的屏幕)的上下文

在构建器中包装每个ListTile

0 个答案:

没有答案