Flutter搜索栏未显示所有信息

slhcrj9b  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(154)

谁能帮我我有一个问题我的搜索栏不工作。键入要搜索的文本,放入搜索框,但没有结果出现,我的代码运行正常。没有错误发生,但有一个问题,其中一些是故障,使搜索栏不可用。enter image description here

class view_problem extends StatefulWidget {
 @override
  State<view_problem> createState() => _view_problemState();
}

class _view_problemState extends State<view_problem> {
  List<problemModel> problemlist = [];
  StreamController _streamController = StreamController();
  Future getAllProblem() async {
    problemlist = await problemcontrollers().getProblem();
    _streamController.sink.add(problemlist);
  }

  @override
  void initState() {
    // TODO: implement initState
    Timer.periodic(Duration(seconds: 1), (timer) {
      getAllProblem();
    });
    super.initState();
  }

      void _filterSearch(String query) {
        List<problemModel> problemlist = [];
        for (var item in problemlist) {
         if (item.toString().contains(query.toString())) {
            problemlist.add(item);
          }
        }
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Color.fromARGB(255, 14, 12, 134),
            title: const Text('information'),
          ),
      body: Padding(
        padding: const EdgeInsets.all(10),
        child: Column(
          children: [
            const SizedBox(
              height: 20,
            ),
            TextField(
              onChanged: (value) => _filterSearch(value),
              decoration: const InputDecoration(
                  labelText: 'Search', suffixIcon: Icon(Icons.search)),
            ),
            const SizedBox(
              height: 20,
            ),
            Expanded(
              child: StreamBuilder(
                stream: _streamController.stream,
                builder: (context, snapshots) {
                  if (snapshots.hasData) {
                    return ListView.builder(
                        itemCount: problemlist.length,
                        itemBuilder: ((context, index) {
                          problemModel problem = problemlist[index];
                          return Card(
                            margin: EdgeInsets.all(10),
                            child: ListTile(
                              title: Text(
                                problem.name_surname,
                                style: TextStyle(
                                   fontWeight: FontWeight.bold, fontSize: 19),
                              ),
                              subtitle: Text(
                                problem.area,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 17),
                              ),
                              trailing: Text(
                                problem.status,
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 16),
                              ),
                            ),
                          );
                        }));
                  }
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

是我的代码的某个部分不工作或行为不端吗?

vlju58qv

vlju58qv1#

尝试这个改变,你在方法中重复了名称problemList,而且你还没有setState。

void _filterSearch(String query) {
List<problemModel> filteredList = [];
  for (var item in problemlist) {
    if (item.toString().contains(query.toString())) {
      filteredList.add(item);
    }
  }
  setState(() {
    problemlist = filteredList;
  });
}

有很多方法来解决列表,其他方法是有2个列表,一个设置完整列表的值,第二个搜索列表,有一个文本编辑控制器,一个_controller = TextEditingController()在initState()中添加一个侦听器,并添加一个使用预览方法,但与

setState(() {
    _searchList = filteredList;
  });

然后在listBuilder里面构建一个if语句,如果_controller.text==''的list是完整的list,else显示_searchList

相关问题