NestedScrollView圆角Body下AppBar背景图无法覆盖空白区域求助
解决SliverAppBar背景图片无法覆盖空白区域的问题
问题根源在于两点:一是默认Scaffold的AppBar不会延伸到状态栏区域,导致顶部出现空白;二是直接用Stack作为flexibleSpace未适配SliverAppBar的布局逻辑,无法完整填充可展开区域。
按以下方式修改代码即可解决:
- 在包裹NestedScrollView的Scaffold中添加
extendBodyBehindAppBar: true,让AppBar覆盖状态栏区域; - 将flexibleSpace替换为
FlexibleSpaceBar,把图片作为它的background属性,利用组件自身布局逻辑填充整个可展开区域; - 设置SliverAppBar的
backgroundColor: Colors.transparent,避免遮挡背景图片。
修改后的完整代码:
Scaffold( extendBodyBehindAppBar: true, // 关键配置:让AppBar延伸到状态栏下方 body: NestedScrollView( headerSliverBuilder: (context, innerBoxIsScrolled) { return [ SliverAppBar( title: Text( "hehe", style: TextStyle(color: Colors.white), ), floating: true, expandedHeight: 100, forceElevated: innerBoxIsScrolled, backgroundColor: Colors.transparent, // 透明化显示背景图 flexibleSpace: FlexibleSpaceBar( background: Image.network( "https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350", fit: BoxFit.cover, ), ), ) ]; }, body: Container( width: DataInstance().screenW, padding: const EdgeInsets.symmetric(vertical: kDouble_20), decoration: BoxDecoration( color: Colors.amber, borderRadius: BorderRadius.only( topLeft: Radius.circular(kDouble_20), topRight: Radius.circular(kDouble_20))), ), ), )
若不需要图片覆盖状态栏,仅填充AppBar自身空白区域,可去掉extendBodyBehindAppBar: true,保留FlexibleSpaceBar的修改即可。
内容的提问来源于stack exchange,提问作者Biến Nguyễn Văn
相关产品推荐
相关产品推荐

