Flutter自定義Appbar搜索框效果
本文實(shí)例為大家分享了Flutter自定義Appbar搜索框效果的具體代碼,供大家參考,具體內(nèi)容如下
首先看一下實(shí)現(xiàn)本次實(shí)現(xiàn)的效果。

本來想直接偷懶從flutter pub上找個能用的使用起來,但是看了下發(fā)現(xiàn)都與目前ui效果相差很大,于是乎決定自己實(shí)現(xiàn)一個。整體的話比較簡單,本來也是為了練手而做的。
為了方便處理statusbar的高度適配,于是乎直接依托于Appbar進(jìn)行了實(shí)現(xiàn),這樣就可以不用處理狀態(tài)欄適配了。
class _HotWidgetState extends State<HotWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
titleSpacing: 0, //清除title左右padding,默認(rèn)情況下會有一定的padding距離
toolbarHeight: 44, //將高度定到44,設(shè)計(jì)稿的高度。為了方便適配,
//推薦使用插件flutter_screenutil做屏幕的適配工作
backgroundColor: Colors.white,
elevation: 0,
//由于title本身是接受一個widget,所以可以直接給他一個自定義的widget。
title: SearchAppBar(
hintLabel: "電影/電視劇/影人",
),
),
body: Container(),
);
}
}
flutter中控件定義推薦的是使用組合控件實(shí)現(xiàn),這個是真的很酷,因?yàn)槿f物皆widget,組合起來很方便。
import 'package:flutter/material.dart';
import 'package:flutter_demo_001/ui.theme/color.dart';
import 'package:flutter_demo_001/ui.theme/theme.dart';
import 'package:flutter_demo_001/utils/padding.dart';
class SearchAppBar extends StatefulWidget {
SearchAppBar({Key? key, required this.hintLabel}) : super(key: key);
final String hintLabel;
@override
State<StatefulWidget> createState() {
return SearchAppBarState();
}
}
class SearchAppBarState extends State<SearchAppBar> {
late FocusNode _focusNode;
///默認(rèn)不展示控件
bool _offstage = true;
///監(jiān)聽TextField內(nèi)容變化
final TextEditingController _textEditingController = TextEditingController();
@override
void initState() {
super.initState();
_focusNode = FocusNode();
_textEditingController.addListener(() {
var isVisible = _textEditingController.text.isNotEmpty;
_updateDelIconVisible(isVisible);
});
}
_updateDelIconVisible(bool isVisible) {
setState(() {
_offstage = !isVisible;
});
}
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
height: 30,
child: Row(
children: [
Expanded(
flex: 1,
child: Container(
height: double.infinity,
margin: const EdgeInsets.only(left: 16),
decoration: BoxDecoration(
color: colorF5F6FA, borderRadius: BorderRadius.circular(4)),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
paddingOnly(const EdgeInsets.only(left: 8)),
Image.asset(
"images/home_search.png",
width: 16,
height: 16,
),
paddingOnly(const EdgeInsets.only(left: 8)),
Expanded(
flex: 1,
child: TextField(
controller: _textEditingController,
autofocus: true,
focusNode: _focusNode,
style: TextStyle(fontSize: 14, color: color333),
decoration: boxInputDecoration(widget.hintLabel),
maxLines: 1,
),
),
paddingOnly(const EdgeInsets.only(right: 8)),
Offstage(
offstage: _offstage,
child: GestureDetector(
onTap: () => {_textEditingController.clear()},
child: Image.asset(
"images/home_search_cancel.png",
width: 16,
height: 16,
),
),
),
paddingOnly(const EdgeInsets.only(right: 8)),
],
),
),
),
GestureDetector(
onTap: () {
_focusNode.unfocus();
},
child: Container(
padding: const EdgeInsets.only(left: 16, right: 16),
child: Text("取消",
style: TextStyle(fontSize: 16, color: Color(0xFF3D7DFF))),
),
),
],
),
);
}
@override
void dispose() {
super.dispose();
_focusNode.unfocus();
}
}
最后為了美觀,需要讓狀態(tài)欄也變成透明。
void main() {
//固定屏幕旋轉(zhuǎn)方向
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(const MyApp());
});
//設(shè)置狀態(tài)欄透明
if (Platform.isAndroid) {
SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
statusBarColor: Colors.transparent, //設(shè)置為透明
);
SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法,結(jié)合實(shí)例形式分析了Android針對assets目錄下SQLite數(shù)據(jù)庫文件的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android?ViewPager實(shí)現(xiàn)左右滑動翻頁效果
這篇文章主要為大家詳細(xì)介紹了Android?ViewPager實(shí)現(xiàn)左右滑動翻頁效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android自定義控件實(shí)現(xiàn)九宮格解鎖功能
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)九宮格解鎖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Android中TextView自動適配文本大小的幾種解決方案
在布局中使用的話,注意按照你最大的設(shè)備來設(shè)置字體大小,這樣在小設(shè)備上回自動縮放,下面這篇文章主要給大家介紹了關(guān)于Android中TextView自動適配文本大小的幾種解決方案,需要的朋友可以參考下2022-06-06
Kotlin中常見內(nèi)聯(lián)擴(kuò)展函數(shù)的使用方法教程
在Kotlin中,使用inline修飾符標(biāo)記內(nèi)聯(lián)函數(shù),既會影響到函數(shù)本身, 也影響到傳遞給它的Lambda表達(dá)式,這兩者都會被內(nèi)聯(lián)到調(diào)用處。下面這篇文章主要給大家介紹了關(guān)于Kotlin中常見內(nèi)聯(lián)擴(kuò)展函數(shù)的使用方法,需要的朋友可以參考下。2017-12-12
詳解Android 7.0 Settings 加載選項(xiàng)
本篇文章主要介紹了Android 7.0 Settings 加載選項(xiàng),Android 7.0 Settings頂部多了一個建議選項(xiàng),多了個側(cè)邊欄,操作更加便捷了,有興趣的可以了解一下。2017-02-02
Android提高之SurfaceView的基本用法實(shí)例分析
這篇文章主要介紹了Android提高之SurfaceView的基本用法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程
這篇文章主要介紹了Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01

