Flutter动画搜索栏插件search_bar_animated的使用
Flutter动画搜索栏插件search_bar_animated的使用
动画搜索栏插件search_bar_animated
search_bar_animated
是一个具有动画效果的搜索栏插件。它支持完全自定义,并且可以在 Flutter 应用中轻松实现搜索功能。
使用示例
首先,在你的 pubspec.yaml
文件中添加依赖:
dependencies:
search_bar_animated: "^latest_version"
然后在 Dart 代码中导入该包:
import 'package:search_bar_animated/search_bar_animated.dart';
以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 search_bar_animated
插件:
import 'package:flutter/material.dart';
import 'package:search_bar_animated/search_bar_animated.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
[@override](/user/override)
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<String> list = [
"Flutter",
"Twitter",
"Facebook",
"Google",
"Instagram",
"Youtube",
];
TextEditingController textController = TextEditingController();
String value = "";
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: AnimatedSearchBar(
backgroundColor: Colors.transparent,
buttonColor: Colors.white,
width: MediaQuery.of(context).size.width,
submitButtonColor: Colors.white,
textStyle: const TextStyle(color: Colors.white),
buttonIcon: const Icon(
Icons.search,
),
duration: const Duration(milliseconds: 700),
submitIcon: const Icon(Icons.arrow_back_rounded),
animationAlignment: AnimationAlignment.right,
onSubmit: () {
setState(() {
value = textController.text;
});
},
searchList: list,
searchQueryBuilder: (query, list) => list.where((item) {
return item!.toString().toLowerCase().contains(query.toLowerCase());
}).toList(),
textController: textController,
overlaySearchListItemBuilder: (dynamic item) => Container(
padding: const EdgeInsets.all(8),
child: Text(
item,
style: const TextStyle(fontSize: 15, color: Colors.black),
),
),
onItemSelected: (dynamic item) {
textController.value = textController.value.copyWith(
text: item.toString(),
);
},
overlaySearchListHeight: 100,
),
),
body: Container(
color: Colors.blueGrey,
child: Column(
children: [
const SizedBox(
height: 200,
),
AnimatedSearchBar(
shadow: const [
BoxShadow(
color: Colors.black38, blurRadius: 6, offset: Offset(0, 6))
],
backgroundColor: Colors.white,
buttonColor: Colors.blue,
width: MediaQuery.of(context).size.width * 0.7,
submitButtonColor: Colors.blue,
textStyle: const TextStyle(color: Colors.blue),
buttonIcon: const Icon(
Icons.search,
),
duration: const Duration(milliseconds: 1000),
submitIcon: const Icon(Icons.send),
animationAlignment: AnimationAlignment.left,
onSubmit: () {
setState(() {
value = textController.text;
});
},
searchList: list,
searchQueryBuilder: (query, list) => list.where((item) {
return item!
.toString()
.toLowerCase()
.contains(query.toLowerCase());
}).toList(),
textController: textController,
overlaySearchListItemBuilder: (dynamic item) => Container(
padding: const EdgeInsets.all(8),
child: Text(
item,
style: const TextStyle(fontSize: 15, color: Colors.black),
),
),
onItemSelected: (dynamic item) {
textController.value = textController.value.copyWith(
text: item.toString(),
);
},
overlaySearchListHeight: 100,
),
const SizedBox(
height: 50,
),
Center(
child: Text(
value,
style: const TextStyle(fontSize: 30, color: Colors.white),
),
),
],
),
),
);
}
}
更多关于Flutter动画搜索栏插件search_bar_animated的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter动画搜索栏插件search_bar_animated的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用search_bar_animated
插件来实现动画搜索栏的示例代码。这个插件提供了一个具有动画效果的搜索栏,非常适合用于移动应用。
首先,确保你的Flutter项目已经添加了对search_bar_animated
插件的依赖。在你的pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter:
sdk: flutter
search_bar_animated: ^x.y.z # 请替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,在你的Dart文件中(例如main.dart
),你可以按照以下方式使用SearchBarAnimated
:
import 'package:flutter/material.dart';
import 'package:search_bar_animated/search_bar_animated.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter SearchBar Animated Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SearchBarDemo(),
);
}
}
class SearchBarDemo extends StatefulWidget {
@override
_SearchBarDemoState createState() => _SearchBarDemoState();
}
class _SearchBarDemoState extends State<SearchBarDemo> {
final TextEditingController _controller = TextEditingController();
String _searchQuery = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Search Bar Animated Demo'),
),
body: Column(
children: <Widget>[
SearchBarAnimated<String>(
searchBarController: _controller,
suggestions: ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'],
onSearch: (query) {
setState(() {
_searchQuery = query;
});
// 这里可以添加你的搜索逻辑,例如从API获取数据
},
onCancel: () {
_controller.clear();
setState(() {
_searchQuery = '';
});
},
placeholder: 'Search...',
style: TextStyle(color: Colors.black),
leading: Icon(Icons.search),
trailing: Icon(Icons.cancel),
physics: BouncingScrollPhysics(),
elevation: 4.0,
borderRadius: BorderRadius.circular(10),
),
SizedBox(height: 20),
if (_searchQuery.isNotEmpty)
Text(
'Search Results for: $_searchQuery',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
// 显示搜索结果的地方,这里只是简单示例
Expanded(
child: ListView.builder(
itemCount: _filteredSuggestions().length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_filteredSuggestions()[index]),
);
},
),
),
],
),
);
}
List<String> _filteredSuggestions() {
return _searchQuery.isEmpty
? []
: _controller.text.toLowerCase().split(' ')
.map((queryWord) =>
_suggestions.where((suggestion) =>
suggestion.toLowerCase().contains(queryWord)).toList())
.expand((list) => list)
.toSet()
.toList();
}
final List<String> _suggestions = [
'Apple',
'Banana',
'Cherry',
'Date',
'Elderberry',
];
}
代码解释:
- 依赖添加:在
pubspec.yaml
文件中添加search_bar_animated
依赖。 - 导入包:在Dart文件中导入
search_bar_animated
包。 - 搜索栏配置:
searchBarController
:用于控制搜索栏的文本输入。suggestions
:提供初始的建议列表。onSearch
:当用户在搜索栏中输入文本时触发,用于更新搜索查询和可能的搜索结果。onCancel
:当用户点击取消按钮时触发,用于清空搜索栏和重置状态。placeholder
:搜索栏的占位符文本。style
:搜索栏文本的样式。leading
和trailing
:搜索栏前后的图标。physics
、elevation
和borderRadius
:用于自定义搜索栏的外观。
- 搜索结果展示:根据用户的搜索查询过滤建议列表,并在UI中显示过滤后的结果。
这个示例展示了如何使用search_bar_animated
插件创建一个具有动画效果的搜索栏,并处理用户的搜索输入和取消操作。你可以根据实际需求进一步自定义和扩展这个示例。