GAvi
GAvi

Reputation: 69

How to check if the user swiped left or right?

Using the gesture detector, i am able to get any horizontal drag (onHorizontalDragStart) from the user, however is it possible to get the actual direction?

Upvotes: 0

Views: 630

Answers (1)

CODE LORD
CODE LORD

Reputation: 149

There is Dismissible widget for this. It's pretty configurable.

enter image description here

Note: If you don't want to provide visual feedback on the swipe, you could use a Stack to put a transparent Dismissible on top of another widget.

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,
          ),
        ),
      ),
    );
  }
}

From Collin Jackson's Answer

Upvotes: 1

Related Questions