【问题标题】:Flutter vertical swipe and avoiding scrolling ListviewFlutter垂直滑动并避免滚动Listview
【发布时间】:2023-03-18 04:56:01
【问题描述】:

在颤振中我有一个挑战,我想有一个简单的Listview 有一些项目,每个项目的底部都有一个图像和文本,你假设我们有Instagram 卡,

我们知道,当我们有一个垂直的ListView 时,我们可以滚动它的顶部或底部,滚动列表视图可以发生在列表视图的每个项目上。

现在在此列表视图的每个项目上,我想像滚动顶部一样滑动顶部,而不是将列表视图滚动到顶部,我想在此项目上显示另一个小部件

我的问题是如何在我们将图像刷入卡片时避免滚动列表视图

【问题讨论】:

  • @pskink 我的意思是垂直列表视图,在图像顶部滑动到列表视图项

标签: flutter dart listview


【解决方案1】:

您可以使用GestureDetector 包装您的图像小部件,并使用this 方法来禁用用户点击图像小部件时的滚动行为。

我发现使用此方法的一个方便行为是用户仍然可以根据需要向上或向下滚动(点击向下并立即滑动,而不是点击向下然后滑动)。这可能不是最好的方法,因为我不能只阻止向上滚动行为。

这是我的例子:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ScrollPhysics physics = const AlwaysScrollableScrollPhysics();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        // Add the scroll physics controller here
        physics: physics,
        children: [
          for (int i = 0; i < 20; i++) ...[
            // Wrap the Widget with GestureDetector
            GestureDetector(
              // Disable the scroll behavior when users tap down
              onTapDown: (_) {
                setState(() {
                  physics = const NeverScrollableScrollPhysics();
                });
              },
              // Enable the scroll behavior when user leave
              onTapCancel: () {
                setState(() {
                  physics = const AlwaysScrollableScrollPhysics();
                });
              },
              onPanUpdate: (details) {
                // Catch the swip up action.
                if (details.delta.dy < 0) {
                  print('Swipping up the element $i');
                }
                // Catch the swip down action.
                if (details.delta.dy > 0) {
                  print('Swipping down the element $i');
                }
              },
              // Your image widget here
              child: Padding(
                padding: const EdgeInsets.all(20),
                child: Container(
                  width: 100,
                  height: 100,
                  color: Colors.red,
                ),
              ),
            ),
            Center(child: Text('Element $i')),
          ],
        ],
      ),
    );
  }
}

【讨论】:

  • 非常感谢,我如何只能限制滑动y 轴?在每个边缘上向上或向下滑动作品
  • 我不太明白你的问题,但上面的代码只是为了解决与 ListView 相关的问题(它只有一个 y 轴行为),它只会阻止 ListView 的滚动行为。当您点击图片时,ListView 的滚动控制器将被禁用,因此您可以对onPanUpdate 中的用户行为进行自己的逻辑。
猜你喜欢
  • 1970-01-01
  • 2012-03-08
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 2020-02-14
  • 1970-01-01
相关资源
最近更新 更多