欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter實(shí)現(xiàn)固定header底部滑動(dòng)頁效果示例

 更新時(shí)間:2022年12月29日 10:47:00   作者:Zuo  
這篇文章主要為大家介紹了Flutter實(shí)現(xiàn)固定header底部滑動(dòng)頁效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

實(shí)現(xiàn)的效果是這樣的:

剛開始的時(shí)候,是在dev上找了兩個(gè)輪子,簡(jiǎn)單測(cè)了下,都不太滿意,滑動(dòng)事件處理的比較粗糙,總有bug。就在想著,要不要拿源碼改一版的時(shí)候,讓我無意間看到了這個(gè)帖子

里面的想法,大開眼界,是通過 DraggableScrollableSheet 和 IgnorePointer 來完美實(shí)現(xiàn)上面的效果。

實(shí)現(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 是個(gè) list,

Stack(
  children: [
    Container(
      color: Colors.blue,
      child: Body( // ListView.separated
        controller: controller,
        paddingTop: headerHeight, // 防止壓蓋
      ),
    ),
    const IgnorePointer( // 這里不接收事件,所以拖動(dòng) header 也能夠滑動(dòng)頁面
      child: Header( // Container[Center[Text]]
        height: headerHeight,
      ),
    ),
  ],
)

但如果我們想在 header 內(nèi)加點(diǎn)擊事件呢?那在 Stack header 上層再加 widget 就好了。

代碼就這點(diǎn),我放在了 gitHub 上,感興趣的可以看下。

2022.8.23 補(bǔ)充:

這是在上面功能基礎(chǔ)上的一個(gè)小擴(kuò)展,即當(dāng)滑動(dòng)距離超過一半則自動(dòng)滾至頂部,反之回到底部,來看下效果:

思路也很簡(jiǎn)單,首先我要知道當(dāng)前滾動(dòng)的距離或其占比,DraggableScrollableController 提供了這個(gè)能力:

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}');
}

其次要知道用戶何時(shí)停止了滾動(dòng),我們可以使用 NotificationListener 來監(jiān)聽 DraggableScrollableSheet 的滾動(dòng)狀態(tài):

NotificationListener<ScrollNotification>(
  onNotification: (ScrollNotification notification) {
    ...
    return false;
  },
child: DraggableScrollableSheet(...),

之后在用戶停止?jié)L動(dòng)的時(shí)候,我們判斷當(dāng)前距離,并根據(jù)結(jié)果讓 DraggableScrollableSheet 自動(dòng)滾動(dòng)到頂部或底部。

onNotification: (ScrollNotification notification) {
  if (_animation) { // 動(dòng)畫中,不處理狀態(tài)
    return false;
  }
  if (notification is ScrollStartNotification) {
    debugPrint('start scroll');
  } else if (notification is ScrollEndNotification) {
    debugPrint('stop scroll');
    // 通過 [_controller.animateTo] 方法滾動(dòng)
    _scrollAnimation();
  }
  return false;

在 _scrollAnimation 內(nèi)就是滾動(dòng)的方法了,這里要注意的是,不能直接使用 await Feature,我測(cè)試在頻繁不同方向滑動(dòng)時(shí),可能會(huì)導(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 是實(shí)際控制控件滾動(dòng)的方法:

void _animationTo(double scale, int duration) {
  _controller.animateTo(
    scale,
    duration: Duration(milliseconds: duration),
    curve: Curves.ease,
  );
}

2022.9.24 補(bǔ)充:

那如果再提供一種通過點(diǎn)擊按鈕來控制 DraggableScrollableSheet 收起和彈出的方法呢?

我們可以直接這樣,是不是很簡(jiǎn)單:

Future<void> _scrollAnimation2() async {
  if (_animation) {
    return;
  }
  if (_currStale > (_maxScale + _minScale) * 0.5) {
    _animationTo(_minScale, _duration);
  } else {
    _animationTo(_maxScale, _duration);
  }
}

以上就是Flutter實(shí)現(xiàn)固定header底部滑動(dòng)頁效果示例的詳細(xì)內(nèi)容,更多關(guān)于Flutter固定header底部滑動(dòng)頁的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Android線程池控制并發(fā)數(shù)多線程下載

    Android線程池控制并發(fā)數(shù)多線程下載

    這篇文章主要為大家詳細(xì)介紹了Android線程池控制并發(fā)數(shù)多線程下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android實(shí)現(xiàn)商城購物車功能的實(shí)例代碼

    Android實(shí)現(xiàn)商城購物車功能的實(shí)例代碼

    最近公司項(xiàng)目做商城模塊,需要實(shí)現(xiàn)購物車功能,主要實(shí)現(xiàn)了單選、全選,金額合計(jì),商品刪除,商品數(shù)量加減等功能,這篇文章主要介紹了Android實(shí)現(xiàn)商城購物車功能,需要的朋友可以參考下
    2019-06-06
  • Android中asset和raw的區(qū)別詳解

    Android中asset和raw的區(qū)別詳解

    本文重點(diǎn)給大家介紹android中asset和raw的區(qū)別,以及在使用過程中需要注意點(diǎn),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧
    2016-07-07
  • Android HttpClient GET或者POST請(qǐng)求基本使用方法

    Android HttpClient GET或者POST請(qǐng)求基本使用方法

    在Android開發(fā)中我們經(jīng)常會(huì)用到網(wǎng)絡(luò)連接功能與服務(wù)器進(jìn)行數(shù)據(jù)的交互,為此Android的SDK提供了Apache的HttpClient來方便我們使用各種Http服務(wù).這里只介紹如何使用HttpClient發(fā)起GET或者POST請(qǐng)求
    2012-12-12
  • Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像

    Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像

    玩qq或者是微信的盆友都知道,這些聊天工具里都要設(shè)置頭像,一般情況下大家的解決辦法是從本地圖庫選擇圖片或是從相機(jī)拍照,然后根據(jù)自己的喜愛截取圖片,接下來通過本文給大家介紹Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像,需要的朋友參考下
    2016-02-02
  • Android下Button實(shí)現(xiàn)圖文混排效果

    Android下Button實(shí)現(xiàn)圖文混排效果

    這篇文章主要為大家詳細(xì)介紹了Android下Button實(shí)現(xiàn)圖文混排效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android 中NumberPicker,DatePicker與DatePickerDialog中分割顏色的修改實(shí)例代碼

    Android 中NumberPicker,DatePicker與DatePickerDialog中分割顏色的修改實(shí)例代

    這篇文章主要介紹了Android 中NumberPicker,DatePicker與DatePickerDialog中分割顏色的修改實(shí)例代碼的相關(guān)資料,這里提供實(shí)例代碼,需要的朋友可以參考下
    2017-03-03
  • Android 點(diǎn)擊生成二維碼功能實(shí)現(xiàn)代碼

    Android 點(diǎn)擊生成二維碼功能實(shí)現(xiàn)代碼

    二維碼,我們也稱作QRCode,QR表示quick response即快速響應(yīng),在很多App中我們都能見到二維碼的身影,最常見的莫過于微信了。接下來給大家介紹android 點(diǎn)擊生成二維碼功能實(shí)現(xiàn)代碼,需要的朋友參考下吧
    2017-11-11
  • android讀寫中文如何避免亂碼詳解

    android讀寫中文如何避免亂碼詳解

    這篇文章主要介紹了android讀寫中文如何避免亂碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • Flutter 完美的驗(yàn)證碼輸入框?qū)崿F(xiàn)

    Flutter 完美的驗(yàn)證碼輸入框?qū)崿F(xiàn)

    這篇文章主要介紹了Flutter 完美的驗(yàn)證碼輸入框?qū)崿F(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論