Flutter按钮与交互 #
一、ElevatedButton #
1.1 基本用法 #
dart
ElevatedButton(
onPressed: () {
print('Button pressed');
},
child: Text('Elevated Button'),
)
1.2 样式设置 #
dart
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
elevation: 4,
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text('Styled Button'),
)
1.3 禁用状态 #
dart
ElevatedButton(
onPressed: null,
style: ElevatedButton.styleFrom(
disabledBackgroundColor: Colors.grey.shade300,
disabledForegroundColor: Colors.grey,
),
child: Text('Disabled'),
)
1.4 带图标 #
dart
ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.add),
label: Text('Add'),
)
二、TextButton #
2.1 基本用法 #
dart
TextButton(
onPressed: () {
print('TextButton pressed');
},
child: Text('Text Button'),
)
2.2 样式设置 #
dart
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
foregroundColor: Colors.blue,
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text('Styled TextButton'),
)
2.3 带图标 #
dart
TextButton.icon(
onPressed: () {},
icon: Icon(Icons.edit),
label: Text('Edit'),
)
三、OutlinedButton #
3.1 基本用法 #
dart
OutlinedButton(
onPressed: () {
print('OutlinedButton pressed');
},
child: Text('Outlined Button'),
)
3.2 样式设置 #
dart
OutlinedButton(
onPressed: () {},
style: OutlinedButton.styleFrom(
foregroundColor: Colors.blue,
side: BorderSide(color: Colors.blue, width: 2),
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text('Styled Outlined'),
)
3.3 带图标 #
dart
OutlinedButton.icon(
onPressed: () {},
icon: Icon(Icons.share),
label: Text('Share'),
)
四、IconButton #
4.1 基本用法 #
dart
IconButton(
icon: Icon(Icons.favorite),
onPressed: () {
print('IconButton pressed');
},
)
4.2 样式设置 #
dart
IconButton(
icon: Icon(Icons.favorite),
onPressed: () {},
iconSize: 32,
color: Colors.red,
splashRadius: 24,
tooltip: 'Add to favorites',
)
4.3 带背景 #
dart
Container(
decoration: BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
),
child: IconButton(
icon: Icon(Icons.add, color: Colors.white),
onPressed: () {},
),
)
五、FloatingActionButton #
5.1 基本用法 #
dart
FloatingActionButton(
onPressed: () {
print('FAB pressed');
},
child: Icon(Icons.add),
)
5.2 样式设置 #
dart
FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
elevation: 6,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
)
5.3 扩展FAB #
dart
FloatingActionButton.extended(
onPressed: () {},
icon: Icon(Icons.edit),
label: Text('Edit'),
backgroundColor: Colors.blue,
)
FloatingActionButton.large(
onPressed: () {},
child: Icon(Icons.add),
)
FloatingActionButton.small(
onPressed: () {},
child: Icon(Icons.add),
)
六、其他按钮 #
6.1 DropdownButton #
dart
String? selectedValue = 'Option 1';
DropdownButton<String>(
value: selectedValue,
items: ['Option 1', 'Option 2', 'Option 3']
.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
selectedValue = newValue;
});
},
)
6.2 PopupMenuButton #
dart
PopupMenuButton<String>(
icon: Icon(Icons.more_vert),
onSelected: (String value) {
print('Selected: $value');
},
itemBuilder: (BuildContext context) {
return [
PopupMenuItem(value: 'edit', child: Text('Edit')),
PopupMenuItem(value: 'delete', child: Text('Delete')),
PopupMenuItem(value: 'share', child: Text('Share')),
];
},
)
6.3 BackButton #
dart
BackButton(
color: Colors.black,
onPressed: () {
Navigator.pop(context);
},
)
6.4 CloseButton #
dart
CloseButton(
color: Colors.black,
onPressed: () {
Navigator.pop(context);
},
)
七、自定义按钮 #
7.1 自定义样式按钮 #
dart
class CustomButton extends StatelessWidget {
final String text;
final VoidCallback? onPressed;
final Color? backgroundColor;
final Color? textColor;
final double borderRadius;
final double? width;
final double height;
const CustomButton({
super.key,
required this.text,
this.onPressed,
this.backgroundColor,
this.textColor,
this.borderRadius = 8,
this.width,
this.height = 48,
});
@override
Widget build(BuildContext context) {
return SizedBox(
width: width,
height: height,
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: backgroundColor ?? Theme.of(context).primaryColor,
foregroundColor: textColor ?? Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(borderRadius),
),
),
child: Text(text),
),
);
}
}
7.2 渐变按钮 #
dart
class GradientButton extends StatelessWidget {
final String text;
final VoidCallback? onPressed;
final List<Color> colors;
const GradientButton({
super.key,
required this.text,
this.onPressed,
this.colors = const [Colors.blue, Colors.purple],
});
@override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
gradient: LinearGradient(colors: colors),
borderRadius: BorderRadius.circular(8),
),
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: Text(
text,
style: TextStyle(color: Colors.white),
),
),
);
}
}
7.3 带加载状态的按钮 #
dart
class LoadingButton extends StatefulWidget {
final String text;
final Future<void> Function()? onPressed;
const LoadingButton({
super.key,
required this.text,
this.onPressed,
});
@override
State<LoadingButton> createState() => _LoadingButtonState();
}
class _LoadingButtonState extends State<LoadingButton> {
bool _isLoading = false;
Future<void> _handlePress() async {
if (_isLoading || widget.onPressed == null) return;
setState(() => _isLoading = true);
try {
await widget.onPressed!();
} finally {
if (mounted) {
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,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
)
: Text(widget.text),
);
}
}
八、按钮主题 #
8.1 全局主题 #
dart
ThemeData(
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
foregroundColor: Colors.blue,
),
),
outlinedButtonTheme: OutlinedButtonThemeData(
style: OutlinedButton.styleFrom(
foregroundColor: Colors.blue,
side: BorderSide(color: Colors.blue),
),
),
)
8.2 ButtonStyle #
dart
ButtonStyle({
MaterialStateProperty<TextStyle?>? textStyle,
MaterialStateProperty<Color?>? backgroundColor,
MaterialStateProperty<Color?>? foregroundColor,
MaterialStateProperty<Color?>? overlayColor,
MaterialStateProperty<Color?>? shadowColor,
MaterialStateProperty<double?>? elevation,
MaterialStateProperty<EdgeInsetsGeometry?>? padding,
MaterialStateProperty<Size?>? minimumSize,
MaterialStateProperty<Size?>? fixedSize,
MaterialStateProperty<Size?>? maximumSize,
MaterialStateProperty<BorderSide?>? side,
MaterialStateProperty<OutlinedBorder?>? shape,
MaterialStateProperty<MouseCursor?>? mouseCursor,
VisualDensity? visualDensity,
MaterialTapTargetSize? tapTargetSize,
Duration? animationDuration,
bool? enableFeedback,
MaterialStateProperty<AlignmentGeometry?>? alignment,
InteractiveInkFeatureFactory? splashFactory,
})
九、交互处理 #
9.1 GestureDetector #
dart
GestureDetector(
onTap: () {
print('Tapped');
},
onDoubleTap: () {
print('Double tapped');
},
onLongPress: () {
print('Long pressed');
},
onHorizontalDrag: (details) {
print('Horizontal drag: ${details.delta}');
},
onVerticalDrag: (details) {
print('Vertical drag: ${details.delta}');
},
child: Container(
width: 200,
height: 200,
color: Colors.blue,
child: Center(child: Text('Tap me')),
),
)
9.2 InkWell #
dart
InkWell(
onTap: () {
print('Tapped');
},
splashColor: Colors.blue.withOpacity(0.3),
highlightColor: Colors.blue.withOpacity(0.1),
borderRadius: BorderRadius.circular(8),
child: Container(
padding: EdgeInsets.all(16),
child: Text('InkWell'),
),
)
9.3 InkResponse #
dart
InkResponse(
onTap: () {
print('Tapped');
},
splashColor: Colors.blue,
highlightColor: Colors.blue.withOpacity(0.3),
radius: 50,
child: Icon(Icons.favorite, size: 32),
)
十、总结 #
10.1 按钮类型 #
| 按钮 | 说明 |
|---|---|
| ElevatedButton | 凸起按钮 |
| TextButton | 文本按钮 |
| OutlinedButton | 边框按钮 |
| IconButton | 图标按钮 |
| FloatingActionButton | 浮动按钮 |
10.2 下一步 #
掌握了按钮与交互后,让我们学习 布局基础!
最后更新:2026-03-28