Flutter搜索建议动画插件searchbar_animated_suggestions的使用

Flutter搜索建议动画插件searchbar_animated_suggestions的使用

Saas Test

Saas Test 是一个 Flutter 包,它提供了一个可自定义的搜索字段小部件(SaasField),并带有动画化的建议。它还包括一个演示小部件 SaasTest,展示了其用法。

安装

要使用此包,请在 pubspec.yaml 文件中添加 saas_test 作为依赖项:

dependencies:
  saas_test:
    git:
      url: git://github.com/your_username/saas_test.git

使用

在您的 Dart 文件中导入包:

import 'package:flutter/material.dart';
import 'package:saas_test/saas_test.dart';

在您的小部件树中使用 SaasField 小部件:

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Saas Test Demo'),
      ),
      body: SaasTest(), // 使用 SaasTest 小部件
    );
  }
}

SaasTest 小部件

SaasTest 小部件演示了 SaasField 的用法:

  • 显示一个带有动画化建议的搜索栏(SaasField)。
  • 展示一个食品项目的样本列表以供搜索建议。
class SaasTest extends StatefulWidget {
  const SaasTest({Key? key}) : super(key: key);

  [@override](/user/override)
  _SaasTestState createState() => _SaasTestState();
}

class _SaasTestState extends State<SaasTest> {
  void toast(String message) {
    ScaffoldMessenger.of(context).clearSnackBars();
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      content: Text(message),
    ));
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    List<dynamic> items = [
      "Chicken curry 🐔",
      "Egg curry 🥚",
      "Fish curry 🐟",
      "Paneer curry 🧀",
      "Heavy diet 🍔",
      "Mixed protein chat 🍲",
    ];

    return Scaffold(
      appBar: _appBar(),
      body: Column(
        children: [
          const SizedBox(height: 20),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: SaasField(
              height: 50,
              items: items,
              showDivider: true,
              onSubmitted: (value) => toast(value),
              padding: const EdgeInsets.symmetric(vertical: 8),
              prefixIcon: const Padding(
                padding: EdgeInsets.all(8.0),
                child: Icon(Icons.search, color: Colors.grey),
              ),
              actions: [
                InkWell(
                  onTap: () {},
                  child: const Padding(
                    padding: EdgeInsets.all(8.0),
                    child: Icon(Icons.filter_alt, color: Colors.grey),
                  ),
                ),
              ],
            ),
          ),
          const Center(
            child: Text(
              'Hello, Saas!',
            ),
          ),
        ],
      ),
    );
  }

  AppBar _appBar() {
    return AppBar(
      title: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            'Saas Test',
            style: TextStyle(
              fontWeight: FontWeight.bold,
            ),
          ),
          Text(
            'Here you can taste the Mohali best food',
            style: TextStyle(
              color: Colors.grey,
              fontWeight: FontWeight.bold,
              fontSize: 16,
            ),
          ),
        ],
      ),
    );
  }
}

特性

  • 搜索栏(SaasField):交互式的搜索字段,具有自动动画化的建议。
  • 可定制性:轻松定制图标、填充和操作等 UI 元素。
  • 用途:适用于各种应用程序中显示动态搜索建议。

许可证

该项目根据 MIT 许可证授权 - 有关详细信息,请参阅 LICENSE 文件。


示例代码

以下是一个完整的示例代码,展示如何使用 searchbar_animated_suggestions 插件:

// example/lib/main.dart
import 'package:flutter/material.dart';
import './saas/saas.dart'; // 导入 SaasField 和相关组件

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // 应用程序根小部件
  [@override](/user/override)
  Widget build(BuildContext context) {
    Color primaryColor = const Color(0xFF6200EE); // 自定义主题颜色
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: MaterialColor(
          primaryColor.value,
          <int, Color>{
            50: primaryColor.withOpacity(0.1),
            100: primaryColor.withOpacity(0.2),
            200: primaryColor.withOpacity(0.3),
            300: primaryColor.withOpacity(0.4),
            400: primaryColor.withOpacity(0.5),
            500: primaryColor.withOpacity(0.6),
            600: primaryColor.withOpacity(0.7),
            700: primaryColor.withOpacity(0.8),
            800: primaryColor.withOpacity(0.9),
            900: primaryColor.withOpacity(1),
          },
        ),
        primaryColor: primaryColor,
        visualDensity: VisualDensity.adaptivePlatformDensity,
        appBarTheme: AppBarTheme(
          backgroundColor: primaryColor,
          foregroundColor: Colors.white,
          titleTextStyle: const TextStyle(
            color: Colors.white,
            fontSize: 20,
            fontWeight: FontWeight.bold,
          ),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10),
          ),
        ),
      ),
      home: const SaasTest(), // 主页面
    );
  }
}

更多关于Flutter搜索建议动画插件searchbar_animated_suggestions的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter搜索建议动画插件searchbar_animated_suggestions的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


searchbar_animated_suggestions 是一个 Flutter 插件,用于创建一个带有动画搜索建议的搜索栏。它可以帮助你在用户输入时动态显示搜索建议,并且支持自定义样式和动画效果。

以下是如何使用 searchbar_animated_suggestions 插件的步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 searchbar_animated_suggestions 依赖:

dependencies:
  flutter:
    sdk: flutter
  searchbar_animated_suggestions: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入包

在需要使用 searchbar_animated_suggestions 的 Dart 文件中导入包:

import 'package:searchbar_animated_suggestions/searchbar_animated_suggestions.dart';

3. 使用 SearchBarAnimatedSuggestions

你可以使用 SearchBarAnimatedSuggestions 组件来创建一个带有动画搜索建议的搜索栏。以下是一个简单的示例:

class SearchPage extends StatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  List<String> suggestions = [
    "Apple",
    "Banana",
    "Orange",
    "Pineapple",
    "Strawberry",
    "Watermelon",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Search Bar with Suggestions'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: SearchBarAnimatedSuggestions(
          suggestions: suggestions,
          onSuggestionSelected: (String suggestion) {
            print("Selected: $suggestion");
          },
          onSearchChanged: (String query) {
            print("Query: $query");
          },
          decoration: InputDecoration(
            hintText: 'Search...',
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10.0),
            ),
          ),
        ),
      ),
    );
  }
}

4. 参数说明

  • suggestions: 这是一个包含所有搜索建议的列表。当用户输入时,插件会根据输入内容过滤这些建议并显示。
  • onSuggestionSelected: 当用户选择一个建议时,会触发此回调函数。
  • onSearchChanged: 当用户输入内容时,会触发此回调函数。
  • decoration: 用于自定义搜索栏的样式,例如提示文本、边框等。

5. 自定义样式和动画

你可以通过调整 SearchBarAnimatedSuggestions 的参数来自定义搜索栏的样式和动画效果。例如:

SearchBarAnimatedSuggestions(
  suggestions: suggestions,
  onSuggestionSelected: (String suggestion) {
    print("Selected: $suggestion");
  },
  onSearchChanged: (String query) {
    print("Query: $query");
  },
  decoration: InputDecoration(
    hintText: 'Search...',
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(10.0),
    ),
  ),
  suggestionListDecoration: BoxDecoration(
    color: Colors.white,
    borderRadius: BorderRadius.circular(10.0),
    boxShadow: [
      BoxShadow(
        color: Colors.black12,
        blurRadius: 5.0,
      ),
    ],
  ),
  suggestionItemBuilder: (context, suggestion) {
    return ListTile(
      title: Text(suggestion),
      onTap: () {
        print("Tapped: $suggestion");
      },
    );
  },
)
回到顶部