NotificationListener 无法与 Flutter 中的水平 ListView.builder 配合使用

问题描述 投票:0回答:1

我的代码是:

import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(
      home: MyHomePage(),
    ));

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: NotificationListener(
          onNotification: (scrollNotification) {
            print(scrollNotification); 
            return true; 
          },
          child: ListView.builder(
            scrollDirection: Axis.horizontal,  // <= works if replaced by vertical
            itemCount: 2,                      // <= works if replaces by 22
            itemBuilder: (context, index) {
              return const Text("text ");
            },
          ),
        ),
      ),
    );
  }
}

当我滚动时,它不会生成滚动事件。

如果我更改为

Axis.vertical
,它会按预期工作。

如果我将

itemCount
更改为 22 - 就可以了。

flutter dart scroll
1个回答
0
投票

使用物理:AlwaysScrollableScrollPhysics(),即使没有足够的项目,也会强制列表视图可滚动,以便侦听器接收数据。

© www.soinside.com 2019 - 2024. All rights reserved.