Flutter页面视图无法在Web上滑动(桌面模式)

c8ib6hqw  于 2022-11-25  发布在  Flutter
关注(0)|答案(4)|浏览(264)

我是flutter的新手。我在flutter文档的帮助下实现了flutter PageView:

/// Flutter code sample for PageView

// Here is an example of [PageView]. It creates a centered [Text] in each of the three pages
// which scroll horizontally.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatelessWidget(),
      ),
    );
  }
}

/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final PageController controller = PageController(initialPage: 0);
    return PageView(
      /// [PageView.scrollDirection] defaults to [Axis.horizontal].
      /// Use [Axis.vertical] to scroll vertically.
      scrollDirection: Axis.horizontal,
      controller: controller,
      children: const <Widget>[
        Center(
          child: Text('First Page'),
        ),
        Center(
          child: Text('Second Page'),
        ),
        Center(
          child: Text('Third Page'),
        )
      ],
    );
  }
}

我在安卓系统上运行它,它运行得很好。而且它在网络(移动模式)下也运行得很好。
但是当我在chrome(web)上运行它时|桌面)页面是不可滑动的,也没有办法更改页面。
如何在Web桌面导出时启用滑动?
Flutter版本为2.5.2

wgx48brx

wgx48brx1#

感谢Bigfoot,对于支持用鼠标滑动,我们需要通过以下步骤更改应用程序的默认滚动行为:
1-创建一个类,从MaterialScrollBeavior扩展它,然后重写dragDevices

class AppScrollBehavior extends MaterialScrollBehavior {
  @override
  Set<PointerDeviceKind> get dragDevices => {
        PointerDeviceKind.touch,
        PointerDeviceKind.mouse,
      };
}

2-传递一个AppScrollBehavior示例给MaterialApp的scrollBehavior属性:

MaterialApp(
      scrollBehavior: AppScrollBehavior(),
      ...
    );

之后,我们可以滑动页面之间也与鼠标。

更新Flutter3.3:

从flutter 3.3开始,笔记本电脑上的触控板不支持滚动和滑动操作,如果你也需要支持触控板,你需要添加PointerDeviceKind.trackpad来拖动设备,如下所示:

class AppScrollBehavior extends MaterialScrollBehavior {
  @override
  Set<PointerDeviceKind> get dragDevices => {
        PointerDeviceKind.touch,
        PointerDeviceKind.mouse,
        PointerDeviceKind.trackpad,
      };
}
ghg1uchk

ghg1uchk2#

在flutter 2.5.0上,他们更改了滚动行为,请检查此Default drag scrolling devices

dluptydi

dluptydi3#

我在Flutter Portfolio网站上使用了PageView。我注意到我无法滑动来切换页面。后来我才知道,这可以通过Mac的触控板手势来完成。由于PageView是水平的,我必须在触控板上水平滑动两个手指。你可以试试。

ma8fv8wu

ma8fv8wu4#

无需创建额外的类和其他内容,您可以执行以下操作:

ScrollConfiguration(
        behavior: ScrollConfiguration.of(context).copyWith(
          dragDevices: {
            PointerDeviceKind.touch,
            PointerDeviceKind.mouse,
          },
        ),
        child: MyScrollableWidget(...)

相关问题