【问题标题】:Flutter change value using GestureDetector, swipe to right or left使用 GestureDetector 改变值,向右或向左滑动
【发布时间】:2020-04-24 06:54:17
【问题描述】:

在颤振应用程序中,我的简单变量为currentValue,此变量值应在0.01 之间,通过手势并向右或向左滑动我想更改currentValue 的此值

考虑到变量的最新值,向右滑动应为加,向左滑动应为负,例如:

swipe to right => 0.0, 0.1, 0.2, 0.3, 0.4 ...


swipe to left  => 0.5, 0.4, 0.3, 0.2, 0.1 ...

【问题讨论】:

    标签: flutter


    【解决方案1】:
    class MyClass extends StatefulWidget {
      @override
      _MyClassState createState() => _MyClassState();
    }
    
    class _MyClassState extends State<MyClass> {
      double value ;
      @override
      void initState() {
        value = 0.0;
        super.initState();
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: <Widget>[
              Expanded(child: Text('value = $value')),
              Expanded(
                child: Container(
                  color: Colors.red,
                  child: GestureDetector(
                    onPanUpdate:(DragUpdateDetails details){
                      if(details.delta.dx>0){
                        print('right swipe');
                        //right scroll
                        //increment counter
                        setState(() {
                          value+=0.1;
                        });
                      }
                      else if(details.delta.dx<0){
                        print('left swipe');
                        //left scroll
                        //decrement counter
                        setState(() {
                          value-=0.1;
                        });
                      }
                    },
                  ),
                ),
              )
            ],
          ),
        );
      }
    }
    

    https://stackoverflow.com/a/54238847/9142279

    【讨论】:

    • 我如何计算dx并将其转换为double?
    • 你为什么要这样做?你说向左滑动 = -0.1 向右滑动 = +0.1
    • 我想通过考虑 0.0 和 1 之间的左右滑动来更改变量值
    • 我不知道如何计算并将 dx 转换为我想要的值
    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多