• flutter 中的搜索条实现


    import 'package:flutter/material.dart';
    import 'package:flutter_app/SearchBarDemo.dart';
    
    
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.green,  //定义主题风格    primarySwatch
          ),
          home:  SearchBarDemo(),
        );
      }
    
    }
    import 'package:flutter/material.dart';
    import 'asset.dart';
    
    class SearchBarDemo extends StatefulWidget {
      _SearchBarDemoState createState() => _SearchBarDemoState();
    }
    
    class _SearchBarDemoState extends State<SearchBarDemo> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: Text('SearchBarDemo'), actions: <Widget>[
          IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                showSearch(context: context, delegate: searchBarDelegate());
              }
              // showSearch(context:context,delegate: searchBarDelegate()),
              ),
        ]));
      }
    }
    
    class searchBarDelegate extends SearchDelegate<String> {
      @override
      List<Widget> buildActions(BuildContext context) {
        return [
          IconButton(
            icon: Icon(Icons.clear),
            onPressed: () => query = "",
          )
        ];
      }
    
      @override
      Widget buildLeading(BuildContext context) {
        return IconButton(
            icon: AnimatedIcon(
                icon: AnimatedIcons.menu_arrow, progress: transitionAnimation),
            onPressed: () => close(context, null));
      }
    
      @override
      Widget buildResults(BuildContext context) {
        return Container(
           100.0,
          height: 100.0,
          child: Card(
            color: Colors.redAccent,
            child: Center(
              child: Text(query),
            ),
          ),
        );
      }
    
      @override
      Widget buildSuggestions(BuildContext context) {
        final suggestionList = query.isEmpty
            ? recentSuggest
            : searchList.where((input) => input.startsWith(query)).toList();
        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 = [
      "上衣",
      "华为",
      "电视",
      "新闻"
    ];
    
    const recentSuggest = [
      "推荐-1",
      "推荐-2"
    ];

    效果:

  • 相关阅读:
    如何突破单库性能瓶颈?
    高性能数据库表该如何设计?
    高性能索引该如何设计?(下)
    高性能索引该如何设计?(上)
    MySQL体系结构与存储引擎
    动态ViewPager导航页面
    ViewPager图片轮转带点的
    手动图片横向轮播
    安卓布局中下拉列表框的实现
    安卓中adapter的应用
  • 原文地址:https://www.cnblogs.com/loaderman/p/11350306.html
Copyright © 2020-2023  润新知