Bỏ qua để đến nội dung

🔘 Button Widgets

Button nổi có shadow, thường dùng cho primary action:

ElevatedButton(
onPressed: () {
print('Pressed!');
},
child: Text('Elevated Button'),
)
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text('Styled Button'),
)

Button phẳng không có background, thường dùng cho secondary action:

TextButton(
onPressed: () {},
child: Text('Text Button'),
)
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
foregroundColor: Colors.blue,
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
),
child: Text('Styled Text Button'),
)

Button có viền, không có background:

OutlinedButton(
onPressed: () {},
child: Text('Outlined Button'),
)
OutlinedButton(
onPressed: () {},
style: OutlinedButton.styleFrom(
foregroundColor: Colors.blue,
side: BorderSide(color: Colors.blue, width: 2),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text('Styled Outlined'),
)

Button chỉ có icon:

IconButton(
onPressed: () {},
icon: Icon(Icons.favorite),
)
IconButton(
onPressed: () {},
icon: Icon(Icons.favorite),
color: Colors.red,
iconSize: 30,
splashRadius: 25,
)

Button nổi hình tròn:

FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
)
FloatingActionButton.extended(
onPressed: () {},
icon: Icon(Icons.add),
label: Text('Add Item'),
)

ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.send),
label: Text('Send'),
)
TextButton.icon(
onPressed: () {},
icon: Icon(Icons.download),
label: Text('Download'),
)
OutlinedButton.icon(
onPressed: () {},
icon: Icon(Icons.share),
label: Text('Share'),
)

Đặt onPressed: null để disable:

ElevatedButton(
onPressed: null, // Disabled
child: Text('Disabled Button'),
)
// Hoặc với điều kiện
ElevatedButton(
onPressed: isValid ? () => submit() : null,
child: Text('Submit'),
)

class LoadingButton extends StatefulWidget {
@override
State<LoadingButton> createState() => _LoadingButtonState();
}
class _LoadingButtonState extends State<LoadingButton> {
bool _isLoading = false;
Future<void> _handlePress() async {
setState(() => _isLoading = true);
await Future.delayed(Duration(seconds: 2));
setState(() => _isLoading = false);
}
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: _isLoading ? null : _handlePress,
child: _isLoading
? SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: Text('Submit'),
);
}
}

Button Use case Style
ElevatedButton Primary action Nổi, có shadow
TextButton Secondary, trong dialogs Phẳng
OutlinedButton Alternative action Có viền
IconButton Toolbar, actions Chỉ icon
FloatingActionButton Main action của screen Nổi, góc màn hình

Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 16),
),
child: Text('Login'),
),
SizedBox(height: 12),
OutlinedButton(
onPressed: () {},
style: OutlinedButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 16),
),
child: Text('Create Account'),
),
SizedBox(height: 8),
TextButton(
onPressed: () {},
child: Text('Forgot Password?'),
),
],
)
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(icon: Icon(Icons.favorite_border), onPressed: () {}),
IconButton(icon: Icon(Icons.comment), onPressed: () {}),
IconButton(icon: Icon(Icons.share), onPressed: () {}),
IconButton(icon: Icon(Icons.bookmark_border), onPressed: () {}),
],
)
AlertDialog(
title: Text('Confirm'),
content: Text('Are you sure?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('Cancel'),
),
ElevatedButton(
onPressed: () {
// Handle confirm
Navigator.pop(context);
},
child: Text('Confirm'),
),
],
)

Button Constructor Use case
ElevatedButton .new, .icon Primary action
TextButton .new, .icon Secondary action
OutlinedButton .new, .icon Alternative action
IconButton .new Icon-only action
FloatingActionButton .new, .extended Screen main action

Styling với styleFrom():

  • backgroundColor - Màu nền
  • foregroundColor - Màu text/icon
  • padding - Padding
  • shape - Shape (RoundedRectangleBorder, etc.)