Flutter:SearchBar提示国际化的文本

问题描述 投票:0回答:2

我想在应用栏中放置一个搜索栏。所以我宣布Appbar给它一个TextField作为标题,并在控制器上放置一个监听器:

class _SearchView extends State<SearchView> {

      TextField appBarTitle;
      final TextEditingController _filterController = new TextEditingController();

  @override
  void initState() {
   buildAppBarTitle();
    super.initState();
  }

      TextField buildAppBarTitle() {
        return appBarTitle = new TextField(
            controller: _filterController,
            decoration: new InputDecoration(
                hintText: AppLocalization.of(context).search
            )
        );
      }


    @override
      Widget build(BuildContext context) {
        return Scaffold(
           appBar: AppBar(title: appBarTitle,),
    }
 }

问题是:hintText: AppLocalization.of(context).search。由于在Widget构建之外,它没有上下文。如何设置此提示文本?

flutter internationalization searchbar appbar
2个回答
0
投票

您没有正确分配appBarTitle变量。尝试下面的代码:

class _SearchView extends State<SearchView> {

final TextEditingController _filterController = new TextEditingController();

TextField buildAppBarTitle(BuildContext context) {
    return new TextField(
        controller: _filterController,
        decoration: new InputDecoration(
        hintText: AppLocalization.of(context).search
        )
    );
}

@override
Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: buildAppBarTitle(context),),
    }
}

0
投票

最后我发现这不是代码块的真正问题,而是我如何在语言环境文件中声明String:

  String get search => Intl.message("Search", name: search);

我忘了在name参数中添加“search”,所以:

String get search => Intl.message(“Search”,name:“search”);

© www.soinside.com 2019 - 2024. All rights reserved.