搜索插件、代码仓与 APP 分类

app_bar_with_search_switch 封面
插件详情v1.8.6

app_bar_with_search_switch

一个可将 AppBar 切换为搜索框的扩展。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
582
喜欢数
59
Pub 分数
150

安装插件

flutter pub add app_bar_with_search_switch

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 包,扩展了 AppBar 的功能,可无缝在标准应用栏和搜索框之间切换。它提供了一种简洁、交互式的方式,在应用栏内切换导航模式和搜索模式,从而提升频繁进行搜索操作的应用的用户体验。该包轻量且易于集成到现有 UI 中。支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 在移动和网页应用中实现动态搜索栏
  • 通过按需搜索创建直观的导航
  • 提升内容密集型应用中的用户体验
  • 使用最少的UI更改构建响应式界面
  • 从顶部应用栏快速访问搜索

主要特点

  • 一键在应用栏和搜索模式之间切换
  • 可自定义的搜索字段外观
  • 支持平台特定行为
  • 轻量且易于集成
  • 内置动画,实现流畅过渡

适合对象

  • 开发具有频繁搜索需求的应用程序的开发者
  • 寻求优雅搜索集成的UI设计师
  • 跨平台应用程序开发者,针对多个操作系统
  • 需要极简但功能齐全的搜索界面的应用程序

使用建议

  • 需要在包外部手动处理搜索逻辑
  • 可能需要为自定义主题添加额外的样式
  • 仅限应用栏级别搜索功能
  • 不适合复杂的搜索工作流
  • 依赖 Flutter 的 Material 或 Cupertino 组件以获得完全兼容性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

AppBar with search switch

Content

Intro

An AppBar that can switch into search field.

The AppBarWithSearchSwitch is a replacement class for AppBar, essentially, it returns two different app bars based on whether search is active.

Note: version 1.5+ is lightweight version of this package, version 2.0+ uses SpeechToText package for speech recognition.

This is complete rewrite of flutter_search_bar with support of these features:

Features

Quick overview

Use appBarBuilder parameter to build default AppBar with:

The appBarBuilder is the only required parameter, all other parameters are optional!

Use one of these callbacks to get text from TextField:

Also, there are callbacks for:

This widget support almost all properties of AppBar, but:

  • leading and title properties are now expect - Widget Function(context)?:

    • this is made in order to access AppBarWithSearchSwitch.of(context) methods in them,
    • don't change them unless it necessary and use templates if you need to change these properties.
  • preferredSize here is a method, you should set it via toolbarWidth and toolbarHeight.

Here is a list of all other new properties(without mentioned above) with their default values:

Examples

Online example here: https://alexqwesa.github.io/app_bar_with_search_switch/.

Full example of Stateful widget is here: https://pub.dev/packages/app_bar_with_search_switch/example.

Full example of Stateless widget is here: (github).

Full example of Stateless widget with 10000 elements searched in place and with search button outside of app bar is here: (github).

Full example of Stateless widget there android back button will close search is here: (github).

And the fragment of example code is here:

  //...
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //
      // *** The Widget AppBarWithSearchSwitch
      //
      appBar: AppBarWithSearchSwitch(
        onChanged: (text) {
          // update you provider here
          // searchText.value = text;
        }, // onSubmitted: (text) => searchText.value = text,
        appBarBuilder: (context) {
          return AppBar(
            title: Text('Example '),
            actions: [
              AppBarSearchButton(),
              // or
              // IconButton(onPressed: AppBarWithSearchSwitch.of(context)?startSearch, icon: Icon(Icons.search)),
            ],
          );
        },
      ),
      // search in body by any way you want, example:
      body: AppBarOnEditListener(builder: (context) { return /* your code here */ ;} ),
    );
  }

Screenshots

TODO

  • [x] Add speech to text support - done in version 2.0+,
  • [x] Animation for Search app bar activation - done in versions 1.5+
  • [ ] don't use shared default ValueNotifier and TextEditingController ?

FAQ

How to activate search field (isSearchMode=true) of AppBarWithSearchSwitch from somewhere far away?

  • If it is inside the same Scaffold or its children, then:

    1. use AppBarWithSearchFinder,
    2. call AppBarWithSearchFinder.of(context)?.triggerSearch() inside.
  • If it is outside of Scaffold, use customIsSearchModeNotifier,

    1. Initialise variable of type ValueNotifier somewhere up in the widget tree,
    2. Set customIsSearchModeNotifier property of AppBarWithSearchSwitch with this variable,
    3. Set value of this ValueNotifier to true to show Search AppBar,
    4. (Note: currently, if you stop search via this variable(by setting it false), clearOnClose will not work, and callBack onClose will not be called), so use GlobalKey if you need them.

How to make android back button close search? (instead of going to previous screen or exit app)

  1. Initialise variable searchText and isSearchMode of type ValueNotifier somewhere up in the widget tree,
  2. Assign these variables to customIsSearchModeNotifier, customTextNotifier,
  3. Wrap WillPopScope widget over Scaffold, and define parameter onWillPop as in example below:
... // inside a widget
  final isSearchMode = ValueNotifier<bool>(false);
  final searchText = ValueNotifier<String>(''); 

  @override
  Widget build(BuildContext context) {

    return WillPopScope(
      onWillPop: () async { // android back button handler
        if (searchText.value != '') {
          isSearchMode.value = false;
          searchText.value = ''; 
          return false;
        }
        return true;
      },
      child: Scaffold(
        //
        // *** The Widget AppBarWithSearchSwitch
        //
        appBar: AppBarWithSearchSwitch(
          customIsSearchModeNotifier: isSearchMode,
          customTextNotifier: searchText,
          appBarBuilder: (context) {
            return AppBar(
... // you code here

How to add autocompletion to search field?

... // inside a widget
  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBarWithSearchSwitch(
          title:  (context) {
                return Autocomplete<String>(
                  optionsBuilder: (TextEditingValue textEditingValue) {
                    // your code here
                  },
                ); 
          }
          appBarBuilder: (context) {
            return AppBar(
... // you code here

Can it be used with old flutter sdk versions?

The release 1.3.5 is a special release with support of old flutter sdk, it tested with flutter 2.10.0

Known issues

所属分类

源码仓库

使用此插件的项目