【问题标题】:Can't search contacts无法搜索联系人
【发布时间】:2019-01-28 04:51:40
【问题描述】:

所以我制作了一个静态联系人列表,并尝试添加搜索栏,但是我无法使用搜索栏搜索联系人。当我点击搜索栏时,它会打开然后关闭。键盘会弹出一会儿,然后会关闭。这个想法是为了使搜索具有预测性,因此当输入名称时,它会根据数据库中的名称列出已关闭的名称。有什么想法吗?

class ContactsPage extends StatefulWidget {
  Widget appBarTitle = new Text("Contacts");
  Icon actionIcon = new Icon(Icons.search);

【问题讨论】:

  • 你能给样本kContacts 值在我们本地运行它吗?

标签: flutter flutter-layout


【解决方案1】:

我已使用 预测搜索 更新了代码并删除了未使用的代码。

import 'package:flutter/material.dart';

class Contact {
  final String fullName;

  const Contact({this.fullName});
}

void main() => runApp(MaterialApp(
      debugShowCheckedModeBanner: false,
      home: ContactsPage(),
    ));

class ContactsPage extends StatefulWidget {
  Widget appBarTitle = new Text("Contacts");
  Icon actionIcon = new Icon(Icons.search);
  final List<Contact> contacts = [
    Contact(
      fullName: 'Ganesh',
    ),
    Contact(
      fullName: 'Dinesh',
    ),
    Contact(
      fullName: 'Somesh',
    ),
    Contact(
      fullName: 'Ramesh',
    )
  ];

  @override
  State<StatefulWidget> createState() {
    return new _ContactPage(contacts);
  }
}

class _ContactPage extends State<ContactsPage> {
  List<Contact> filteredContacts;

  _ContactPage(this.filteredContacts);

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
          appBar: new AppBar(
            title: widget.appBarTitle,
            actions: <Widget>[
              new IconButton(
                icon: widget.actionIcon,
                onPressed: () {
                  setState(() {
                    if (widget.actionIcon.icon == Icons.search) {
                      widget.actionIcon = new Icon(Icons.close);
                      widget.appBarTitle = new TextField(
                        style: new TextStyle(
                          color: Colors.white,
                        ),
                        decoration: new InputDecoration(
                            prefixIcon:
                                new Icon(Icons.search, color: Colors.white),
                            hintText: "Search...",
                            hintStyle: new TextStyle(color: Colors.white)),
                        onChanged: (value) {
                          filterContacts(value);
                        },
                      );
                    } else {
                      widget.actionIcon =
                          new Icon(Icons.search); //reset to initial state
                      widget.appBarTitle = new Text("Contacts");
                      filteredContacts = widget.contacts;
                    }
                  });
                },
              ),
            ],
          ),
          body: new ContactList(filteredContacts)),
      // replace the body with your contacts list view
    );
  }

  void filterContacts(String value) {
    var temp = widget.contacts.where((contact) {
      return contact.fullName.contains(value);
    }).toList();
    setState(() {
      filteredContacts = temp;
    });
  }
}

class ContactList extends StatelessWidget {
  final List<Contact> _contacts;

  ContactList(this._contacts);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemBuilder: (context, index) {
          return new _ContactListItem(this._contacts[index]);
        },
        itemCount: this._contacts.length,
        padding: new EdgeInsets.symmetric(vertical: 8.0));
  }
}

class _ContactListItem extends ListTile {
  _ContactListItem(Contact contact)
      : super(
            title: new Text(contact.fullName),
            leading: new CircleAvatar(child: new Text(contact.fullName[0])));
}

参考屏幕截图:

【讨论】:

  • @Dinesh_Balasubramanianit 不允许我在单击搜索栏时搜索联系人。没有键盘出现。
  • 上面的代码对我有用。你做了什么改变
  • @Dinesh_Balasubramanian 还是一样,我会在不同的手机上测试。你不会看我的其他颤振问题吗?
  • 你在模拟器中测试吗?
  • 不,正在运行设备。我的笔记本电脑不支持模拟器
猜你喜欢
  • 2020-12-07
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
相关资源
最近更新 更多