Achieve Flutter search interface, SearchDelegate use flutter in the search bar to achieve

https://www.jianshu.com/p/63aca25a463c

https://www.cnblogs.com/loaderman/p/11350306.html

Use realize Flutter search interface, SearchDelegate of

1. Use the system search interface

In the previous study in their own implements a search interface, which you customize the search bar, it will achieve results, later found to have an existing control is available in Flutter, that is SearchDelegate<T>, calls showSearch(context: context, delegate: searchBarDelegate())to achieve.

2. DefinitionsSearchDelegate

class searchBarDelegate extends SearchDelegate<String> { @override List<Widget> buildActions(BuildContext context) { return null; } @override Widget buildLeading(BuildContext context) { return null; } @override Widget buildResults(BuildContext context) { return null; } @override Widget buildSuggestions(BuildContext context) { return null; } @override ThemeData appBarTheme(BuildContext context) { // TODO: implement appBarTheme return super.appBarTheme(context); } } 

Inherited SearchDelegate<String>need to rewrite some way after, here given Stringtype refers to the search querytype is String.

  • List<Widget> buildActions(BuildContext context): This method returns a list of controls, displays an icon for the button to the right of the search box, set here as a clear button, and displays search suggestions in the search content is empty, using the showSuggestions(context)method:
@override
List<Widget> buildActions(BuildContext context) { return [ IconButton( icon: Icon(Icons.clear), onPressed: () { query = ""; showSuggestions(context); }, ), ]; } 
  • showSuggestions(context): This method displays the contents of the search suggestions, that is Widget buildSuggestions(BuildContext context)calling the method;

  • Widget buildLeading(BuildContext context): This method returns a control button is displayed as the search box on the left, is generally set to return, this returns a return button having a dynamic effect:

@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); } }, ); } 
  • Widget buildSuggestions(BuildContext context): This method returns a control demonstrating that the proposed content for the search of the content area.
  • Widget buildSuggestions(BuildContext context): This method returns a control to display the contents of the search area for the search results content.
  • ThemeData appBarTheme(BuildContext context): This method returns a theme, which is the subject of style definitions can customize the search interface:
///  * [AppBar.backgroundColor], which is set to [ThemeData.primaryColor].
///  * [AppBar.iconTheme], which is set to [ThemeData.primaryIconTheme].
///  * [AppBar.textTheme], which is set to [ThemeData.primaryTextTheme].
///  * [AppBar.brightness], which is set to [ThemeData.primaryColorBrightness].
ThemeData appBarTheme(BuildContext context) { assert(context != null); final ThemeData theme = Theme.of(context); assert(theme != null); return theme.copyWith( primaryColor: Colors.white, primaryIconTheme: theme.primaryIconTheme.copyWith(color: Colors.grey), primaryColorBrightness: Brightness.light, primaryTextTheme: theme.textTheme, ); } 

You can see the default theme of white light gray word, they can modify their own;



 
Suggested searches
 

 
Search results display

3. Call showSearch (context: context, delegate: searchBarDelegate ()) method to jump to the search interface

showSearch(context: context, delegate: searchBarDelegate()): Jump to the search interface.



Author: I love white white love wide open
link: https: //www.jianshu.com/p/63aca25a463c
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

 

 

 

flutter in the search bar to achieve

Copy the code
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(),
    );
  }

}
Copy the code
Copy the code
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(
      width: 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))
                  ])),
            ));
  }
}
Copy the code
Copy the code
searchList = const [ 
  "coat", 
  "Huawei", 
  "TV", 
  "news" 
]; 

const recentSuggest = [ 
  "Recommended -1", 
  "recommended 2" 
];
Copy the code

effect:

 

Learn together, grow together!

 

Copy the code
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(),
    );
  }

}
Copy the code
Copy the code
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(
      width: 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))
                  ])),
            ));
  }
}
Copy the code
Copy the code
searchList = const [ 
  "coat", 
  "Huawei", 
  "TV", 
  "news" 
]; 

const recentSuggest = [ 
  "Recommended -1", 
  "recommended 2" 
];
Copy the code

effect:

 

Guess you like

Origin www.cnblogs.com/sundaysme/p/12580542.html