Flutter PageController nextPage、previousPage、animateToPage 不工作

Flutter PageController nextPage, previousPage, animateToPage are not working

我正在尝试在 web 上通过鼠标滚动(快照)进行页面切换,因为 flutter 默认不支持它。但是,animateTo 方法没有造成任何影响,滚动仍在继续。 我使用按钮尝试了 animateTo,它起作用了。我不太清楚为什么它不能以这种方式工作。

代码如下:

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show kIsWeb;

class MainView extends StatelessWidget {
  MainView({Key? key}) : super(key: key);
  final PageController controller = PageController(initialPage: 0);

  @override
  Widget build(BuildContext context) {
    if (kIsWeb) {
      // running on the web!
      return Listener(
        onPointerSignal: (pointerSignal) {
          if (pointerSignal is PointerScrollEvent) {
            if (pointerSignal.scrollDelta.dy > 0) {
              // scroll down
              controller.nextPage(
                  duration: const Duration(seconds: 1), curve: Curves.ease);
            } else if (pointerSignal.scrollDelta.dy < 0) {
              controller.previousPage(
                  duration: const Duration(seconds: 1), curve: Curves.ease);
            }
          }
        },
        child: PageView(
          scrollDirection: Axis.vertical,
          pageSnapping: false,
          controller: controller,
          children: const <Widget>[
            Center(
              child: Text('First Page'),
            ),
            Center(
              child: Text('Second Page'),
            ),
            Center(
              child: Text('Third Page'),
            )
          ],
        ),
      );
    } else {
      // NOT running on the web! You can check for additional platforms here.
      return const Container();
    }
  }
}```

如果我没理解错的话,您想在使用指针信号事件用鼠标滚动时为页面设置动画。一种选择是将 physics: const NeverScrollableScrollPhysics(), 添加到您的 PageView 小部件,这将禁用任何默认滚动行为。

希望这就是您要找的!