简体   繁体   English

flutter 中的搜索栏实现

[英]Search Bar implementation in flutter

Is here any way to implement a search bar using a TextField to accept the query string and using the search icon on being pressed giving the desired results.这里有什么方法可以使用 TextField 来实现搜索栏以接受查询字符串并在按下时使用搜索图标给出所需的结果。

dependencies:
  flutter_search_bar: ^2.1.0 

then然后

import 'package:flutter/material.dart';
    import 'package:flutter_search_bar/flutter_search_bar.dart';
    
    void main() {
      runApp(new SearchBarDemoApp());
    }
    
    class SearchBarDemoApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
            title: 'Search Bar Demo',
            theme: new ThemeData(primarySwatch: Colors.blue),
            home: new SearchBarDemoHome());
      }
    }
    
    class SearchBarDemoHome extends StatefulWidget {
      @override
      _SearchBarDemoHomeState createState() => new _SearchBarDemoHomeState();
    }
    
    class _SearchBarDemoHomeState extends State<SearchBarDemoHome> {
      SearchBar searchBar;
      final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
    
      AppBar buildAppBar(BuildContext context) {
        return new AppBar(
            title: new Text('Search Bar Demo'),
            actions: [searchBar.getSearchAction(context)]);
      }
    
      void onSubmitted(String value) {
        setState(() => _scaffoldKey.currentState
            .showSnackBar(new SnackBar(content: new Text('You wrote $value!'))));
      }
    
      _SearchBarDemoHomeState() {
        searchBar = new SearchBar(
            inBar: false,
            buildDefaultAppBar: buildAppBar,
            setState: setState,
            onSubmitted: onSubmitted,
            onCleared: () {
              print("cleared");
            },
            onClosed: () {
              print("closed");
            });
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: searchBar.build(context),
          key: _scaffoldKey,
          body: new Center(
              child: new Text("Don't look at me! Press the search button!")),
        );
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM