zoukankan      html  css  js  c++  java
  • Flutter: SearchDelegate 委托showSearch定义搜索页面的内容

    class _MyHomeState extends State<MyHome> {
      List<String> _list = List.generate(100, (i) => 'item $i');
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Flutter Search'),
            actions: <Widget>[
              Builder(
                builder: (context) {
                  return IconButton(
                    icon: Icon(Icons.search),
                    onPressed: () async {
                      String r = await showSearch<String>(
                        context: context,
                        delegate: ListSearchPage(_list),
                      );
                      Scaffold.of(context).showSnackBar(
                        SnackBar(
                          content: Text(r),
                          action: SnackBarAction(
                            label: 'CLOSE',
                            onPressed: () {
    
                            },
                          ),
                        ),
                      );
                    },
                  );
                },
              )
            ],
          ),
          body: ListView(
            children: <Widget>[
              for (var el in _list)
                ListTile(
                  title: Text(el),
                ),
            ],
          ),
        );
      }
    }
    
    class ListSearchPage extends SearchDelegate<String> {
      List<String> list;
      String select;
    
      ListSearchPage(this.list);
    
      @override
      appBarTheme(BuildContext context) {
        return Theme.of(context);
      }
    
      @override
      List<Widget> buildActions(BuildContext context) {
        return [
          IconButton(
            icon: Icon(Icons.close),
            onPressed: () {
              query = '';
            },
          ),
        ];
      }
    
      @override
      Widget buildLeading(BuildContext context) {
        return IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            close(context, '');
          },
        );
      }
    
      /// 用户从搜索页面提交搜索后显示的结果
      @override
      Widget buildResults(BuildContext context) {
        var filterList = list.where((String s) => s.contains(query.trim()));
        return ListView(
          children: <Widget>[
            for (String item in filterList)
              ListTile(
                leading: Icon(
                  Icons.message,
                  color: Colors.blue,
                ),
                title: Text(
                  item,
                  style: Theme.of(context).textTheme.title,
                ),
                onTap: () {
                  close(context, item);
                },
              ),
          ],
        );
      }
    
      /// 当用户在搜索字段中键入查询时,在搜索页面正文中显示的建议
      @override
      Widget buildSuggestions(BuildContext context) {
        var filterList = list.where((String s) => s.contains(query.trim()));
        return ListView(
          children: <Widget>[
            for (String item in filterList)
              ListTile(
                leading: Icon(Icons.message),
                title: Text(
                  item,
                  style: Theme.of(context).textTheme.title,
                ),
                onTap: () {
                  close(context, item);
                },
              ),
          ],
        );
      }
    }
    
  • 相关阅读:
    ORM是什么?及ORM框架是什么?
    Spring与其两大核心
    装箱和拆箱
    ==和equals的比较
    Vue中ESlint配置文件eslintrc.js文件详解
    RESTful API规范
    CORS跨域djangosetting.py 配置
    LDAP
    模拟浏览器发送请求报文
    学HTTP协议所要知道的基础知识(微总结)
  • 原文地址:https://www.cnblogs.com/ajanuw/p/10952909.html
Copyright © 2011-2022 走看看