使用GestureDetector颤动更改值,向右或向左滑动

时间:2020-04-23 21:57:52

标签: flutter

在flutter应用程序中,我将简单变量设置为currentValue,并且此变量值应介于0.01之间,通过手势并向右或向左滑动,我想更改此{ {1}}

考虑到变量的最新值,应添加

向右滑动,向左滑动为负,例如:

currentValue

1 个答案:

答案 0 :(得分: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