dreampad/lib/app/shared/widgets/my_button.dart

106 lines
3.2 KiB
Dart
Raw Normal View History

2023-11-28 10:44:58 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../utils/theme_utils.dart';
import '../constants/constants.dart';
class MyButton extends StatelessWidget {
const MyButton({
super.key,
this.text = '',
this.textStyle,
this.textColor,
this.disabledTextColor,
this.backgroundColor,
this.disabledBackgroundColor,
this.icon,
this.minHeight = 45.0,
this.minWidth = double.infinity,
this.padding = 12.0,
this.radius = 9.0,
this.side = BorderSide.none,
this.statesController,
required this.onPressed,
});
final String text;
final TextStyle? textStyle;
final Color? textColor;
final Color? disabledTextColor;
final Color? backgroundColor;
final Color? disabledBackgroundColor;
final double? minHeight;
final double? minWidth;
final VoidCallback? onPressed;
final double padding;
final double radius;
final BorderSide side;
final Widget? icon;
final MaterialStatesController? statesController;
@override
Widget build(BuildContext context) {
final bool isDark = context.isDark;
return TextButton(
onPressed: onPressed,
statesController: statesController,
style: ButtonStyle(
// 文字颜色
foregroundColor: MaterialStateProperty.resolveWith(
(states) {
if (states.contains(MaterialState.disabled)) {
return disabledTextColor ??
(isDark ? Colours.dark_text_disabled : Colours.text_disabled);
}
return textColor ??
(isDark ? Colours.dark_button_text : Colours.button_text);
},
),
// 背景颜色
backgroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.disabled)) {
return disabledBackgroundColor ??
(isDark
? Colours.dark_button_disabled
: Colours.button_disabled);
}
return backgroundColor ??
(isDark ? Colours.dark_app_main : Colours.app_main);
}),
// 按钮最小大小
minimumSize: (minWidth == null || minHeight == null)
? null
: MaterialStateProperty.all<Size>(Size(minWidth!.w, minHeight!.h)),
padding: MaterialStateProperty.all<EdgeInsetsGeometry>(
REdgeInsets.symmetric(horizontal: padding)),
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(radius).r,
),
),
side: MaterialStateProperty.all<BorderSide>(side),
),
child: icon == null
? Text(
text,
style: textStyle,
)
: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
icon!,
Offstage(
offstage: text.isEmpty,
child: Gaps.hGap6,
),
Text(
text,
style: textStyle,
)
],
),
);
}
}