Flutter自定義Appbar搜索框效果
本文實(shí)例為大家分享了Flutter自定義Appbar搜索框效果的具體代碼,供大家參考,具體內(nèi)容如下
首先看一下實(shí)現(xiàn)本次實(shí)現(xiàn)的效果。
本來(lái)想直接偷懶從flutter pub上找個(gè)能用的使用起來(lái),但是看了下發(fā)現(xiàn)都與目前ui效果相差很大,于是乎決定自己實(shí)現(xiàn)一個(gè)。整體的話比較簡(jiǎn)單,本來(lái)也是為了練手而做的。
為了方便處理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)情況下會(huì)有一定的padding距離 toolbarHeight: 44, //將高度定到44,設(shè)計(jì)稿的高度。為了方便適配, //推薦使用插件flutter_screenutil做屏幕的適配工作 backgroundColor: Colors.white, elevation: 0, //由于title本身是接受一個(gè)widget,所以可以直接給他一個(gè)自定義的widget。 title: SearchAppBar( hintLabel: "電影/電視劇/影人", ), ), body: Container(), ); } }
flutter中控件定義推薦的是使用組合控件實(shí)現(xiàn),這個(gè)是真的很酷,因?yàn)槿f(wàn)物皆widget,組合起來(lái)很方便。
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)聽(tīng)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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?Java?try?catch?失效問(wèn)題及解決
這篇文章主要介紹了Android?Java?try?catch?失效問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11android中實(shí)現(xiàn)OkHttp下載文件并帶進(jìn)度條
本篇文章主要介紹了android中實(shí)現(xiàn)OkHttp下載文件并帶進(jìn)度條,OkHttp是比較火的網(wǎng)絡(luò)框架,它支持同步與異步請(qǐng)求,支持緩存,可以攔截,更方便下載大文件與上傳文件的操作,有興趣的可以了解一下2017-07-07viewPager+fragment刷新緩存fragment的方法
這篇文章主要介紹了viewPager+fragment刷新緩存fragment的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實(shí)例
這篇文章主要介紹了Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實(shí)例,本文用完整的代碼實(shí)例講解了在Android中讀取網(wǎng)絡(luò)中Json數(shù)據(jù)的方法,需要的朋友可以參考下2014-10-10Android權(quán)限操作之uses-permission詳解
這篇文章主要介紹了Android權(quán)限操作之uses-permission,較為詳細(xì)的分析了uses-permission常見(jiàn)權(quán)限操作類(lèi)型與功能,需要的朋友可以參考下2016-10-10Android實(shí)現(xiàn)標(biāo)題上顯示隱藏進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)標(biāo)題上顯示隱藏進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android入門(mén)之ListView應(yīng)用解析(一)
這篇文章主要介紹了Android入門(mén)之ListView應(yīng)用,簡(jiǎn)單說(shuō)明了ListView的實(shí)現(xiàn),需要的朋友可以參考下2014-08-08