網(wǎng)站首頁 編程語言 正文
示例 1 :使用搜索表單創(chuàng)建全屏模式
我們要構(gòu)建的小應(yīng)用程序有一個(gè)應(yīng)用程序欄,右側(cè)有一個(gè)搜索按鈕。按下此按鈕時(shí),將出現(xiàn)一個(gè)全屏模式對(duì)話框。它不會(huì)突然跳出來,而是帶有淡入淡出動(dòng)畫和幻燈片動(dòng)畫(從上到下)。在圓形搜索字段旁邊,有一個(gè)取消按鈕,可用于關(guān)閉模式。在搜索字段下方,我們會(huì)顯示一些搜索歷史記錄(您可以添加其他內(nèi)容,如建議、類別等)。
編碼
我們通過定義一個(gè)擴(kuò)展 ModalRoute 類的名為FullScreenSearchModal的類來創(chuàng)建完整模式。
main.dart中的完整源代碼及說明:
// 大前端之旅
// main.dart
import 'package:flutter/material.dart';
?
void main() => runApp(const MyApp());
?
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
?
@override
Widget build(BuildContext context) {
return MaterialApp(
// remove the debug banner
debugShowCheckedModeBanner: false,
title: '大前端之旅',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const KindaCodeDemo(),
);
}
}
?
// this class defines the full-screen search modal
// by extending the ModalRoute class
class FullScreenSearchModal extends ModalRoute {
@override
Duration get transitionDuration => const Duration(milliseconds: 500);
?
@override
bool get opaque => false;
?
@override
bool get barrierDismissible => false;
?
@override
Color get barrierColor => Colors.black.withOpacity(0.6);
?
@override
String? get barrierLabel => null;
?
@override
bool get maintainState => true;
?
@override
Widget buildPage(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// implement the search field
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: TextField(
autofocus: true,
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(
vertical: 0, horizontal: 20),
filled: true,
fillColor: Colors.grey.shade300,
suffixIcon: const Icon(Icons.close),
hintText: 'Search 大前端之旅',
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(30)),
),
),
),
const SizedBox(
width: 10,
),
// This button is used to close the search modal
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'))
],
),
?
// display other things like search history, suggestions, search results, etc.
const SizedBox(
height: 20,
),
const Padding(
padding: EdgeInsets.only(left: 5),
child: Text('Recently Searched',
style:
TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
),
const ListTile(
title: Text('Flutter tutorials'),
leading: Icon(Icons.search),
trailing: Icon(Icons.close),
),
const ListTile(
title: Text('How to fry a chicken'),
leading: Icon(Icons.search),
trailing: Icon(Icons.close),
),
const ListTile(
title: Text('大前端之旅'),
leading: Icon(Icons.search),
trailing: Icon(Icons.close),
),
const ListTile(
title: Text('Goodbye World'),
leading: Icon(Icons.search),
trailing: Icon(Icons.close),
),
const ListTile(
title: Text('Cute Puppies'),
leading: Icon(Icons.search),
trailing: Icon(Icons.close),
)
],
),
),
),
);
}
?
// animations for the search modal
@override
Widget buildTransitions(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
// add fade animation
return FadeTransition(
opacity: animation,
// add slide animation
child: SlideTransition(
position: Tween<Offset>(
begin: const Offset(0, -1),
end: Offset.zero,
).animate(animation),
child: child,
),
);
}
}
?
// This is the main screen of the application
class KindaCodeDemo extends StatelessWidget {
const KindaCodeDemo({Key? key}) : super(key: key);
?
void _showModal(BuildContext context) {
Navigator.of(context).push(FullScreenSearchModal());
}
?
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('大前端之旅'), actions: [
// this button is used to open the search modal
IconButton(
icon: const Icon(Icons.search),
onPressed: () => _showModal(context),
)
]),
body: Container(),
);
}
}
示例 2:AppBar 內(nèi)的搜索字段(最常見于娛樂應(yīng)用程序)
通常,許多娛樂應(yīng)用程序(包括 Facebook、Youtube、Spotify 等大型應(yīng)用程序)默認(rèn)不顯示搜索字段,而是顯示搜索圖標(biāo)按鈕。按下此按鈕時(shí),將顯示搜索字段。
我們要制作的演示應(yīng)用程序包含 2 個(gè)屏幕(頁面):HomePage和SearchPage。用戶可以通過點(diǎn)擊搜索圖標(biāo)按鈕從主頁移動(dòng)到搜索頁面。搜索字段將通過使用SearchPage 的 AppBar的title參數(shù)來實(shí)現(xiàn)。
讓我們看看它是如何工作的:
編碼
./lib/main.dart中的完整源代碼及說明:
// main.dart
import 'package:flutter/material.dart';
?
void main() {
runApp(const MyApp());
}
?
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
?
@override
Widget build(BuildContext context) {
return MaterialApp(
// Remove the debug banner
debugShowCheckedModeBanner: false,
title: '大前端之旅',
theme: ThemeData(
primarySwatch: Colors.indigo,
),
home: const HomePage());
}
}
?
// Home Page
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
?
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('大前端之旅'),
actions: [
// Navigate to the Search Screen
IconButton(
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => const SearchPage())),
icon: const Icon(Icons.search))
],
),
);
}
}
?
// Search Page
class SearchPage extends StatelessWidget {
const SearchPage({Key? key}) : super(key: key);
?
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// The search area here
title: Container(
width: double.infinity,
height: 40,
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(5)),
child: Center(
child: TextField(
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
suffixIcon: IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
/* Clear the search field */
},
),
hintText: 'Search...',
border: InputBorder.none),
),
),
)),
);
}
}
示例 3:搜索字段和 SliverAppBar
廣告搜索是許多電子商務(wù)應(yīng)用程序最重要的功能之一,因此它們通常以最容易識(shí)別的方式顯示搜索字段,并且從一開始就占用大量空間(亞馬遜、Shopee 等)。
編碼
// main.dart
import 'package:flutter/material.dart';
?
void main() {
runApp(const MyApp());
}
?
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
?
@override
Widget build(BuildContext context) {
return MaterialApp(
// Remove the debug banner
debugShowCheckedModeBanner: false,
title: '大前端之旅',
theme: ThemeData(
primarySwatch: Colors.deepPurple,
),
home: const HomePage());
}
}
?
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
?
@override
State<HomePage> createState() => _HomePageState();
}
?
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
floating: true,
pinned: true,
snap: false,
centerTitle: false,
title: const Text('大前端之旅'),
actions: [
IconButton(
icon: const Icon(Icons.shopping_cart),
onPressed: () {},
),
],
bottom: AppBar(
title: Container(
width: double.infinity,
height: 40,
color: Colors.white,
child: const Center(
child: TextField(
decoration: InputDecoration(
hintText: 'Search for something',
prefixIcon: Icon(Icons.search),
suffixIcon: Icon(Icons.camera_alt)),
),
),
),
),
),
// Other Sliver Widgets
SliverList(
delegate: SliverChildListDelegate([
const SizedBox(
height: 400,
child: Center(
child: Text(
'This is an awesome shopping platform',
),
),
),
Container(
height: 1000,
color: Colors.pink,
),
]),
),
],
),
);
}
}
結(jié)論
您已經(jīng)研究了在 Flutter 中實(shí)現(xiàn)全屏搜索框的端到端示例。這種搜索方式如今非常流行,您可以在許多大型應(yīng)用程序和移動(dòng)網(wǎng)站中注意到它。
原文鏈接:https://juejin.cn/post/7129292610295824421
相關(guān)推薦
- 2022-01-18 報(bào)錯(cuò):Error occurred when invoke the listener‘s inter
- 2023-03-11 Go語言break跳轉(zhuǎn)語句怎么使用_Golang
- 2022-08-04 Python數(shù)據(jù)存儲(chǔ)之XML文檔和字典的互轉(zhuǎn)_python
- 2022-12-09 C#中async和await的深入分析_C#教程
- 2023-05-20 Android內(nèi)存泄漏導(dǎo)致原因深入探究_Android
- 2022-09-18 IOS開發(fā)壓縮后圖片模糊問題解決_IOS
- 2022-11-17 Go語言中常用的基礎(chǔ)方法總結(jié)_Golang
- 2022-09-25 CopyOnWriteArrayList的源碼分析
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支