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

标签 flutter dart flutter-layout

当垂直滚动时,我的小部件树顶部有一个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/

相关文章:

Flutter SharedPreferences 保存在 Firebase FCM 后台并通过按钮加载不起作用

dart - 将 map 的值插入字符串

user-interface - 防止内容在 SliverAppBar 下方滚动

flutter - 将 Wrap 小部件与文本一起使用

flutter - 在 AlertDialog 中获取复选框

flutter - 参数类型 'Color' 无法分配给参数类型“MaterialColor?”

sqlite - 使用flutter的sqflite报错database_closed

dart - 如何在 flutter 集成测试中模拟 http 请求?

flutter - 状态错误 : Unexpected diagnostics: Expected an identifier. [严重] json_serializable :json_serializable on test/widget_test. dart

flutter - setState() vs notifyListeners(),哪个效率更高?