【问题标题】:Flutter search on listView with multiple values在具有多个值的 listView 上进行颤振搜索
【发布时间】:2021-10-22 18:48:09
【问题描述】:

我需要简单地在 ListView 上实现具有多个值的搜索。使用单个值可以正常工作我不知道如何将其用于多个值。

我的代码

    Padding(
      padding: const EdgeInsets.only(top: 11),
      child: Container(
        width: Width * 0.9,
        child: TextFormField(
          onChanged: (value) {
            setState(() {
              searchString = value;
            });
          },
          decoration: new InputDecoration(
            suffixIcon: Padding(
              padding: const EdgeInsets.all(5.0),
              child: ClipOval(
                child: Material(
                  color: Color(0xffdee8ec), // Button color
                  child: InkWell(
                    splashColor: Colors.red, // Splash color
                    onTap: () {},
                    child: Icon(
                      Icons.search_outlined,
                      color: kPrimaryColor,
                      size: 20,
                    ),
                  ),
                ),
              ),
            ),
            labelText: "Search",
            labelStyle:
                TextStyle(color: textGreyColor, fontFamily: 'SegoeUI'),
            filled: true,
            fillColor: Colors.white,
            focusedBorder: OutlineInputBorder(
              borderRadius: new BorderRadius.circular(10),
              borderSide: BorderSide(color: kPrimaryColor, width: 1.0),
            ),
            enabledBorder: OutlineInputBorder(
              borderRadius: new BorderRadius.circular(10),
              borderSide:
                  BorderSide(color: Color(0xffE6E6E6), width: 1.0),
            ),

            border: new OutlineInputBorder(
              borderRadius: new BorderRadius.circular(10),
              borderSide: new BorderSide(color: Color(0xffE6E6E6)),
            ),
            //fillColor: Colors.green
          ),
          keyboardType: TextInputType.emailAddress,
          style: new TextStyle(
              fontFamily: "SegoeUI", color: kPrimaryColor),
        ),
      ),
    ),
    Container(
      width: Width * 0.9,
      child: ListView.builder(
        padding: EdgeInsets.only(top: 10),
        itemCount: _serviceList.length,
        shrinkWrap: true,
        scrollDirection: Axis.vertical,
        physics: ScrollPhysics(),
        itemBuilder: (context, index) {
          return _serviceList[index]['serviceName']
                  .toString()
                  .contains(searchString)
              ? Padding(
                  padding: const EdgeInsets.only(bottom: 7),
                  child: Container(
                    width: Width * 0.9,
                    child: Card(
                      elevation: 2,
                      color: Colors.white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      child: Padding(
                          padding: const EdgeInsets.all(10.0),
                          child: Row(
                            mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                            children: [
                              Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.start,
                                crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '${_serviceList[index]['serviceName']} ',
                                    style: TextStyle(
                                        fontFamily: 'SegoeUI',
                                        color: kPrimaryColor,
                                        fontSize: 15),
                                  ),
                                  SizedBox(
                                    height: 5,
                                  ),
                                  Text(
                                    '${_serviceList[index]['serviceDuration']}',
                                    style: TextStyle(
                                        fontFamily: 'SegoeUI',
                                        color: textGreyColor,
                                        fontSize: 15),
                                  )
                                ],
                              ),
                            ],
                          )),
                    ),
                  ),
                )
              : Container();
        },
      ),
    )

您可以看到它现在只使用 ServiceName 进行搜索

【问题讨论】:

标签: flutter dart


【解决方案1】:

您可以按照自己的方式继续进行。根据需要设置多个输入字段并获取要搜索的值并将其与该特定部分一起作为条件包含:

return _serviceList[index]['serviceName']
                  .toString()
                  .contains(searchString)
              ? 

我想这会更容易和成功。如果您需要进一步说明,请发表评论。

【讨论】:

  • Nup 我需要从一个 TextField 完成
  • 好的,我认为这不是问题。因此,只需将 searchString 用于 _serviceList 中的多个字段。如果出现问题,请根据需要更改字段的数据类型。如果全部都在字符串中,则重复使用相同的条件,例如: _serviceList[index]['x'].toString() .contains(searchString) || _serviceList[index]['y'].toString() .contains(searchString) ?
猜你喜欢
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
相关资源
最近更新 更多