【问题标题】:How to check if the user swiped left or right?如何检查用户是向左还是向右滑动?
【发布时间】:2018-06-09 15:01:56
【问题描述】:

使用手势检测器,我可以从用户那里获得任何水平拖动(onHorizo​​ntalDragStart),但是是否可以获得实际方向?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    为此提供了 Dismissible 小部件。它非常可配置。

    注意:如果您不想在滑动时提供视觉反馈,您可以使用 Stack 将透明的Dismissible 放在另一个小部件的顶部。

    import 'package:flutter/material.dart';
    void main() {
      runApp(new MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      State createState() => new MyHomePageState();
    }
    
    class MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Dismissible(
            resizeDuration: null,
            onDismissed: (DismissDirection direction) {
              setState(() {
                _counter += direction == DismissDirection.endToStart ? 1 : -1;
              });
            },
            key: new ValueKey(_counter),
            child: new Center(
              child: new Text(
                '$_counter',
                style: Theme.of(context).textTheme.display4,
              ),
            ),
          ),
        );
      }
    }
    

    来自科林杰克逊的回答

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      相关资源
      最近更新 更多