If you want to add on tap function in flutter? You can create the button and Wrap it in a GestureDetector with an onTap callback.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { final title = 'Gesture Demo'; return MaterialApp( title: title, home: MyHomePage(title: title), ); } } class MyHomePage extends StatelessWidget { final String title; MyHomePage({Key key, this.title}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(title), ), body: Center(child: MyButton()), ); } } class MyButton extends StatelessWidget { @override Widget build(BuildContext context) { // Our GestureDetector wraps our button return GestureDetector( // When the child is tapped, show a snackbar onTap: () { final snackBar = SnackBar(content: Text("Tap")); Scaffold.of(context).showSnackBar(snackBar); }, // Our Custom Button! child: Container( padding: EdgeInsets.all(12.0), decoration: BoxDecoration( color: Theme.of(context).buttonColor, borderRadius: BorderRadius.circular(8.0), ), child: Text('My Button'), ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.