Flutter實現(xiàn)固定header底部滑動頁效果示例
正文
實現(xiàn)的效果是這樣的:
剛開始的時候,是在dev上找了兩個輪子,簡單測了下,都不太滿意,滑動事件處理的比較粗糙,總有bug。就在想著,要不要拿源碼改一版的時候,讓我無意間看到了這個帖子
里面的想法,大開眼界,是通過 DraggableScrollableSheet 和 IgnorePointer 來完美實現(xiàn)上面的效果。
實現(xiàn)
這是 DraggableScrollableSheet 的代碼,
DraggableScrollableSheet( maxChildSize: 0.8, minChildSize: 0.25, // 注意都是占父組件的比例 initialChildSize: 0.25, expand: true, builder: (BuildContext context, ScrollController controller) { return Stack(); // body列表和header欄都在stack內(nèi) }, )
這是 body 列表和 header,這里的 body 是個 list,
Stack( children: [ Container( color: Colors.blue, child: Body( // ListView.separated controller: controller, paddingTop: headerHeight, // 防止壓蓋 ), ), const IgnorePointer( // 這里不接收事件,所以拖動 header 也能夠滑動頁面 child: Header( // Container[Center[Text]] height: headerHeight, ), ), ], )
但如果我們想在 header 內(nèi)加點擊事件呢?那在 Stack header 上層再加 widget 就好了。
代碼就這點,我放在了 gitHub 上,感興趣的可以看下。
2022.8.23 補充:
這是在上面功能基礎(chǔ)上的一個小擴展,即當(dāng)滑動距離超過一半則自動滾至頂部,反之回到底部,來看下效果:
思路也很簡單,首先我要知道當(dāng)前滾動的距離或其占比,DraggableScrollableController 提供了這個能力:
void _draggableScrollListener() { // [_currStale] 記錄下當(dāng)前的占比 // [_controller.size] 即占比, 范圍[minChildSize,maxChildSize] // [_controller.pixels] 即距離 if (_currStale != _controller.size) { _currStale = _controller.size; } debugPrint('[listener] size: ${_controller.size}' ', pixels : ${_controller.pixels}'); }
其次要知道用戶何時停止了滾動,我們可以使用 NotificationListener 來監(jiān)聽 DraggableScrollableSheet 的滾動狀態(tài):
NotificationListener<ScrollNotification>( onNotification: (ScrollNotification notification) { ... return false; }, child: DraggableScrollableSheet(...),
之后在用戶停止?jié)L動的時候,我們判斷當(dāng)前距離,并根據(jù)結(jié)果讓 DraggableScrollableSheet 自動滾動到頂部或底部。
onNotification: (ScrollNotification notification) { if (_animation) { // 動畫中,不處理狀態(tài) return false; } if (notification is ScrollStartNotification) { debugPrint('start scroll'); } else if (notification is ScrollEndNotification) { debugPrint('stop scroll'); // 通過 [_controller.animateTo] 方法滾動 _scrollAnimation(); } return false;
在 _scrollAnimation 內(nèi)就是滾動的方法了,這里要注意的是,不能直接使用 await Feature,我測試在頻繁不同方向滑動時,可能會導(dǎo)致方法被掛起。在這直接 dedelayed(duration: xx) 即可:
Future<void> _scrollAnimation() async { if (_animation) { return; } _animation = true; //debugPrint('async start'); final int duration; // `await`ing the returned Feature(of [animateTo]) may cause the method to hang // So, Start a timer to set [_animation]. if (_currStale >= (_maxScale + _minScale) * 0.5) { duration = (_duration * ((_maxScale - _currStale) / (_maxScale - _minScale))) .toInt(); if (duration == 0) { _animation = false; return; } else { // [duration] control speed, Avoid situations where it's equal to 0 _animationTo(_maxScale, duration); } } else { duration = (_duration * ((_currStale - _minScale) / (_maxScale - _minScale))) .toInt(); if (duration == 0) { _animation = false; return; } else { _animationTo(_minScale, duration); } } Future.delayed( Duration(milliseconds: duration), ).then((value) => { //debugPrint('async stop'), _animation = false, }); }
其中 _animationTo
是實際控制控件滾動的方法:
void _animationTo(double scale, int duration) { _controller.animateTo( scale, duration: Duration(milliseconds: duration), curve: Curves.ease, ); }
2022.9.24 補充:
那如果再提供一種通過點擊按鈕來控制 DraggableScrollableSheet
收起和彈出的方法呢?
我們可以直接這樣,是不是很簡單:
Future<void> _scrollAnimation2() async { if (_animation) { return; } if (_currStale > (_maxScale + _minScale) * 0.5) { _animationTo(_minScale, _duration); } else { _animationTo(_maxScale, _duration); } }
以上就是Flutter實現(xiàn)固定header底部滑動頁效果示例的詳細內(nèi)容,更多關(guān)于Flutter固定header底部滑動頁的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android HttpClient GET或者POST請求基本使用方法
在Android開發(fā)中我們經(jīng)常會用到網(wǎng)絡(luò)連接功能與服務(wù)器進行數(shù)據(jù)的交互,為此Android的SDK提供了Apache的HttpClient來方便我們使用各種Http服務(wù).這里只介紹如何使用HttpClient發(fā)起GET或者POST請求2012-12-12Android實現(xiàn)從本地圖庫/相機拍照后裁剪圖片并設(shè)置頭像
玩qq或者是微信的盆友都知道,這些聊天工具里都要設(shè)置頭像,一般情況下大家的解決辦法是從本地圖庫選擇圖片或是從相機拍照,然后根據(jù)自己的喜愛截取圖片,接下來通過本文給大家介紹Android實現(xiàn)從本地圖庫/相機拍照后裁剪圖片并設(shè)置頭像,需要的朋友參考下2016-02-02Android 中NumberPicker,DatePicker與DatePickerDialog中分割顏色的修改實例代
這篇文章主要介紹了Android 中NumberPicker,DatePicker與DatePickerDialog中分割顏色的修改實例代碼的相關(guān)資料,這里提供實例代碼,需要的朋友可以參考下2017-03-03