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

Flutter 滾動監(jiān)聽及實(shí)戰(zhàn)appBar滾動漸變的實(shí)現(xiàn)

 更新時間:2019年09月29日 10:49:36   作者:曉峰殘?jiān)? 
這篇文章主要介紹了Flutter 滾動監(jiān)聽及實(shí)戰(zhàn)appBar滾動漸變,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

介紹

在 Flutter 中滾動監(jiān)聽一般可以采用兩種方式來實(shí)現(xiàn),分別是ScrollControllerNotificationListener這兩種方式。

ScrollController介紹

ScrollController

介紹一下ScrollController常用的屬性和方法:

  • offset:可滾動組件當(dāng)前的滾動位置。
  • jumpTo(double offset)跳轉(zhuǎn)到指定位置,offset為滾動偏移量。
  • animateTo(double offset,@required Duration duration,@required Curve curve)jumpTo(double offset)一樣,不同的是animateTo跳轉(zhuǎn)時會執(zhí)行一個動畫,需要傳入執(zhí)行動畫需要的時間和動畫曲線。

ScrollPosition

ScrollPosition是用來保存可滾動組件的滾動位置的。一個 ScrollController 對象可能會被多個可滾動的組件使用,

ScrollController 會為每一個滾動組件創(chuàng)建一個 ScrollPosition 對象來存儲位置信息。ScrollPosition 中存儲的是在 ScrollController 的 positions 屬性里面,他是一個List<ScrollPosition>數(shù)組,在 ScrollController 中真正保存位置信息的就是 ScrollPosition,而 offset 只是一個便捷使用的屬性。查看源碼中可以發(fā)現(xiàn) offset 獲取就是從 ScrollPosition 中獲取的。

/// Returns the attached [ScrollPosition], from which the actual scroll offset
 /// of the [ScrollView] can be obtained.
 /// Calling this is only valid when only a single position is attached.
 ScrollPosition get position {
  assert(_positions.isNotEmpty, 'ScrollController not attached to any scroll views.');
  assert(_positions.length == 1, 'ScrollController attached to multiple scroll views.');
  return _positions.single;
 } 

 /// The current scroll offset of the scrollable widget.
 /// Requires the controller to be controlling exactly one scrollable widget.
 double get offset => position.pixels;

一個ScrollController雖然可以對應(yīng)多個可滾動組件,但是讀取滾動位置offset,則需要一對一讀取。在一對多的情況下,我們可以使用其他方法來實(shí)現(xiàn)讀取滾動位置。假設(shè)現(xiàn)在一個ScrollController對應(yīng)了兩個可以滾動的組件,那么可以通過position.elementAt(index)來獲取ScrollPosition,從而獲得offset

controller.positions.elementAt(0).pixels
controller.positions.elementAt(1).pixels

ScrollPosition的方法

ScrollPosition有兩個常用方法:分別是animateTo()jumpTo(),他們才是真正控制跳轉(zhuǎn)到滾動位置的方法,在 ScrollController 中這兩個同名方法,內(nèi)部最終都會調(diào)用 ScrollPosition 這兩個方法。

Future<void> animateTo(
  double offset, {
  @required Duration duration,
  @required Curve curve,
 }) {
  assert(_positions.isNotEmpty, 'ScrollController not attached to any scroll views.');
  final List<Future<void>> animations = List<Future<void>>(_positions.length);
  for (int i = 0; i < _positions.length; i += 1)
   // 調(diào)用 ScrollPosition 中的 animateTo 方法
   animations[i] = _positions[i].animateTo(offset, duration: duration, curve: curve);
  return Future.wait<void>(animations).then<void>((List<void> _) => null);
 }

ScrollController控制原理

ScrollController還有其他比較重要的三個方法:

1、createScrollPosition:當(dāng)ScrollController和可滾動組件關(guān)聯(lián)時,可滾動組件首先會調(diào)ScrollControllercreateScrollPosition方法來創(chuàng)建一個ScrollPosition來存儲滾動位置信息。

ScrollPosition createScrollPosition(
  ScrollPhysics physics,
  ScrollContext context,
  ScrollPosition oldPosition);

2、在滾動組件調(diào)用createScrollPosition方法之后,接著會調(diào)用attach方法來將創(chuàng)建號的ScrollPosition信息添加到positions屬性中,這一步稱為“注冊位置”,只有注冊后animateTo()jumpTo()才可以被調(diào)用。

void attach(ScrollPosition position);

3、最后當(dāng)可滾動組件被銷毀時,會調(diào)用detach()方法,將其ScrollPosition對象從ScrollControllerpositions屬性中移除,這一步稱為“注銷位置”,注銷后animateTo()jumpTo()將不能再被調(diào)用。

void detach(ScrollPosition position);

NotificationListener介紹

通知冒泡

Flutter Widget 樹中子 Widge t可以通過發(fā)送通知(Notification)與父(包括祖先) Widget 進(jìn)行通信,父級組件可以通過NotificationListener組件來監(jiān)聽自己關(guān)注的通知,這種通信方式類似于 Web 開發(fā)中瀏覽器的事件冒泡,在 Flutter 中就沿用了“冒泡”這個術(shù)語,稱為通知冒泡

通知冒泡和用戶觸摸事件冒泡是相似的,但有一點(diǎn)不同:通知冒泡可以中止,但用戶觸摸事件不行。

滾動通知

Flutter 中很多地方使用了通知,如可滾動組件(Scrollable Widget)滑動時就會分發(fā)滾動通知(ScrollNotification),而Scrollbar正是通過監(jiān)聽ScrollNotification來確定滾動條位置的。

switch (notification.runtimeType){
 case ScrollStartNotification: print("開始滾動"); break;
 case ScrollUpdateNotification: print("正在滾動"); break;
 case ScrollEndNotification: print("滾動停止"); break;
 case OverscrollNotification: print("滾動到邊界"); break;
}

其中ScrollStartNotificationScrollUpdateNotification等都是繼承ScrollNotification類的,不同類型的通知子類會包含不同的信息,ScrollUpdateNotification有一個scrollDelta屬性,它記錄了移動的位移。

NotificationListener時繼承StatelessWidget類的額,左右我們可以直接在放置在Widget 數(shù)中,通過里面的onNotification可以指定一個模板參數(shù),該模板參數(shù)類型必須是繼承自Notification,可以顯式指定模板參數(shù)時,比如通知的類型為滾動結(jié)束通知:

NotificationListener<ScrollEndNotification>

這個時候NotificationListener便只會接收該參數(shù)類型的通知。

onNotification回調(diào)為通知處理回調(diào),他的返回值時布爾類型(bool),當(dāng)返回值為true時,阻止冒泡,其父級 Widget 將再也收不到該通知;當(dāng)返回值為false時繼續(xù)向上冒泡通知。

兩者區(qū)別

首先這兩種方式都可以實(shí)現(xiàn)對滾動的監(jiān)聽,但是他們還是有一些區(qū)別:

  • ScrollController可以控制滾動控件的滾動,而NotificationListener是不可以的。
  • 通過NotificationListener可以在從可滾動組件到widget樹根之間任意位置都能監(jiān)聽,而ScrollController只能和具體的可滾動組件關(guān)聯(lián)后才可以。
  • 收到滾動事件后獲得的信息不同;NotificationListener在收到滾動事件時,通知中會攜帶當(dāng)前滾動位置和ViewPort的一些信息,而ScrollController只能獲取當(dāng)前滾動位置。ScrollController實(shí)例效果圖

代碼實(shí)現(xiàn)步驟

創(chuàng)建滾動所需的界面,一個Scaffold組件body里面方式一個Stack的層疊小部件,里面放置一個listview,和自定義的appBar;floatingActionButton放置一個返回頂部的懸浮按鈕。

Scaffold(
   body: Stack(
    children: <Widget>[
     MediaQuery.removePadding(
      removeTop: true,
      context: context,
      child: ListView.builder(
       // ScrollController 關(guān)聯(lián)滾動組件
       controller: _controller,
       itemCount: 100,
       itemBuilder: (context, index) {
        if (index == 0) {
         return Container(
          height: 200,
          child: Swiper(
           itemBuilder: (BuildContext context, int index) {
            return new Image.network(
             "http://via.placeholder.com/350x150",
             fit: BoxFit.fill,
            );
           },
           itemCount: 3,
           autoplay: true,
           pagination: new SwiperPagination(),
          ),
         );
        }
        return ListTile(
         title: Text("ListTile:$index"),
        );
       },
      ),
     ),
     Opacity(
      opacity: toolbarOpacity,
      child: Container(
       height: 98,
       color: Colors.blue,
       child: Padding(
        padding: const EdgeInsets.only(top: 30.0),
        child: Center(
         child: Text(
          "ScrollerDemo",
          style: TextStyle(color: Colors.white, fontSize: 20.0),
         ),
        ),
       ),
      ),
     )
    ],
   ),
   floatingActionButton: !showToTopBtn
     ? null
     : FloatingActionButton(
       child: Icon(Icons.keyboard_arrow_up),
       onPressed: () {
        _controller.animateTo(0.0, duration: Duration(milliseconds: 500), curve: Curves.ease);
       },
      ),
  )

創(chuàng)建ScrollController對象,在初始化中添加對滾動的監(jiān)聽,并和ListView這個可滾動小部件進(jìn)行關(guān)聯(lián):

 double t = _controller.offset / DEFAULT_SCROLLER;
 if (t < 0.0) {
  t = 0.0;
 } else if (t > 1.0) {
  t = 1.0;
 }
 setState(() {
  toolbarOpacity = t;
 });

在 _controller.addListener 中添加相關(guān)業(yè)務(wù)代碼,根據(jù)滾動的偏移量計(jì)算出透明度,實(shí)現(xiàn)appBar滾動漸變:

if(_controller.offset < DEFAULT_SHOW_TOP_BTN && showToTopBtn){
 setState(() {
 showToTopBtn = false;
	});
}else if(_controller.offset >= DEFAULT_SHOW_TOP_BTN && !showToTopBtn){
 setState(() {
  showToTopBtn = true;
 });
}

更具滾動的高度和當(dāng)前floatingActionButton的現(xiàn)實(shí)狀態(tài),判斷floatingActionButton是否需要展示:

if(_controller.offset < DEFAULT_SHOW_TOP_BTN && showToTopBtn){
 setState(() {
 showToTopBtn = false;
	});
}else if(_controller.offset >= DEFAULT_SHOW_TOP_BTN && !showToTopBtn){
 setState(() {
  showToTopBtn = true;
 });
}

點(diǎn)擊floatingActionButton返回到頂部:

 _controller.animateTo(0.0, duration: Duration(milliseconds: 500), curve: Curves.ease);

完整代碼請參考下方GitHub項(xiàng)目中/demo/scroller_demo.dart文件。

NotificationListener實(shí)例

效果圖

代碼實(shí)現(xiàn)步驟

在 NotificationListener 實(shí)例中布局基本上和 ScrollController 一致,不同的地方在于 ListView 需要包裹在 NotificationListener 中作為 child,然后 NotificationListener 在 onNotification 中判斷滾動偏移量:

if (notification is ScrollUpdateNotification && notification.depth == 0) {
 double t = notification.metrics.pixels / DEFAULT_SCROLLER;
 if (t < 0.0) {
  t = 0.0;
 } else if (t > 1.0) {
  t = 1.0;
 }
 setState(() {
  toolbarOpacity = t;
 });

 print(notification.metrics.pixels); //打印滾動位置
}

完整代碼請參考下方GitHub項(xiàng)目中/demo/notification_listener_demo.dart文件

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中自定義ContentProvider實(shí)例

    Android中自定義ContentProvider實(shí)例

    應(yīng)用A(TestBaidu)調(diào)用另外一個應(yīng)用(TestContentProvider)即自定義ContentProvider的使用,其它應(yīng)用調(diào)用該ContentProvider,具體如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果

    Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果

    這篇文章主要介紹了Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果,本文通過實(shí)例代碼結(jié)合文字說明的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2018-03-03
  • 深入了解OkHttp3之Interceptors

    深入了解OkHttp3之Interceptors

    這篇文章主要介紹了深入了解OkHttp3之Interceptors,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Android 超詳細(xì)講解fitsSystemWindows屬性的使用

    Android 超詳細(xì)講解fitsSystemWindows屬性的使用

    fitsSystemWindows屬性可以讓view根據(jù)系統(tǒng)窗口來調(diào)整自己的布局;簡單點(diǎn)說就是我們在設(shè)置應(yīng)用布局時是否考慮系統(tǒng)窗口布局,這里系統(tǒng)窗口包括系統(tǒng)狀態(tài)欄、導(dǎo)航欄、輸入法等,包括一些手機(jī)系統(tǒng)帶有的底部虛擬按鍵
    2022-03-03
  • Android中的Handler與多線程應(yīng)用實(shí)例

    Android中的Handler與多線程應(yīng)用實(shí)例

    這篇文章主要介紹了Android中的Handler與多線程應(yīng)用實(shí)例,本文首先解釋一下handler是用來干嘛的,然后通過例子介紹其在多線程中的應(yīng)用,需要的朋友可以參考下
    2015-03-03
  • Android?Studio實(shí)現(xiàn)智能聊天

    Android?Studio實(shí)現(xiàn)智能聊天

    這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)智能聊天,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Android開發(fā)中Flutter組件實(shí)用技巧

    Android開發(fā)中Flutter組件實(shí)用技巧

    這篇文章主要為大家介紹了Android開發(fā)中Flutter組件實(shí)用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程

    Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程

    這篇文章主要介紹了Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • MTK Android平臺開發(fā)流程

    MTK Android平臺開發(fā)流程

    這篇文章主要介紹了MTK在Android平臺開發(fā)的流程,一共分析了44個步驟,需要的朋友學(xué)習(xí)下吧。
    2017-12-12
  • 去掉RecycleView或者ListView上下滑動陰影的方法

    去掉RecycleView或者ListView上下滑動陰影的方法

    下面小編就為大家分享一篇去掉RecycleView或者ListView上下滑動陰影的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01

最新評論