Flutter自定義搜索框效果
本文實(shí)例為大家分享了Flutter自定義搜索框效果的具體代碼,供大家參考,具體內(nèi)容如下
效果
實(shí)現(xiàn)方式
import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:keduo/base/baseSize.dart'; import 'package:keduo/utils/icon_utils.dart'; class SearchBarWidget extends StatefulWidget { final ValueChanged<String> onchangeValue; final VoidCallback onEditingComplete; const SearchBarWidget({this.onchangeValue, this.onEditingComplete, Key key}) : super(key: key); @override SearchBarWidgetState createState() => SearchBarWidgetState(); } class SearchBarWidgetState extends State<SearchBarWidget> { ///編輯控制器 TextEditingController _controller; ///是否顯示刪除按鈕 bool _hasDeleteIcon = false; @override void initState() { super.initState(); _controller = TextEditingController(); } Widget buildTextField() { //theme設(shè)置局部主題 return TextField( controller: _controller, textInputAction: TextInputAction.search, keyboardType: TextInputType.text, maxLines: 1, decoration: InputDecoration( //輸入框decoration屬性 contentPadding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 1.0), //設(shè)置搜索圖片 prefixIcon: Padding( padding: EdgeInsets.only(left: 0.0), child: ImageIcon( AssetImage( IconUtils.getIconPath('ic_edit_search'), ), color: Colors.black26, ), ), prefixIconConstraints: BoxConstraints( //設(shè)置搜索圖片左對齊 minWidth: 30, minHeight: 25, ), border: InputBorder.none, //無邊框 hintText: " 請輸入商品名", hintStyle: new TextStyle(fontSize: BaseSize.sp(14), color: Colors.grey), //設(shè)置清除按鈕 suffixIcon: Container( padding: EdgeInsetsDirectional.only( start: 2.0, end: _hasDeleteIcon ? 0.0 : 0, ), child: _hasDeleteIcon ? new InkWell( onTap: (() { setState(() { /// 保證在組件build的第一幀時才去觸發(fā)取消清空內(nèi)容 WidgetsBinding.instance .addPostFrameCallback((_) => _controller.clear()); _hasDeleteIcon = false; }); }), child: Icon( Icons.cancel, size: 18.0, color: Colors.grey, ), ) : new Text(''), ), ), onChanged: (value) { setState(() { if (value.isEmpty) { _hasDeleteIcon = false; } else { _hasDeleteIcon = true; } widget.onchangeValue(_controller.text); }); }, onEditingComplete: () { FocusScope.of(context).requestFocus(FocusNode()); widget.onEditingComplete(); }, style: new TextStyle(fontSize: 14, color: Colors.black), ); } @override Widget build(BuildContext context) { return Container( //背景與圓角 decoration: new BoxDecoration( border: Border.all(color: Colors.black12, width: 1.0), //邊框 color: Colors.black12, borderRadius: new BorderRadius.all(new Radius.circular(BaseSize.dp(18))), ), alignment: Alignment.center, height: BaseSize.dp(36), padding: EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0), child: buildTextField(), ); } @override void dispose() { super.dispose(); _controller.dispose(); } }
使用
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( elevation: 0, brightness: Brightness.light, leading: IconButton( icon: Image.asset(IconUtils.getIconPath('fanhui')), onPressed: () => Navigator.pop(context)), title: SearchBarWidget( onchangeValue: (value) { print(value); }, onEditingComplete: () { print('編輯結(jié)束'); }, ), backgroundColor: Colors.white, ), body: Text(''), backgroundColor: BaseColor.colorFFF5F5F5, ); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android kotlin RecyclerView遍歷json實(shí)現(xiàn)列表數(shù)據(jù)的案例
這篇文章主要介紹了Android kotlin RecyclerView遍歷json實(shí)現(xiàn)列表數(shù)據(jù)的案例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-08-08Android?配合Mat工具監(jiān)聽查找內(nèi)存泄漏的操作方法
這篇文章主要介紹了Android?配合Mat工具監(jiān)聽查找內(nèi)存泄漏問題,使用Android Studio Profiler查看內(nèi)存的操作,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05Android App中實(shí)現(xiàn)向右滑動銷毀功能的要點(diǎn)解析
這篇文章主要介紹了Android應(yīng)用中實(shí)現(xiàn)向右滑動銷毀條目功能的要點(diǎn)解析,有些類似于iOS App中的滑動頁面刪除效果,需要的朋友可以參考下2016-04-04Android編程實(shí)現(xiàn)震動與振鈴的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)震動與振鈴的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)震動與振鈴的Vibrator類及MediaPlayer類相關(guān)使用技巧,需要的朋友可以參考下2018-03-03Android開發(fā)TextvView實(shí)現(xiàn)鏤空字體效果示例代碼
這篇文章主要介紹了Android開發(fā)TextvView實(shí)現(xiàn)鏤空字體效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)的改變
這篇文章主要為大家詳細(xì)介紹了Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)的改變,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Android中制作自定義dialog對話框的實(shí)例分享
這篇文章主要介紹了Android中制作自定義dialog對話框的實(shí)例分享,安卓自帶的Dialog顯然不夠用,因而我們要繼承Dialog類來制作自己的對話框,需要的朋友可以參考下2016-04-04