Filter by Widgets


Handling gestures in Flutter

This Article is posted by seven.srikanth at 06-12-2018 02:57:16


In this article, I'm going to share you an example of how to handle gestures in Flutter.
This is how the final output is going to look. 
    
In this below example you handle onTap, onDoubleTap and onLongPress gestures. Check out https://docs.flutter.io/flutter/widgets/GestureDetector-class.html for more properties. 
Here is the full code from main.dart file. You just need to create a flutter program and replace the code in main.dart file with below code.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: "Gestures Demo",
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _action = "Waiting for an action";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Gestures Demo"),
),
body: Center(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(50.0),
),
Text(
"$_action",
),
GestureDetector(
onTap: () {
setState(() {
_action = "Tapped";
});
},
onDoubleTap: () {
setState(() {
_action = "Double Tapped";
});
},
onLongPress: () {
setState(() {
_action = "Log Pressed";
});
},
child: Container(
height: 40.0,
padding: EdgeInsets.all(10.0),
margin: EdgeInsets.symmetric(vertical: 28.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.lightGreen,
),
child: Text("Do something here"),
),
),
],
)),
);
}
}
Hope this is helpful. 
Thanks,
Srikanth
0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.




Recent articles

Error: The name 'RangeSlider' is defined in the libraries 'package:flutter/src/material/range_slider.dart' and 'package:flutter_range_slider/src/flutter_range_slider.dart'.

Check out this article on Error: The name 'RangeSlider' is defined in the libraries 'package:flutter/src/material/range_slider.dart' and 'package:flutter_range_slider/src/flutter_range_slider.dart'.

Read full post

How to create a persistent bottom sheet in Flutter?

Check out this article on How to create a persistent bottom sheet in Flutter?

Read full post

How to draw a Decorative Horizontal line in Flutter?

Check out this article on How to draw a Decorative Horizontal line in Flutter?

Read full post

How to fetch an Image from Internet in Flutter

Check out this article on How to fetch an Image from Internet in Flutter

Read full post

ListView with Images from the Internet

Check out this article on ListView with Images from the Internet

Read full post

How to show Tooltip in Flutter?

Check out this article on How to show Tooltip in Flutter?

Read full post

How to create BottomNavigationBar in flutter?

Check out this article on How to create BottomNavigationBar in flutter?

Read full post

How to add actions to Appbar?

Check out this article on How to add actions to Appbar?

Read full post

How to open dart devtools in VS code?

Check out this article on How to open dart devtools in VS code?

Read full post

Accessibility features in flutter

Check out this article on Accessibility features in flutter

Read full post

© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com

Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License , and code samples are licensed under the BSD License.