如何在打字时使 Flutter 搜索代码工作?

清除

我似乎真的无法弄清楚我的代码有什么问题。搜索功能似乎只有在我输入搜索查询并进行热加载后才能工作。我打字时它不能立即工作。我错过了什么吗?是不是因为API有很多数据?

我已经包含了我的代码的所有相关部分,从模型到搜索功能。

import 'dart:async';
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

class Search extends StatefulWidget {
  @override
  _SearchState createState() => new _SearchState();
}

class _SearchState extends State<Search> {
  TextEditingController controller = new TextEditingController();


  Future getUserDetails() async {
    final response =
        await http.get('http://makeup-api.herokuapp.com/api/v1/products.json');
    final responseJson = json.decode(response.body);
    setState(() {
      for (Map user in responseJson) {
        _userDetails.add(UserDetails.fromJson(user));
      }
    });
  }

  @override
  void initState() {
    super.initState();
    getUserDetails();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Home'),
        elevation: 0.0,
      ),
      body: new Column(
        children: <Widget>[
          new Container(
            color: Theme.of(context).primaryColor,
            child: new Padding(
              padding: const EdgeInsets.all(8.0),
              child: new Card(
                child: new ListTile(
                  leading: new Icon(Icons.search),
                  title: new TextField(
                    controller: controller,
                    decoration: new InputDecoration(
                        hintText: 'Search', border: InputBorder.none),
                    onChanged: onSearchTextChanged,
                  ),
                  trailing: new IconButton(
                    icon: new Icon(Icons.cancel),
                    onPressed: () {
                      controller.clear();
                      onSearchTextChanged('');
                    },
                  ),
                ),
              ),
            ),
          ),
          new Expanded(
            child: _searchResult.length != 0 || controller.text.isNotEmpty
                ? new ListView.builder(
                    itemCount: _searchResult.length,
                    itemBuilder: (context, i) {
                      return new Card(
                        child: new ListTile(
                          leading: new CircleAvatar(
                            backgroundImage: new NetworkImage(
                              _searchResult[i].imageLink
                            ),
                          ),
                          title: new Text(_searchResult[i].name +
                              ' ' +
                              _searchResult[i].brand),
                        ),
                        margin: const EdgeInsets.all(0.0),
                      );
                    },
                  )
                : new ListView.builder(
                    itemCount: _userDetails.length,
                    itemBuilder: (context, index) {
                      return new Card(
                        child: new ListTile(
                          leading: new CircleAvatar(
                            backgroundImage: new NetworkImage(
                              _userDetails[index].imageLink,
                            ),
                          ),
                          title: new Text(_userDetails[index].name +
                              ' ' +
                              _userDetails[index].brand),
                        ),
                        margin: const EdgeInsets.all(0.0),
                      );
                    },
                  ),
          ),
        ],
      ),
    );
  }

  onSearchTextChanged(String text) async {
    _searchResult.clear();
    if (text.isEmpty) {
      setState(() {});
      return;
    }

    _userDetails.forEach((userDetail) {
      if (userDetail.name.contains(text) ||
          userDetail.brand.contains(text)) _searchResult.add(userDetail);
    });

    setState(() {});
  }
}

List<UserDetails> _searchResult = [];

List<UserDetails> _userDetails = [];

final String url = 'http://makeup-api.herokuapp.com/api/v1/products.json';

class UserDetails {
  final String name, brand, imageLink;

  UserDetails({this.name, this.brand, this.imageLink = 'https://cdn.business2community.com/wp-content/uploads/2017/08/blank-profile-picture-973460_640.png'});

  factory UserDetails.fromJson(Map<String, dynamic> json) {
    return new UserDetails(
      name: json['name'],
      brand: json['brand'],
    );
  }
}
w461

你应该onChanged: onSearchTextChanged,

onChanged: (value) => onSearchTextChanged(value),

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

嵌套for循环在打印时如何工作?

惰性模块加载在打字稿中如何工作?

如何使旧样式类在打字稿中工作?

如何在Flutter DropDown按钮中搜索

Flutter:Firebase基本查询或基本搜索代码

如何在打字稿中的Angular2 highcharts中添加鼠标滚轮代码

代码搜索-搜索特定的打字稿功能时排除键入

如何在Flutter的Google Maps顶部制作浮动搜索栏?

我在Flutter / Dart异步代码执行方面遇到问题,因为它如何工作

Provider中的侦听器如何在Flutter中工作?

如何在Flutter中对JSON数据执行文本搜索?

“ flutter create”命令如何工作?

您如何编写Vlookup代码以搜索其他工作簿?

搜索用户未在Flutter中工作,如何解决此问题?

如何在android studio或vscode中搜索整个Flutter源代码

如何优化VBA代码以从其他工作表中搜索值

搜索时%通配符如何在sql中准确地工作

如何在打开工作簿时使 VBA 函数工作

代码建议停止工作(Flutter)

如何从 Flutter 插件调用代码?

如何优化此 Flutter 代码以减少工作量?

如何在搜索结果中显示匹配的用户列表 [Flutter]

`Equals` 在打字稿中是如何工作的?

我应该如何在打字稿代码中编写 `root` 类型?

如何在 Flutter 中為 TextField 添加搜索圖標?

** 在搜索路径时如何工作

Flutter如何在flutter搜索过滤结果无结果时显示消息

Flutter - 如何创建这种动画搜索栏?

如何在打开 Kickstarter 源代码时“运行 make bootstrap”