如何实现Flutter中Scaffold的滚动效果?嵌套列表后消失求助
问题解决:SingleChildScrollView包裹ListView后列表消失
你的问题出在ListView本身是可滚动组件,和SingleChildScrollView嵌套时,ListView无法确定自身高度,直接收缩成0导致看不见。给你两种解决思路:
思路一:直接用ListView(推荐)
ListView本身支持滚动,不需要额外套SingleChildScrollView,直接把ListView作为body即可:
return Scaffold( body: ListView.builder( itemCount: subjects.length, itemBuilder: (context, i) { return singleSubject( subjects[i], () => deleteItem(i), ); }, ), floatingActionButton: FloatingActionButton( backgroundColor: const Color(0xFF383838), onPressed: newSubject, child: const Icon(Icons.add), ), backgroundColor: const Color(0xFFB94844), );
思路二:必须嵌套时的处理
如果页面有其他需要滚动的内容(比如顶部固定栏+列表),非要嵌套的话,给ListView加两个属性:
shrinkWrap: true:让ListView根据内容自适应高度physics: NeverScrollableScrollPhysics():禁用ListView自身的滚动,把滚动逻辑交给外层的SingleChildScrollView
修改后的代码:
return Scaffold( body: SingleChildScrollView( child: ListView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), itemCount: subjects.length, itemBuilder: (context, i) { return singleSubject( subjects[i], () => deleteItem(i), ); }, ), ), floatingActionButton: FloatingActionButton( backgroundColor: const Color(0xFF383838), onPressed: newSubject, child: const Icon(Icons.add), ), backgroundColor: const Color(0xFFB94844), );
内容的提问来源于stack exchange,提问作者Vince
相关产品推荐
相关产品推荐

