【问题标题】:Why navigator not working in onSubmitted of TextField in flutter?为什么导航器在颤动中无法在 TextField 的 onSubmitted 中工作?
【发布时间】:2020-10-05 07:12:23
【问题描述】:

这是我的代码:

AppBar(
        actions: [
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () => Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => SearchScreen(
                  query: _query,
                ),
              ),
            ),
          ),
        ],
        title: Container(
          height: 40,
          decoration: BoxDecoration(
            color: kSecondaryColor.withOpacity(0.1),
            borderRadius: BorderRadius.circular(15),
          ),
          child: TextField(
            onSubmitted: (value) {
              print('HHi');
              _query.searchBy = value;
              Navigator.popAndPushNamed(context, '/ads');
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => SearchScreen(
                    query: _query,
                  ),
                ),
              );
            },
            decoration: InputDecoration(
                border: InputBorder.none,
                enabledBorder: InputBorder.none,
                focusedBorder: InputBorder.none,
                prefixIcon: Icon(Icons.search),
                isDense: true,
                contentPadding:
                    EdgeInsets.symmetric(vertical: lang == 'en' ? 11 : 5),
                hintText: 'Search'.tr()),
          ),
        ),
      ),

在这里,我尝试了一切导航并尝试了 pushNamed 方式和正常推送方式,但没有任何反应,打印工作正常但 Navigator 不起作用,我尝试将 TextField 包装在 Builder 中但仍然无法正常工作,即使它没有显示任何错误,什么也没发生,矛盾的是,当我点击同一个小部件和同一个 AppBar 中的按钮时,它就起作用了。

我需要在键盘上点击回车后进入搜索结果屏幕,我希望有其他方法而不是onSubmitted。

颤振版本 1.22

【问题讨论】:

  • 不确定是否相关,但您是否查看了有关 Navigator 2.0 的最新更新?
  • @OsaXma 不,我在哪里可以查看?
  • 我搜了一下,写代码有什么区别?

标签: flutter


【解决方案1】:

需要按下键盘的回车键。并从您的文本字段中集中注意力。

Check Here

【讨论】:

    猜你喜欢
    • 2021-12-01
    • 2021-06-06
    • 2020-09-20
    • 2020-08-19
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    相关资源
    最近更新 更多