我已经编写了下面的代码,当用户想要的时候,可以让容器向左滑动.

    import 'package:flutter/material.dart';

double swipH = 0 ;
double swipV = 0 ;
class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {

 bool  isV = true ;
 bool isH = true ;


  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body:   Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [

          GestureDetector(




              onLongPress: (){
                debugPrint('vvvvvvvvv');
              },




                onPanUpdate:(noti){

                  if(noti.delta.dy < 0&&isV){
                    isH = false;
                    swipV += noti.delta.dy;
                    setState(() {});
                  }

                  if(noti.delta.dx < 0&&isH){
                    isV = false;
                    debugPrint('Started');
                    swipH += noti.delta.dx;
                    setState(() {});
                  }
                },

            onPanEnd:(noti){
                isV = true ;
               isH = true ;
              swipH=0;
              swipV = 0 ;
              setState(() {});
            },


              child: Transform.translate(
                offset: Offset(swipH, swipV),
                child: Container(
                  padding: const EdgeInsets.all(8),
                  height: 200,
                  width: MediaQuery.of(context).size.width / 2,
                  color: Colors.red,
                ),
              ),

            ),

          ],
        ),
      ),

    );
  }
}

编辑

 onLongPress: (){
 debugPrint('onLongPress');
  },

enter image description here

有什么好主意吗

不用onLongPress.一切都很好..但我需要这个例子中逻辑相同的方法

推荐答案

我们可以使用另一个bool?来跟踪初始运动方向并判断阻力.

double swipH = 0;
double swipV = 0;

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  bool? _isDx;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            GestureDetector(
              onPanUpdate: (noti) {
                if (noti.delta.dy < 0 && _isDx != false) {
                  swipV += noti.delta.dy;
                  _isDx = true;
                }

                if (noti.delta.dx < 0 && _isDx != true) {
                  swipH += noti.delta.dx;
                  _isDx = false;
                }
                setState(() {});
              },
              onPanEnd: (noti) {
                debugPrint('end');
                swipH = 0;
                swipV = 0;
                _isDx = null;
                setState(() {});
              },
              child: Transform.translate(
                offset: Offset(swipH, swipV),
                child: Container(
                  padding: const EdgeInsets.all(8),
                  height: 200,
                  width: MediaQuery.of(context).size.width / 2,
                  color: Colors.red,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Flutter相关问答推荐

如何使用Firebase Firestore来获取外部集合中的文档子集合中的文档

如何在Flutter 中播放音频

我怎样才能判断一个字符串值是否为空(&Q;&Q;)?

我不能通过第5步了解Flutter 翼Firebase教程

Flutter -如何停止块监听程序在后台堆栈中运行

将图标呈现在Flutter 克牌小工具的角落上

为什么在setState之后Ffltter Pageview重置为第0页

使小部件处于有状态状态会导致hasSize异常

无法构建AndroidBundle 包或iOS等

Flutter 附近的连接

在 Dart 中按土耳其语字母顺序对字符串进行排序

从所有设备注销,因为用户帐户已从 firebase flutter 中删除

Flutter / 为什么当我点击我的图片时我的模型没有更新?

Flutter 判断 Uint8List 是否有效 Image

setState 方法有时有效,有时无效

如何将金钱格式化为数以百万计的Flutter 动?

使用 onPressed 切换到另一个屏幕无法正常工作

在 Flutter 中,您什么时候应该更喜欢Widget继承而不是组合?

如何在没有上下文的情况下使用国际Flutter ?

如何从 Firebase Firestore 中获取具有特定字段值的文档?