flutter_swipe_detector 2.0.0 copy "flutter_swipe_detector: ^2.0.0" to clipboard
flutter_swipe_detector: ^2.0.0 copied to clipboard

A package to detect your swipe directions and provides you with callbacks to handle them.

example/lib/main.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swipe_detector/flutter_swipe_detector.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(
    BuildContext context,
  ) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const int _swipeHistoryLimit = 4;
  final List<SwipeDirection> _swipeHistory = [];

  @override
  Widget build(
    BuildContext context,
  ) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            SwipeDetector(
              // onSwipe: (direction, offset) {
              //   _addSwipe(direction);
              // },
              onSwipeUp: (offset) {
                _addSwipe(SwipeDirection.up);
              },
              onSwipeDown: (offset) {
                _addSwipe(SwipeDirection.down);
              },
              onSwipeLeft: (offset) {
                _addSwipe(SwipeDirection.left);
              },
              onSwipeRight: (offset) {
                _addSwipe(SwipeDirection.right);
              },
              child: Container(
                width: MediaQuery.of(context).size.width * 0.5,
                height: MediaQuery.of(context).size.width * 0.5,
                padding: const EdgeInsets.all(16),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(16),
                  color: Colors.greenAccent,
                ),
                child: Column(
                  children: [
                    const Text(
                      'Swipe me!',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 8),
                    for (final direction in _swipeHistory)
                      Padding(
                        padding: const EdgeInsets.only(top: 8),
                        child: Text(describeEnum(direction)),
                      ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _addSwipe(
    SwipeDirection direction,
  ) {
    setState(() {
      _swipeHistory.insert(0, direction);
      if (_swipeHistory.length > _swipeHistoryLimit) {
        _swipeHistory.removeLast();
      }
    });
  }
}
51
likes
120
pub points
94%
popularity

Publisher

verified publisherbaseflow.com

A package to detect your swipe directions and provides you with callbacks to handle them.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_swipe_detector