Flutter中DataTable排行榜滚动到底部后外层页面无法继续滚动问题求解
问题原因
这是Flutter嵌套滚动事件冲突导致的:默认情况下子级SingleChildScrollView会消费所有垂直滑动事件,内部列表滚动到边界后,滚动事件不会传递给外层可滚动组件,所以外层无法继续滚动。
解决方案
方案1(适合DataTable行数不多的场景)
直接禁用内部滚动能力,让内部内容高度自适应,所有滚动逻辑交给外层处理,只需在内部SingleChildScrollView中新增两个属性:
Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Expanded( child: SingleChildScrollView( scrollDirection: Axis.vertical, // 新增以下两行配置 shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), child: DataTable( columns: const <DataColumn>[ DataColumn( label: Text('Name/vol', style: TextStyle(fontSize: 15, color: Colors.grey))), DataColumn( label: Text('Last Price', style: TextStyle(fontSize: 15, color: Colors.grey))), DataColumn( label: Text('24h Chg %', style: TextStyle(fontSize: 15, color: Colors.grey))), ], dataRowHeight: 65, rows: row()), ), ), ], ),
shrinkWrap: true:让SingleChildScrollView的高度跟随内部内容总高度自适应,不再默认占满父级剩余高度physics: const NeverScrollableScrollPhysics():禁用内部滚动响应,所有滑动事件都交给外层可滚动组件处理
方案2(适合DataTable行数多,需要保留内部独立滚动能力的场景)
如果需要内部列表先独立滚动,滚到底部后再触发外层滚动,可使用NestedScrollView统筹内外层滚动逻辑:将外层可滚动组件替换为NestedScrollView,把当前代码作为body属性传入,同时给内部SingleChildScrollView设置physics: const ClampingScrollPhysics()即可实现滚动联动。
内容的提问来源于stack exchange,提问作者Imesh Madushanka
相关产品推荐
相关产品推荐

