| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- import 'package:flutter/material.dart';
- import 'package:font_awesome_flutter/font_awesome_flutter.dart';
- class FlutterFlowIconButton extends StatefulWidget {
- const FlutterFlowIconButton({
- Key? key,
- required this.icon,
- this.borderColor,
- this.borderRadius,
- this.borderWidth,
- this.buttonSize,
- this.fillColor,
- this.disabledColor,
- this.disabledIconColor,
- this.hoverColor,
- this.hoverIconColor,
- this.hoverBorderColor,
- this.onPressed,
- this.showLoadingIndicator = false,
- this.focusBorderSide,
- this.focusBorderRadius,
- }) : super(key: key);
- final Widget icon;
- final double? borderRadius;
- final double? buttonSize;
- final Color? fillColor;
- final Color? disabledColor;
- final Color? disabledIconColor;
- final Color? hoverColor;
- final Color? hoverIconColor;
- final Color? hoverBorderColor;
- final Color? borderColor;
- final double? borderWidth;
- final bool showLoadingIndicator;
- final Function()? onPressed;
- final BorderSide? focusBorderSide;
- final BorderRadius? focusBorderRadius;
- @override
- State<FlutterFlowIconButton> createState() => _FlutterFlowIconButtonState();
- }
- class _FlutterFlowIconButtonState extends State<FlutterFlowIconButton> {
- bool loading = false;
- late double? iconSize;
- late Color? iconColor;
- late Widget effectiveIcon;
- @override
- void initState() {
- super.initState();
- _updateIcon();
- }
- @override
- void didUpdateWidget(FlutterFlowIconButton oldWidget) {
- super.didUpdateWidget(oldWidget);
- _updateIcon();
- }
- void _updateIcon() {
- final isFontAwesome = widget.icon is FaIcon;
- if (isFontAwesome) {
- FaIcon icon = widget.icon as FaIcon;
- effectiveIcon = FaIcon(
- icon.icon,
- size: icon.size,
- );
- iconSize = icon.size;
- iconColor = icon.color;
- } else {
- Icon icon = widget.icon as Icon;
- effectiveIcon = Icon(
- icon.icon,
- size: icon.size,
- );
- iconSize = icon.size;
- iconColor = icon.color;
- }
- }
- @override
- Widget build(BuildContext context) {
- ButtonStyle style = ButtonStyle(
- shape: MaterialStateProperty.resolveWith<OutlinedBorder>(
- (states) {
- if (states.contains(MaterialState.hovered)) {
- return RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(widget.borderRadius ?? 0),
- side: BorderSide(
- color: widget.hoverBorderColor ??
- widget.borderColor ??
- Colors.transparent,
- width: widget.borderWidth ?? 0,
- ),
- );
- }
- if (states.contains(WidgetState.focused) &&
- widget.focusBorderSide != null) {
- return RoundedRectangleBorder(
- borderRadius:
- widget.focusBorderRadius ?? BorderRadius.circular(8),
- side: widget.focusBorderSide!,
- );
- }
- return RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(widget.borderRadius ?? 0),
- side: BorderSide(
- color: widget.borderColor ?? Colors.transparent,
- width: widget.borderWidth ?? 0,
- ),
- );
- },
- ),
- iconColor: MaterialStateProperty.resolveWith<Color?>(
- (states) {
- if (states.contains(MaterialState.disabled) &&
- widget.disabledIconColor != null) {
- return widget.disabledIconColor;
- }
- if (states.contains(MaterialState.hovered) &&
- widget.hoverIconColor != null) {
- return widget.hoverIconColor;
- }
- return iconColor;
- },
- ),
- backgroundColor: MaterialStateProperty.resolveWith<Color?>(
- (states) {
- if (states.contains(MaterialState.disabled) &&
- widget.disabledColor != null) {
- return widget.disabledColor;
- }
- if (states.contains(MaterialState.hovered) &&
- widget.hoverColor != null) {
- return widget.hoverColor;
- }
- return widget.fillColor;
- },
- ),
- overlayColor: MaterialStateProperty.resolveWith<Color?>((states) {
- if (states.contains(MaterialState.pressed)) {
- return null;
- }
- return widget.hoverColor == null ? null : Colors.transparent;
- }),
- );
- return SizedBox(
- width: widget.buttonSize,
- height: widget.buttonSize,
- child: Theme(
- data: ThemeData.from(
- colorScheme: Theme.of(context).colorScheme,
- useMaterial3: true,
- ),
- child: IgnorePointer(
- ignoring: (widget.showLoadingIndicator && loading),
- child: IconButton(
- icon: (widget.showLoadingIndicator && loading)
- ? Container(
- width: iconSize,
- height: iconSize,
- child: CircularProgressIndicator(
- valueColor: AlwaysStoppedAnimation<Color>(
- iconColor ?? Colors.white,
- ),
- ),
- )
- : effectiveIcon,
- onPressed: widget.onPressed == null
- ? null
- : () async {
- if (loading) {
- return;
- }
- setState(() => loading = true);
- try {
- await widget.onPressed!();
- } finally {
- if (mounted) {
- setState(() => loading = false);
- }
- }
- },
- splashRadius: widget.buttonSize,
- style: style,
- ),
- ),
- ),
- );
- }
- }
|