There are 3 type of buttons. 1) TextButton , 2) ElevatedButton and 3) OutlinedButton
1. TextButton
Syntax
TextButton(
child: Text(
"CLICK HERE",
),
onPressed: () async {
},
)
Example
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Button Example'),
),
body: Center(
child: TextButton(
onPressed: () {
// Handle button press
print('Button Pressed!');
},
child: Text('Press me'),
),
),
),
);
}
}
Styling
TextButton(
child: Text(
_buttonText,
style: TextStyle(fontSize: 25),
),
onPressed: () {},
style: TextButton.styleFrom(
foregroundColor: Colors.red,
elevation: 2,
backgroundColor: Colors.amber),
),
2. ElevatedButton
Syntax
ElevatedButton(
onPressed: () {
print('Button Pressed');
},
child: const Text('Button with OnPressed'),
)
Example
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Button Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// Handle button press
print('Button Pressed!');
},
child: Text('Press me'),
),
),
),
);
}
}
Styling
ElevatedButton(
onPressed: () {},
child: const Text('Fluttering Button'),
style: ButtonStyle(
padding: MaterialStateProperty.all(EdgeInsets.all(15)),
backgroundColor: MaterialStateProperty.all(Colors.green),
shadowColor: MaterialStateProperty.all(Colors.grey),
elevation: MaterialStateProperty.resolveWith(
(Set states) {
if (states.contains(MaterialState.pressed)) return 10;
return 5; // default elevation
},
),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
),
animationDuration: Duration(milliseconds: 200)
),
)
3. OutlinedButton:
Syntax
OutlinedButton(
child: Text('Outlined Button'),
onPressed: () => Navigator.of(context)
)
Example
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Button Example'),
),
body: Center(
child: OutlinedButton(
onPressed: () {
// Handle button press
print('Button Pressed!');
},
child: Text('Press me'),
),
),
),
);
}
}
Styling
OutlinedButton(
child: Text('Outlined Button'),
style: OutlinedButton.styleFrom(
backgroundColor: Colors.green,
primary: Colors.black,
textStyle: TextStyle(fontSize: 15, fontStyle: FontStyle.italic),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)))),
onPressed: () => Navigator.of(context)
)
import 'package:flutter/material.dart';
class MyButtonDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Button Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
// Add onPressed action here
},
child: Text('Elevated Button'),
),
SizedBox(height: 20),
TextButton(
onPressed: () {
// Add onPressed action here
},
child: Text('Text Button'),
),
SizedBox(height: 20),
OutlinedButton(
onPressed: () {
// Add onPressed action here
},
child: Text('Outlined Button'),
),
],
),
),
);
}
}