【问题标题】:Select listview item, place value in form field, and close list view - Flutter选择列表视图项,在表单字段中放置值,然后关闭列表视图 - Flutter
【发布时间】:2021-03-30 13:32:48
【问题描述】:

我有一个连接到 google places api 的列表视图,它会随着用户在地址处输入而更新。当用户点击我想要的地址时,该地址将被放置在文本字段中并关闭列表视图。

以下代码中的selectedItem 是用户选择的项目。我正在尝试使用 selectedItem 更新 textformfield。有什么想法吗?

TextFormFiled 和 ListView.builder:

    TextFormField(
      controller: _controller,
      decoration: textInputDecoration.copyWith(
          labelText: 'Address or Perferred City'),
    ),
    ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: _placeList.length,
        itemBuilder: (context, index) {
          return ListTile(
              title: Text(_placeList[index]["description"]),
              onTap: () {
                selectedItem = _placeList[index]["description"];
                print(selectedItem);
              });
        }),

在应用程序中构建的列表视图的图像:

【问题讨论】:

    标签: flutter listview google-places-api google-places


    【解决方案1】:

    您可以在此处使用if 条件。最初selectedItemnull,所以ListView 是可见的,一旦选择了一个项目,它就不是空的,所以我们隐藏了ListView。确保在您的 onTap 方法中使用 setState。一个简单的演示是

    Column(
      children: [
        TextFormField(),
        if (selectedItem == null)
          ListView.builder(),
      ],
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多