浮動AppBar中的textField焦點回滾問題解決
完整問題描述
SliverAppBar的floating=true,pinned=false模式中嵌套的TextField,會在獲取焦點時觸發(fā)CustomScrollView滾動到頂部。
問題表現(xiàn)
CustomScrollView和SliverAppBar的介紹和演示,參見官方文檔。
在floating=true和pinned=false 這兩個組合參數(shù)的模式下,SliverAppBar表現(xiàn)為:列表向上滑動時隨列表向上滑動直至消失。
列表在任何位置向下滑動時,會立即從上方滑入直至全部展現(xiàn)。
如果該組件內(nèi)嵌套了TextField,在列表上滑一段距離,再下滑至SliverAppBar及其內(nèi)嵌套的TextField出現(xiàn)時(此時列表尚未滑動到頂端),點擊TextField使其獲取焦點以輸入文字,此時列表會立即滾動至頂。
如圖:
初步探索
開始調(diào)試問題,嘗試了各種參數(shù)組合,只要pinned為true就沒有這個問題,因為SliverAppBar總會展現(xiàn)在最頂端。然后想到了在獲取焦點的同時,將CustomScrollView的physics設(shè)置為 NeverScrollableScrollPhysics(意為禁止?jié)L動),此時并不影響CustomScrollView的滾動位置,然后在輸入完成或失去焦點時,再取消禁止?jié)L動的狀態(tài),即可避免獲取焦點時列表滾動至頂端的問題。解決代碼如下:
class CustomScrollTextFieldPage extends StatefulWidget { const CustomScrollTextFieldPage({Key? key}) : super(key: key); @override State<CustomScrollTextFieldPage> createState() => _CustomScrollTextFieldPageState(); } class _CustomScrollTextFieldPageState extends State<CustomScrollTextFieldPage> { final textController = TextEditingController(); final editableTextController = TextEditingController(); bool focused = false; final focusNode = FocusNode(); final buttonFocus = FocusNode(); final textFocus = FocusNode(); @override void initState() { super.initState(); focusNode.addListener(_onFocus); } @override void dispose() { focusNode.removeListener(_onFocus); super.dispose(); } _onFocus() { setState(() { focused = focusNode.hasFocus; }); } @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( behavior: HitTestBehavior.translucent, onTapDown: () { FocusManager.instance.rootScope.requestFocus(FocusNode()); }, child: CustomScrollView( physics: focused ? const NeverScrollableScrollPhysics() : null, slivers: <Widget>[ SliverAppBar( floating: true, pinned: false, expandedHeight: 250.0, flexibleSpace: FlexibleSpaceBar( expandedTitleScale: 1, title: Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded( child: TextField( focusNode: focusNode, controller: textController, onEditingComplete: () { FocusManager.instance.rootScope.requestFocus(FocusNode()); }, style: const TextStyle(color: Colors.white), decoration: const InputDecoration( border: UnderlineInputBorder( borderSide: BorderSide(color: Colors.white), ), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.white), ), ), ), ), Padding( padding: EdgeInsets.symmetric(horizontal: 16), child: IconButton( visualDensity: VisualDensity(horizontal: 0, vertical: -4), padding: EdgeInsets.zero, onPressed: () { print('btn clicked'); buttonFocus.requestFocus(); }, focusNode: buttonFocus, icon: Icon(Icons.heart_broken), ), ) ], ), ), ), SliverGrid( gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent( maxCrossAxisExtent: 200.0, mainAxisSpacing: 10.0, crossAxisSpacing: 10.0, childAspectRatio: 4.0, ), delegate: SliverChildBuilderDelegate( (BuildContext context, int index) { return Container( alignment: Alignment.center, color: Colors.teal[100 * (index % 9)], child: Text('Grid Item $index'), ); }, childCount: 20, ), ), SliverFixedExtentList( itemExtent: 50.0, delegate: SliverChildBuilderDelegate( (BuildContext context, int index) { return Container( alignment: Alignment.center, color: Colors.lightBlue[100 * (index % 9)], child: Text('List Item $index'), ); }, ), ), ], ), ), ); } }
這個解決方法有點不完美的表現(xiàn),就是輸入完成時不點擊頁面,而是直接點擊收起鍵盤,這時不會觸發(fā)onTapDown也不會觸發(fā) onEditingComplete ,就需要在屏幕再點擊或者滑動時才能重置列表的可滾動狀態(tài)。
更好的解決辦法
經(jīng)過進一步測試,發(fā)現(xiàn)在輸入框內(nèi)的EditableText中對focus進行了監(jiān)聽,在獲取焦點時遞歸調(diào)用了RenderObject的showOnScreen方法,會一直向上追溯Render樹,最終調(diào)用到RenderSliverList中,觸發(fā)了滾動事件。
是不是可以在TextField外包裹一個自定義了RenderBox的組件,把這個showOnScreen調(diào)用給切斷呢?于是翻了下官方的幾個組件寫法,照貓畫虎寫了個自定義的組件
class IgnoreShowOnScreenWidget extends SingleChildRenderObjectWidget { const IgnoreShowOnScreenWidget({ Key? key, Widget? child, this.ignoreShowOnScreen = true, }) : super(key: key, child: child); final bool ignoreShowOnScreen; @override RenderObject createRenderObject(BuildContext context) { return IgnoreShowOnScreenRenderObject( ignoreShowOnScreen: ignoreShowOnScreen, ); } } class IgnoreShowOnScreenRenderObject extends RenderProxyBox { IgnoreShowOnScreenRenderObject({ RenderBox? child, this.ignoreShowOnScreen = true, }); final bool ignoreShowOnScreen; @override void showOnScreen({ RenderObject? descendant, Rect? rect, Duration duration = Duration.zero, Curve curve = Curves.ease, }) { if (!ignoreShowOnScreen) { return super.showOnScreen( descendant: descendant, rect: rect, duration: duration, curve: curve, ); } } }
使用方法
class CustomScrollTextFieldPage extends StatefulWidget { const CustomScrollTextFieldPage({Key? key}) : super(key: key); @override State<CustomScrollTextFieldPage> createState() => _CustomScrollTextFieldPageState(); } class _CustomScrollTextFieldPageState extends State<CustomScrollTextFieldPage> { final textController = TextEditingController(); final focusNode = FocusNode(); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( behavior: HitTestBehavior.translucent, onTap: () { FocusManager.instance.rootScope.requestFocus(FocusNode()); }, child: CustomScrollView( slivers: <Widget>[ SliverAppBar( floating: true, pinned: false, expandedHeight: 250.0, flexibleSpace: FlexibleSpaceBar( expandedTitleScale: 1, title: Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded( child: IgnoreShowOnScreenWidget( child: TextField( focusNode: focusNode , controller: textController , style: const TextStyle(color: Colors.white), decoration: const InputDecoration( border: UnderlineInputBorder( borderSide: BorderSide(color: Colors.white), ), focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.white), ), ), ), ), ), Padding( padding: EdgeInsets.symmetric(horizontal: 16), child: IconButton( visualDensity: VisualDensity(horizontal: 0, vertical: -4), padding: EdgeInsets.zero, onPressed: () { print('btn clicked'); }, icon: Icon(Icons.heart_broken), ), ) ], ), ), ), SliverGrid( gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent( maxCrossAxisExtent: 200.0, mainAxisSpacing: 10.0, crossAxisSpacing: 10.0, childAspectRatio: 4.0, ), delegate: SliverChildBuilderDelegate( (BuildContext context, int index) { return Container( alignment: Alignment.center, color: Colors.teal[100 * (index % 9)], child: Text('Grid Item $index'), ); }, childCount: 20, ), ), SliverFixedExtentList( itemExtent: 50.0, delegate: SliverChildBuilderDelegate( (BuildContext context, int index) { return Container( alignment: Alignment.center, color: Colors.lightBlue[100 * (index % 9)], child: Text('List Item $index'), ); }, ), ), ], ), ), ); } }
初步嘗試,確實可以更方便地解決問題。
效果如圖:
目前還未發(fā)現(xiàn)有什么副作用,如果哪位大神有更好的解決辦法,
以上就是浮動AppBar中的textField焦點回滾問題解決的詳細內(nèi)容,更多關(guān)于AppBar浮動textField焦點回滾的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android 中圖片和按鈕按下狀態(tài)變化實例代碼解析
這篇文章通過實例代碼給大家總結(jié)了android 中圖片和按鈕按下狀態(tài)變化問題,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-06-06深入學(xué)習(xí)Android?ANR?的原理分析及解決辦法
Android系統(tǒng)中,AMS和WMS會檢測App的響應(yīng)時間,如果App在特定時間無法相應(yīng)屏幕觸摸或鍵盤輸入時間,或者特定事件沒有處理完畢,就會出現(xiàn)ANR。本文將帶領(lǐng)大學(xué)深入學(xué)習(xí)一下ANR的原理及解決辦法,感興趣的同學(xué)可以學(xué)習(xí)一下2021-11-11android?studio?項目?:UI設(shè)計高精度實現(xiàn)簡單計算器
這篇文章主要介紹了android?studio?項目?:UI設(shè)計高精度實現(xiàn)簡單計算器,自主完成一個簡單APP的設(shè)計工作,綜合應(yīng)用已經(jīng)學(xué)到的Android?UI設(shè)計技巧,下面來看看實驗實現(xiàn)過程2021-12-12Android實例代碼理解設(shè)計模式SOLID六大原則
程序設(shè)計領(lǐng)域, SOLID (單一功能、開閉原則、里氏替換、接口隔離以及依賴反轉(zhuǎn))是由羅伯特·C·馬丁在21世紀(jì)早期 引入的記憶術(shù)首字母縮略字,指代了面向?qū)ο缶幊毯兔嫦驅(qū)ο笤O(shè)計的基本原則2021-10-10Android?RecyclerChart其它圖表繪制示例詳解
這篇文章主要為大家介紹了Android?RecyclerChart其它圖表繪制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12