123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import 'package:flutter/material.dart';
- import 'package:widgets/ext/ex_widget.dart';
- import 'utils/dark_theme_util.dart';
- import 'package:shared/utils/ext_dart.dart';
- // ignore: slash_for_doc_comments
- /**
- MyButton(
- fontSize: 16,
- textColor: Colors.black,
- text: "My Button的封装按钮",
- backgroundColor: ColorConstants.gray,
- onPressed: () {
- SmartDialog.compatible.showToast("MyButton的封装按钮");
- },
- radius: 15,
- side: BorderSide(color: Colors.black,width: 1.0),
- ),
- */
- class MyButton extends StatelessWidget {
- const MyButton({
- Key? key,
- required this.onPressed,
- this.text = '',
- this.fontSize = 16,
- this.textColor,
- this.disabledTextColor,
- this.backgroundColor,
- this.disabledBackgroundColor,
- this.minHeight = 43.0,
- this.minWidth = double.infinity,
- this.padding = const EdgeInsets.symmetric(horizontal: 16.0),
- this.radius = 5.0,
- this.enableOverlay = true,
- this.elevation = 0.0,
- this.shadowColor = Colors.black,
- this.side = BorderSide.none,
- this.fontWeight,
- this.type = ClickType.none,
- this.milliseconds = 500,
- this.isEnabled = true, // 控制按钮是否可用
- }) : super(key: key);
- final String text;
- final double fontSize;
- final Color? textColor;
- final Color? disabledTextColor;
- final Color? backgroundColor;
- final Color? disabledBackgroundColor;
- final double? minHeight;
- final double? minWidth;
- final VoidCallback? onPressed;
- final EdgeInsetsGeometry padding;
- final double radius;
- final BorderSide side;
- final bool enableOverlay;
- final double elevation;
- final Color? shadowColor;
- final FontWeight? fontWeight;
- final ClickType type;
- final int milliseconds;
- final bool isEnabled; // 控制按钮是否可用
- @override
- Widget build(BuildContext context) {
- return TextButton(
- onPressed: isEnabled
- ? (type == ClickType.debounce
- ? onPressed?.debounce(milliseconds)
- : type == ClickType.throttle
- ? onPressed?.throttle(milliseconds)
- : onPressed)
- : null, // 如果按钮不可用,则 onPressed 设为 null
- style: ButtonStyle(
- foregroundColor: MaterialStateProperty.resolveWith(
- (states) {
- if (states.contains(MaterialState.disabled)) {
- return DarkThemeUtil.multiColors(
- context, disabledTextColor ?? Colors.grey,
- darkColor: Colors.grey);
- }
- return DarkThemeUtil.multiColors(
- context, textColor ?? Colors.white,
- darkColor: Colors.white);
- },
- ),
- backgroundColor: MaterialStateProperty.resolveWith((states) {
- if (states.contains(MaterialState.disabled)) {
- return disabledBackgroundColor;
- }
- return backgroundColor;
- }),
- overlayColor: MaterialStateProperty.resolveWith((states) {
- return enableOverlay
- ? DarkThemeUtil.multiColors(context, textColor ?? Colors.white)
- ?.withOpacity(0.12)
- : Colors.transparent;
- }),
- minimumSize: (minWidth == null || minHeight == null)
- ? null
- : MaterialStateProperty.all<Size>(
- Size(minWidth!, minHeight!)),
- padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
- shape: MaterialStateProperty.all<OutlinedBorder>(
- RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(radius),
- ),
- ),
- side: MaterialStateProperty.all<BorderSide>(side),
- elevation: MaterialStateProperty.all<double>(elevation),
- shadowColor: MaterialStateProperty.all<Color>(
- DarkThemeUtil.multiColors(
- context, shadowColor ?? Colors.black,
- darkColor: Colors.white)!),
- ),
- child: Text(
- text,
- style: TextStyle(
- fontSize: fontSize, fontWeight: fontWeight ?? FontWeight.w400),
- ));
- }
- }
|