Rogerto
Rogerto

Reputation: 317

Is there a way to tell if a gesture is currently active in Flutter?

I want to check if a Slider is actively being dragged and then trigger another behaviour once the dragging gesture has been completed.

Is there a way to do this, for example with a callback of similiar?

Upvotes: 1

Views: 166

Answers (2)

Elfor
Elfor

Reputation: 587

in addition to chunhunghan's answer, you can use the GestureDetector widget to detects gestures on other widgets that may not have such methods.

Upvotes: 0

chunhunghan
chunhunghan

Reputation: 54367

You can copy paste run full code below
You can use onChangeStart, onChanged, onChangeEnd

  onChangeStart: (double value) {
    setState(() {
      _drag = "drag start";
    });
  },
  onChanged: (double value) {
    setState(() {
      _drag = "dragging";
      _currentSliderValue = value;
    });
  },
  onChangeEnd: (double value) {
    setState(() {
      _drag = "drag end";
    });
  },

working demo

enter image description here

full code

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: MyStatefulWidget(),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  double _currentSliderValue = 20;
  String _drag = "";
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text("drag status : $_drag"),
        Slider(
          value: _currentSliderValue,
          min: 0,
          max: 100,
          divisions: 5,
          label: _currentSliderValue.round().toString(),
          onChangeStart: (double value) {
            setState(() {
              _drag = "drag start";
            });
          },
          onChanged: (double value) {
            setState(() {
              _drag = "dragging";
              _currentSliderValue = value;
            });
          },
          onChangeEnd: (double value) {
            setState(() {
              _drag = "drag end";
            });
          },
        ),
      ],
    );
  }
}

Upvotes: 1

Related Questions