Flutter - Introduction To Gestures
Flutter - Introduction To Gestures
o onPanUpdate
o onPanEnd
Now try to create sample hello world application to include gesture detection
feature and try to understand the concept.
Change the body content of the MyHomePage widget as shown below:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Center(
child: GestureDetector(
//Gesture of onTap
onTap: () {
_showDialog(context);
},
child: Text(
'Click ME',
))),
);
}
Observe that here we have placed the GestureDetector widget above the Text
widget in the widget hierarchy, captured the onTap event and then finally shown a
dialog window.
Implement the *_showDialog* function to present a dialog when user tabs the Click
ME message. It uses the generic showDialog and AlertDialog widget to create a new
dialog widget. The code is shown below:
// user defined function
void _showDialog(BuildContext context) {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Message"),
content: new Text("Hello, How are you ?"),
actions: <Widget>[
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
The application will reload in the device using Hot Reload feature. Now, simply click
the message, Hello World and it will show the dialog as below:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hello World Demo Application',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: '.:: Home page - Gesture ::.'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key, this.title=''}) : super(key: key);
final String title;
// user defined function
void _showDialog(BuildContext context) {
// flutter defined function
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Message"),
content: new Text("Hello, How are you ?"),
actions: <Widget>[
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Center(
child: GestureDetector(
//Gesture of onTap
onTap: () {
_showDialog(context);
},
child: Text(
'Click ME',
))),
);
}
}
Tugas :
- Buatlah sebuah aplikasi Latihan tersebut kemudian coba lakukan modifikasi.
- Kirimkan file project dan dokumentasi ke email :
Muhammad.irvan@mercubuana.ac.id
- Tugas boleh di kerjakan berkelompok – max 2 orang.