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'
    ];
  • 相关阅读:
    mysql查询没有主键的表
    pgsql某一个库所有表导出前1000行,CSV格式
    Redis密码设置
    MYSQL PXC
    PXC Percona 5.7 遇到的问题
    ERROR You have enabled the binary log, but you haven't provided the mandatory server-id
    关于js中的比较时遇到的坑
    git 第一次上传本地代码到远程仓库,解决 ! [rejected] master -> master (non-fast-forward)错误
    用css3实现图片的放大缩小
    关于前端使用JavaScript获取base64图片大小的方法
  • 原文地址:https://www.cnblogs.com/ssjf/p/11806387.html
Copyright © 2011-2022 走看看