即使在使用 SingleChildScrollView() 包装 column() 后也无法在小部件上滚动
Posted
技术标签:
【中文标题】即使在使用 SingleChildScrollView() 包装 column() 后也无法在小部件上滚动【英文标题】:Unable to scroll on widget even after wrapping column() with SingleChildScrollView() 【发布时间】:2020-11-09 14:45:40 【问题描述】:这是根小部件。该小部件有一个子 BuyerPostList(),它是 ListView 类型的小部件。我删除 SingleChildScrollView() 给出了一个渲染异常。添加它后,错误不再出现,但页面仍然不可滚动。
class PostsPage extends StatelessWidget
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context)
return StreamProvider<List<BuyerPost>>.value(
value: BuyerDatabaseService().buyerPosts,
child:Scaffold(
backgroundColor: Colors.white,
appBar: header(context,isAppTitle: true),
body:Container(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
BuyerPostList(),
SizedBox(height: 100,),
],
),
),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: ()
Navigator.push(
context,
MaterialPageRoute(builder: (context) => NewPost()),
);
,
label: Text(
'New',
style: TextStyle(fontWeight:FontWeight.w900,color:Color.fromRGBO(0, 0, 0, 0.4),),
),
icon: Icon(Icons.add,color:Color.fromRGBO(0, 0, 0, 0.4),),
backgroundColor:Colors.white,
),
),
);
这是列表视图 BuyerPostList 小部件()
class BuyerPostList extends StatefulWidget
@override
_BuyerPostListState createState() => _BuyerPostListState();
class _BuyerPostListState extends State<BuyerPostList>
@override
Widget build(BuildContext context)
final posts = Provider.of<List<BuyerPost>>(context) ?? [];
return ListView.builder(
shrinkWrap: true,
itemCount: posts.length,
itemBuilder: (context, index)
return BuyerPostTile(post: posts[index]);
,
);
我希望我的解释已经足够清楚了。我将如何使它可滚动? 提前致谢。
【问题讨论】:
【参考方案1】:在ListView.builder(
中添加physics: NeverScrollableScrollPhysics(),
代码:
@override
Widget build(BuildContext context)
final posts = Provider.of<List<BuyerPost>>(context) ?? [];
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: posts.length,
itemBuilder: (context, index)
return BuyerPostTile(post: posts[index]);
,
);
【讨论】:
以上是关于即使在使用 SingleChildScrollView() 包装 column() 后也无法在小部件上滚动的主要内容,如果未能解决你的问题,请参考以下文章
即使在使用 await [重复] 之后,Javascript 也难以使用承诺值
即使在使用属性 multiple = false 不起作用后也禁用多个文件选择