【问题标题】:TextField keeps old valueTextField 保留旧值
【发布时间】:2020-06-17 15:30:33
【问题描述】:

我在 Flutter 中遇到了文本字段的问题。 我需要获取城镇或邮政编码以将用户重定向到另一个页面。但是,即使文本字段中的文本发生更改,我似乎也保留了旧值。 我试图清除它,提出条件,但我不明白为什么文本不保持当前值。 这里有一些代码:

检查是否是巴黎,对于自治市镇

 @override
 void initState() {
    editingController.addListener((){
       setState(() {
         if(editingController.text.isEmpty) {
           editingController.clear();
           isParis = false;
         }
      });
   });
 super.initState();
}

重定向按钮,getPcFromQuery 方法(它是一个异步方法)检索与城镇名称相关的数据。

RaisedButton(
  onPressed: (){
    print(editingController.text);
    getPCFromQuery(editingController.text)
      .then((town) {city = town.featureName; postalCode = town.postalCode;});
       setState(() {
         if(city.toString().toLowerCase() == "paris")
           isParis = true;
         else
           isParis = false;
         });
      print("postalCode : $postalCode");
      postalCode != null && city.toString().toLowerCase() != "paris"?
      Navigator.of(context).push(MaterialPageRoute(builder: (context) => PreferencesPage(postalCode: postalCode))) :
      Container(child: Text(""));
    })

但是当我点击下一个按钮时(我需要点击它两次才能使其工作)并且当我更改文本字段中的值时,它会保留之前输入的值。

你知道它是从哪里来的吗?

这是构建的 TextField:

class SearchBar extends StatelessWidget {
    const SearchBar({
    Key key,
    @required this.editingController,
   }) : super(key: key) ;

  final TextEditingController editingController;

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
        color: Colors.grey.withOpacity(0.1),
        borderRadius: BorderRadius.circular(10.0)),
        child: TextField(
           controller: editingController,
           decoration: InputDecoration(
             hintText: AppStrings.searchTown,
             hintStyle: GoogleFonts.notoSans(fontSize: 14.0),
             border: InputBorder.none,
             fillColor: Colors.grey.withOpacity(0.5),
             prefixIcon: Icon(Icons.search, color: Colors.grey)
           ),
        )
    );
  }
 }

还有方法 _getPcFromQuery :

_getPCFromQuery(String city) async {
   var addresses = await Geocoder.local.findAddressesFromQuery(city);
   var first = addresses.first;

   print(first.featureName);
   print(first.addressLine);

   return first;
}

奇怪的是,当我第一次尝试时,_getPcFromQuery 方法中的打印得到文本,但打印“print("postalCode : $postalCode");" onPressed 方法上的按钮为空。

【问题讨论】:

  • 可以分享一下文本字段所在的build方法吗?
  • @LucileS 如果您提供可重现的代码而不是代码的零碎部分,我们会更容易为您提供帮助。大多数情况下,您会在创建可重现代码时自己发现错误

标签: flutter


【解决方案1】:

指定TextField的关键参数。

class SearchBar extends StatelessWidget {
  const SearchBar({
    Key key,
    @required this.editingController,
   }) : super(key: key) ;

  final TextEditingController editingController;

  final searchKey = GlobalKey(); // <-- create a global key

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
        color: Colors.grey.withOpacity(0.1),
        borderRadius: BorderRadius.circular(10.0)),
        child: TextField(
           key: searchKey, // <-- specify the declared key here
           controller: editingController,
           decoration: InputDecoration(
             hintText: AppStrings.searchTown,
             hintStyle: GoogleFonts.notoSans(fontSize: 14.0),
             border: InputBorder.none,
             fillColor: Colors.grey.withOpacity(0.5),
             prefixIcon: Icon(Icons.search, color: Colors.grey)
           ),
        )
    );
  }
}

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2019-05-17
    • 1970-01-01
    • 2019-07-08
    • 2013-06-29
    相关资源
    最近更新 更多