Material 组件库中有多种按钮组件如ElevatedButton、TextButton、OutlineButton等,它们的父类是于ButtonStyleButton。
基本的按钮特点:
1.按下时都会有“水波文动画”。
2.onPressed属性设置点击回调,如果不提供该回调则按钮会处于禁用状态,禁用状态不响应用户点击。
ElevatedButton
简单实现
const ElevatedButton({
super.key,
required super.onPressed,
super.onLongPress,
super.onHover,
super.onFocusChange,
super.style,
super.focusNode,
super.autofocus = false,
super.clipBehavior,
super.statesController,
required super.child,
super.iconAlignment,
});
import 'package:flutter/material.dart';
class ButtonWidgetPage extends StatelessWidget {
const ButtonWidgetPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Button Demo"),
),
body: Container(
alignment: Alignment.center,
margin: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text("TEST Button"),
ElevatedButton(
onPressed: () {
print('ElevatedButton clicked');
},
child: Text("Click ElevatedButton"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
elevation: 10,
minimumSize: Size(150, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10))),
)
],
),
),
);
}
}
对上面的属性做简单介绍:
- child: Text("Click ElevatedButton")设置按钮显示的文本为“Click ElevatedButton”。
- onPressed:设置按钮点击事件。
- style:使用ElevatedButton.styleFrom方法设置按钮的样式,包括背景色、文本颜色、阴影效果、最小宽度和形状等。
2024-09-07 15:44:54.993 12985-13050 flutter I ElevatedButton clicked
2024-09-07 15:44:56.418 12985-13050 flutter I ElevatedButton clicked
2024-09-07 15:44:57.601 12985-13050 flutter I ElevatedButton clicked
2024-09-07 15:44:58.044 12985-13050 flutter I ElevatedButton clicked
2024-09-07 15:47:37.981 12985-13050 flutter I ElevatedButton clicked
static ButtonStyle styleFrom({
Color? foregroundColor,
Color? backgroundColor,
Color? disabledForegroundColor,
Color? disabledBackgroundColor,
Color? shadowColor,
Color? surfaceTintColor,
Color? iconColor,
Color? disabledIconColor,
Color? overlayColor,
double? elevation,
TextStyle? textStyle,
EdgeInsetsGeometry? padding,
Size? minimumSize,
Size? fixedSize,
Size? maximumSize,
BorderSide? side,
OutlinedBorder? shape,
MouseCursor? enabledMouseCursor,
MouseCursor? disabledMouseCursor,
VisualDensity? visualDensity,
MaterialTapTargetSize? tapTargetSize,
Duration? animationDuration,
bool? enableFeedback,
AlignmentGeometry? alignment,
InteractiveInkFeatureFactory? splashFactory,
ButtonLayerBuilder? backgroundBuilder,
ButtonLayerBuilder? foregroundBuilder,
})
去除去掉ElevatedButton边距
import 'package:flutter/material.dart';
class ButtonWidgetPage extends StatelessWidget {
const ButtonWidgetPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Button Demo"),
),
body: Container(
alignment: Alignment.center,
margin: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text("TEST Button"),
ElevatedButton(
onPressed: () {
print('ElevatedButton clicked');
},
child: Text("Click ElevatedButton"),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
elevation: 10,
minimumSize: Size.zero,
padding: EdgeInsets.zero,
tapTargetSiz