zoukankan      html  css  js  c++  java
  • Flutter-SearchDelegate搜索框

    搜索欄

    import 'package:flutter/material.dart';
    
    typedef SearchItemCall = void Function(String item);
    
    class SearchBarDelegate extends SearchDelegate<String> {
    
      @override
      List<Widget> buildActions(BuildContext context) {
        //右侧显示内容 这里放清除按钮
        return [
          IconButton(
            icon: Icon(Icons.clear),
            onPressed: () {
              query = "";
              showSuggestions(context);
            },
          ),
        ];
      }
    
      @override
      Widget buildLeading(BuildContext context) {
        //左侧显示内容 这里放了返回按钮
        return IconButton(
          icon: AnimatedIcon(
              icon: AnimatedIcons.menu_arrow, progress: transitionAnimation),
          onPressed: () {
            if (query.isEmpty) {
              close(context, null);
            } else {
              query = "";
              showSuggestions(context);
            }
          },
        );
      }
    
      @override
      Widget buildResults(BuildContext context) {
        //点击了搜索显示的页面
        return Center(
          child: Text('123'),
        );
      }
    
      @override
      Widget buildSuggestions(BuildContext context) {
        //点击了搜索窗显示的页面
        return SearchContentView();
      }
    
      @override
      ThemeData appBarTheme(BuildContext context) {
        return super.appBarTheme(context);
      }
    }
    
    class SearchContentView extends StatefulWidget {
      @override
      _SearchContentViewState createState() => _SearchContentViewState();
    }
    
    class _SearchContentViewState extends State<SearchContentView> {
      @override
      Widget build(BuildContext context) {
        return Container(
          padding: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                child: Text(
                  '大家都在搜',
                  style: TextStyle(fontSize: 16),
                ),
              ),
              SearchItemView(),
              Container(
                margin: EdgeInsets.only(top: 20),
                child: Text(
                  '历史记录',
                  style: TextStyle(fontSize: 16),
                ),
              ),
              SearchItemView()
            ],
          ),
        );
      }
    }
    
    class SearchItemView extends StatefulWidget {
      @override
      _SearchItemViewState createState() => _SearchItemViewState();
    }
    
    class _SearchItemViewState extends State<SearchItemView> {
      List<String> items = [
        'index',
        'order',
        'main',
        '123123',
        '5test',
      ];
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Wrap(
            spacing: 10,
            // runSpacing: 0,
            children: items.map((item) {
              return SearchItem(title: item);
            }).toList(),
          ),
        );
      }
    }
    
    class SearchItem extends StatefulWidget {
      @required
      final String title;
      const SearchItem({Key key, this.title}) : super(key: key);
      @override
      _SearchItemState createState() => _SearchItemState();
    }
    
    class _SearchItemState extends State<SearchItem> {
      @override
      Widget build(BuildContext context) {
        return Container(
          child: InkWell(
            child: Chip(
              label: Text(widget.title),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10)
              ),
            ),
            onTap: () {
              print(widget.title);
            },
          ),
          color: Colors.white,
        );
      }
    }

    main

    body: Center(
              child: InkWell(
                child: Icon(Icons.search),
                onTap: (){
                  showSearch(context: context,delegate: SearchBarDelegate());
                },
              )
          ),

     demo

    import 'package:flutter/material.dart';
    import 'asset.dart';
    
    class SearchBar extends StatefulWidget {
      @override
      _SearchBarState createState() => _SearchBarState();
    }
    
    class _SearchBarState extends State<SearchBar> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('SearchBar'),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.search),
                onPressed: (){
                    showSearch(context: context, delegate: SearchBarDelegate());
                },
              ),
            ],
          ),
        );
      }
    }
    
    class SearchBarDelegate extends SearchDelegate<String>{
      //重寫交叉按鈕清空數據
      @override
      List<Widget> buildActions(BuildContext context) {
        // TODO: implement buildActions
        return [
          IconButton(
            icon: Icon(Icons.clear),
            onPressed: ()=> query = '',
          )
        ];
      }
      //重寫左邊按鈕
      @override
      Widget buildLeading(BuildContext context) {
        // TODO: implement buildLeading
        return IconButton(
          icon: AnimatedIcon(
            icon: AnimatedIcons.menu_arrow,//動態圖表
            progress: transitionAnimation,
          ),
          onPressed: () => close(context,null),
        );
      }
      //重寫返回結果
      @override
      Widget buildResults(BuildContext context) {
        // TODO: implement buildResults
        return Container(
           100,
          height: 100,
          child: Card(
            color: Colors.blueAccent,
            child: Text(query),
          ),
        );
      }
      //推薦
      @override
      Widget buildSuggestions(BuildContext context) {
        final suggestionList = query.isEmpty//判斷是否為空
          ? recentSuggest:searchList.where((input)=> input.startsWith(query)).toList();
        // TODO: implement buildSuggestions
        return ListView.builder(
          itemCount: suggestionList.length,
          itemBuilder: (context,index)=>ListTile(
            title: RichText(
              text: TextSpan(
                //被搜索的
                text: suggestionList[index].substring(0,query.length),
                style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
                //未被搜索的
                children: [
                  TextSpan(
                    text: suggestionList[index].substring(query.length),
                    style: TextStyle(color: Colors.grey)
                  )
                ]
              ),
            ),
          ),
        );
      }
    }

    數據

    const searchList = [
      'test-1',
      'test-2',
      'test-3',
      'test-4',
      'test-5'
    ];
    
    const recentSuggest = [
      '推薦 - 1',
      '推薦 - 2'
    ];
  • 相关阅读:
    向量代数与空间解析几何(前篇)
    操作系统(笔试系列)-第七讲设备管理
    win10系统IIS服务器配置
    IIS本地部署Arcgis for js API开发文档
    IIS本地部署Arcgis for js API
    vue中使用mockjs服务器测试项目
    vue动态配置嵌套页面(含iframe嵌套)可实现白天夜间皮肤切换
    如何在vue项目打包去掉console
    Vue之element table 后端排序实现
    D3.tsv与D3.csv加载数据
  • 原文地址:https://www.cnblogs.com/ssjf/p/11806387.html
Copyright © 2011-2022 走看看