Flutter 首頁必用組件NestedScrollView的示例詳解
昨天Flutter 1.17版本重磅發(fā)布,新的版本主要是優(yōu)化性能、修復bug,有人覺得此版本毫無亮點,但也從另一方面體現(xiàn)了Flutter目前針對移動端已經(jīng)較為完善,想了解具體內(nèi)容,文末有鏈接,如果你想升級到最新版本,建議慎重,有些人升級后項目無法運行。
今天介紹的組件是NestedScrollView,大部分的App首頁都會用到這個組件。
可以在其內(nèi)部嵌套其他滾動視圖的滾動視圖,其滾動位置是固有鏈接的。
在普通的ScrollView中, 如果有一個Sliver組件容納了一個TabBarView,它沿相反的方向滾動(例如,允許用戶在標簽所代表的頁面之間水平滑動,而列表則垂直滾動),則該TabBarView內(nèi)部的任何列表都不會相互作用 與外部ScrollView。 例如,瀏覽內(nèi)部列表以滾動到頂部不會導致外部ScrollView中的SliverAppBar折疊以展開。
滾動隱藏AppBar
比如實現(xiàn)如下場景,當列表滾動時,隱藏AppBar,用法如下:
NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[SliverAppBar(
title: Text('腳本之家'),
)];
},
body: ListView.builder(itemBuilder: (BuildContext context,int index){
return Container(
height: 80,
color: Colors.primaries[index % Colors.primaries.length],
alignment: Alignment.center,
child: Text(
'$index',
style: TextStyle(color: Colors.white, fontSize: 20),
),
);
},itemCount: 20,),
)
效果如下:

SliverAppBar展開折疊
用法如下:
NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[SliverAppBar(
expandedHeight: 230.0,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text('復仇者聯(lián)盟'),
background: Image.network(
'http://img.haote.com/upload/20180918/2018091815372344164.jpg',
fit: BoxFit.fitHeight,
),
),
)];
},
body: ListView.builder(itemBuilder: (BuildContext context,int index){
return Container(
height: 80,
color: Colors.primaries[index % Colors.primaries.length],
alignment: Alignment.center,
child: Text(
'$index',
style: TextStyle(color: Colors.white, fontSize: 20),
),
);
},itemCount: 20,),
)
效果如下:

與TabBar配合使用
用法如下:
NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 230.0,
pinned: true,
flexibleSpace: Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: PageView(),
),
),
SliverPersistentHeader(
pinned: true,
delegate: StickyTabBarDelegate(
child: TabBar(
labelColor: Colors.black,
controller: this._tabController,
tabs: <Widget>[
Tab(text: '資訊'),
Tab(text: '技術(shù)'),
],
),
),
),
];
},
body: TabBarView(
controller: this._tabController,
children: <Widget>[
RefreshIndicator(
onRefresh: (){
print(('onRefresh'));
},
child: _buildTabNewsList(_newsKey, _newsList),
),
_buildTabNewsList(_technologyKey, _technologyList),
],
),
)
StickyTabBarDelegate 代碼如下:
class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
final TabBar child;
StickyTabBarDelegate({@required this.child});
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return Container(
color: Theme.of(context).backgroundColor,
child: this.child,
);
}
@override
double get maxExtent => this.child.preferredSize.height;
@override
double get minExtent => this.child.preferredSize.height;
@override
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
return true;
}
}
效果如下:

其他屬性
通過scrollDirection和reverse參數(shù)控制其滾動方向,用法如下:
NestedScrollView( scrollDirection: Axis.horizontal, reverse: true, ... )
scrollDirection滾動方向,分為垂直和水平方向。
reverse參數(shù)表示反轉(zhuǎn)滾動方向,并不是由垂直轉(zhuǎn)為水平,而是垂直方向滾動時,默認向下滾動,reverse設(shè)置false,滾動方向改為向上,同理水平滾動改為水平向左。
controller為滾動控制器,可以監(jiān)聽滾到的位置,設(shè)置滾動的位置等,用法如下:
_scrollController = ScrollController();
//監(jiān)聽滾動位置
_scrollController.addListener((){
print('${_scrollController.position}');
});
//滾動到指定位置
_scrollController.animateTo(20.0);
CustomScrollView(
controller: _scrollController,
...
)
physics表示可滾動組件的物理滾動特性,具體查看ScrollPhysics
交流
Flutter博客地址(近200個控件用法):http://laomengit.com
總結(jié)
到此這篇關(guān)于Flutter 首頁必用組件NestedScrollView的文章就介紹到這了,更多相關(guān)Flutter 首頁必用組件NestedScrollView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中SharedPreferences簡單使用實例
這篇文章主要介紹了Android中SharedPreferences簡單使用案例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
解析Android中View轉(zhuǎn)換為Bitmap及getDrawingCache=null的解決方法
在android中經(jīng)常會遇到View轉(zhuǎn)換為Bitmap的情形,本篇文章主要介紹了Android中View轉(zhuǎn)換為Bitmap及getDrawingCache=null的解決方法,有需要的可以了解一下。2016-11-11
Android實現(xiàn)下拉刷新的視圖和圖標的旋轉(zhuǎn)
本篇文章主要介紹了Android實現(xiàn)下拉刷新的視圖和圖標的旋轉(zhuǎn)的實例,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
Kotlin如何安全訪問lateinit變量的實現(xiàn)
這篇文章主要介紹了Kotlin如何安全訪問lateinit變量的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01
Android Kotlin 基本數(shù)據(jù)類型詳解
Kotlin是一種靜態(tài)類型語言,適用于Android開發(fā),Kotlin的基本數(shù)據(jù)類型包括數(shù)值類型、字符類型、布爾類型和數(shù)組類型,本文介紹Android Kotlin 基本數(shù)據(jù)類型,感興趣的朋友一起看看吧2025-03-03
Android SQLite3多線程操作問題研究總結(jié)
這篇文章主要介紹了Android SQLite3多線程操作問題研究總結(jié),本文總結(jié)了SQLite3是否支持多線程、SQLiteDatabase的同步鎖、多線程讀數(shù)據(jù)庫等問題,需要的朋友可以參考下2015-03-03
Jetpack?Compose重寫TopAppBar實現(xiàn)標題多行折疊詳解
這篇文章主要為大家介紹了Jetpack?Compose重寫TopAppBar實現(xiàn)標題多行折疊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android多國語言轉(zhuǎn)換Excel及Excel轉(zhuǎn)換為string詳解
這篇文章主要給大家介紹了關(guān)于Android多國語言轉(zhuǎn)換Excel以及Excel轉(zhuǎn)換為string的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2019-01-01

