【问题标题】:Flutter PageView not swipeable on web (desktop mode)Flutter PageView 在网络上不可滑动(桌面模式)
【发布时间】:2021-11-24 05:26:06
【问题描述】:

我是新来的颤振。 我在其文档的帮助下实现了 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'),
        )
      ],
    );
  }
}

我在 Android 上运行它,它运行良好。 它也适用于网络(移动模式)。

但是当我在 chrome (web|desktop) 上运行时,页面无法滑动,也无法更改页面。

如何在 Web 桌面导出时启用滑动?

Flutter 版本是 2.5.2

【问题讨论】:

    标签: flutter flutter-pageview


    【解决方案1】:

    我在 Flutter 作品集网站中使用了 PageView。我注意到我无法滑动以更改页面。后来我才知道这可以通过我的 Mac 的触控板手势来完成。 由于 PageView 是水平的,我不得不在触控板上水平滑动两根手指。你可以试试。

    【讨论】:

      【解决方案2】:

      在颤振 2.5.0 上,它们改变了滚动行为 检查这个Default drag scrolling devices

      【讨论】:

        【解决方案3】:

        感谢Bigfoot,为了支持鼠标滑动,我们需要通过以下步骤来改变应用的默认滚动行为:

        1- 创建一个类,从MaterialScrollBeavior 扩展它,并覆盖dragDevices

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

        2- 将 AppScrollBehavior 实例传递给 MaterialApp 的 scrollBehavior 属性:

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

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

        【讨论】:

          猜你喜欢
          • 2020-12-14
          • 2020-07-05
          • 2020-08-26
          • 1970-01-01
          • 2022-01-18
          • 2020-02-08
          • 2020-09-17
          • 1970-01-01
          • 2021-03-27
          相关资源
          最近更新 更多