gpt4 book ai didi

flutter - Flutter滚动条防止滚动条出现在水平 ListView 中

转载 作者:行者123 更新时间:2023-12-03 03:53:16 25 4
gpt4 key购买 nike

当垂直滚动时,我的小部件树顶部有一个Scrollbar,显示在右侧。现在,在小部件树中,我还有一个水平的ListView。如果用户在水平方向滚动,滚动条也会显示在此处。我不想在这里显示ScrollBar。有什么通用的方法可以禁止在水平轴上显示吗?

@override
Widget build(BuildContext context) {

return SafeArea(
child: Scrollbar(
child: SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height * 5,
margin: EdgeInsets.all(25),
child: Column(
children: <Widget>[
Row(
...

稍后在列表 View 中:
      Expanded(child:
Consumer<Model>(builder: (context, myModel, child) {
return ListView.builder(
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
itemCount: list.length,
itemBuilder: (ctx, index) => ChangeNotifierProvider.value(
value: list[index],
child: GestureDetector(

最佳答案

假设您有一个包含两个ListViews的小部件:

Scrollbar(
controller: scrollController,
child: ListView.builder(
controller: scrollController,
scrollDirection: Axis.vertical,
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (context, index) {
return SizedBox(
height: 90,
child: ListView.separated(
scrollDirection: Axis.horizontal,
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (context, index) {
return Container(
height: 100,
width: 100,
color: Colors.blue,
);
},
separatorBuilder: (context, index) {
return SizedBox(
width: 10,
);
},
itemCount: 50,
),
);
},
itemCount: 50,
),
),
一个简单的解决方案是使用 NotificationListener 包装ListView并将 设置为onNotification 以返回true。在这种情况下,水平ListView将成为有问题的小部件。这将通知NotificationListener通知已被处理,这将阻止它在小部件树上发送ScrollNotification。
因此, 解决方案将是:
 Scrollbar(
controller: scrollController,
child: ListView.builder(
controller: scrollController,
scrollDirection: Axis.vertical,
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (context, index) {
return SizedBox(
height: 90,
child: NotificationListener<ScrollNotification>(
onNotification: (_) => true,
child: ListView.separated(
scrollDirection: Axis.horizontal,
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (context, index) {
return Container(
height: 100,
width: 100,
color: themeColor,
);
},
separatorBuilder: (context, index) {
return SizedBox(
width: 10,
);
},
itemCount: 50,
),
),
);
},
itemCount: 50,
),
),
要深入了解,请考虑阅读 docs

关于flutter - Flutter滚动条防止滚动条出现在水平 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62258506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com