flutter开发实战-实现自定义按钮类似UIButton效果

这篇具有很好参考价值的文章主要介绍了flutter开发实战-实现自定义按钮类似UIButton效果。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

flutter开发实战-实现自定义按钮类似UIButton效果

最近开发过程中需要实现一下UIButton效果的flutter按钮,这里使用的是监听手势点击事件。

一、GestureDetector

GestureDetector属性定义

GestureDetector({
    super.key,
    this.child,
    this.onTapDown,
    this.onTapUp,
    this.onTap,
    this.onTapCancel,
    this.onSecondaryTap,
    this.onSecondaryTapDown,
    this.onSecondaryTapUp,
    this.onSecondaryTapCancel,
    this.onTertiaryTapDown,
    this.onTertiaryTapUp,
    this.onTertiaryTapCancel,
    this.onDoubleTapDown,
    this.onDoubleTap,
    this.onDoubleTapCancel,
    this.onLongPressDown,
    this.onLongPressCancel,
    this.onLongPress,
    this.onLongPressStart,
...

由于属性太多,我们实现onTapDown、onTapUp、onTapCancel、onTap。

二、实现flutter自定义按钮

实现自定义按钮类似,我们实现onTapDown、onTapUp、onTapCancel、onTap这几个方法

return GestureDetector(
      onTapDown: handleTapDown,
      // 处理按下事件
      onTapUp: handleTapUp,
      // 处理抬起事件
      onTap: handleTap,
      onTapCancel: handleTapCancel,
}

void handleTapDown(TapDownDetails details) {
    if (widget.enabled != null && widget.enabled == true) {
      setState(() {
        _highlighted = true;
      });
    }
  }

  void handleTapUp(TapUpDetails details) {
    setState(() {
      _highlighted = false;
    });
  }

  void handleTapCancel() {
    setState(() {
      _highlighted = false;
    });
  }

  void handleTap() {
    if (widget.enabled != null && widget.enabled == true) {
      setState(() {
        _highlighted = true;
      });
      Future.delayed(Duration(milliseconds: 100), () {
        if (mounted) {
          setState(() {
            _highlighted = false;
          });
        }
      });

      if (widget.enabled != null && widget.enabled == true) {
        widget.onPressed();
      }
    }
  }

完整代码如下

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

//枚举类的声明
enum ButtonAlignment { Center, Left, Right }

class ButtonWidget extends StatefulWidget {
  const ButtonWidget({
    Key? key,
    this.bgColor,
    this.bgHighlightedColor,
    this.color,
    this.highlightedColor,
    this.disableColor,
    this.bgDisableColor,
    this.width,
    this.height,
    this.borderRadius,
    this.buttonAlignment: ButtonAlignment.Center,
    this.text: "",
    this.textFontSize,
    this.icon,
    this.iconTextPadding,
    required this.onPressed,
    this.enabled = true,
    required this.child,
    this.border,
    this.padding,
  }) : super(key: key);

  final Color? bgColor; // 背景颜色
  final Color? bgHighlightedColor; // 背景点击高亮颜色
  final Color? color;
  final Color? highlightedColor;
  final Color? disableColor;
  final Color? bgDisableColor;
  final double? width;
  final double? height;
  final VoidCallback onPressed;
  final double? borderRadius;
  final ButtonAlignment? buttonAlignment;
  final String? text;
  final double? textFontSize;
  final Icon? icon;
  final double? iconTextPadding;
  final bool? enabled;
  final Widget child;
  final Border? border;
  final EdgeInsetsGeometry? padding;

  
  _ButtonWidgetState createState() => _ButtonWidgetState();
}

class _ButtonWidgetState extends State<ButtonWidget> {
  bool _highlighted = false;
  
  void initState() {
    // TODO: implement initState
    super.initState();
    _highlighted = false;
  }

  void handleTapDown(TapDownDetails details) {
    if (widget.enabled != null && widget.enabled == true) {
      setState(() {
        _highlighted = true;
      });
    }
  }

  void handleTapUp(TapUpDetails details) {
    setState(() {
      _highlighted = false;
    });
  }

  void handleTapCancel() {
    setState(() {
      _highlighted = false;
    });
  }

  void handleTap() {
    if (widget.enabled != null && widget.enabled == true) {
      setState(() {
        _highlighted = true;
      });
      Future.delayed(Duration(milliseconds: 100), () {
        if (mounted) {
          setState(() {
            _highlighted = false;
          });
        }
      });

      if (widget.enabled != null && widget.enabled == true) {
        widget.onPressed();
      }
    }
  }

  AlignmentGeometry showAlignment(ButtonAlignment? buttonAlignment) {
    AlignmentGeometry alignment = Alignment.center;
    if (buttonAlignment != null) {
      if (buttonAlignment == ButtonAlignment.Left) {
        alignment = Alignment.centerLeft;
      } else if (buttonAlignment == ButtonAlignment.Right) {
        alignment = Alignment.centerRight;
      } else {
        alignment = Alignment.center;
      }
    }

    return alignment;
  }

  
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: handleTapDown,
      // 处理按下事件
      onTapUp: handleTapUp,
      // 处理抬起事件
      onTap: handleTap,
      onTapCancel: handleTapCancel,
      child: Container(
        padding: widget.padding,
        width: widget.width,
        height: widget.height,
        alignment: showAlignment(widget.buttonAlignment),
        decoration: BoxDecoration(
          color: boxDecorationBgColor(),
          borderRadius: BorderRadius.circular(widget.borderRadius ?? 0),
          border: widget.border
        ),
        child: widget.child,
      ),
    );
  }

  Color? boxDecorationBgColor() {
    if (widget.enabled != null && widget.enabled == true) {
      return (_highlighted ? widget.bgHighlightedColor : widget.bgColor);
    }

    return widget.bgDisableColor ?? widget.bgColor;
  }

  Color? textColor() {
    if (widget.enabled != null && widget.enabled == true) {
      return (_highlighted ? widget.highlightedColor : widget.color);
    }

    return widget.disableColor ?? widget.bgColor;
  }
}

三、小结

flutter开发实战-实现自定义按钮类似UIButton效果,通过监听手势GestureDetector的onTapDown、onTapUp、onTapCancel、onTap来实现按下背景变换,松开背景恢复默认等效果。

学习记录,每天不停进步。文章来源地址https://www.toymoban.com/news/detail-629323.html

到了这里,关于flutter开发实战-实现自定义按钮类似UIButton效果的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • flutter开发实战-实现marquee根据文本长度显示文本跑马灯效果

    flutter开发实战-实现marquee文本跑马灯效果 最近开发过程中需要marquee文本跑马灯效果,这里使用到了flutter的插件marquee 效果图如下 1.1 引入marquee 在pubspec.yaml中引入marquee 1.2 marquee使用 marquee使用也是非常方便的。比如直接指定文本text 或者设置更多属性值 根据Text文本的大小判断

    2024年02月13日
    浏览(31)
  • flutter开发实战-Camera自定义相机拍照功能实现

    flutter开发实战-Camera自定义相机拍照功能实现 在项目中使用image_picker插件时候,在android设备上使用无法默认设置前置摄像头(暂时不清楚什么原因),由于项目默认需要使用前置摄像头,所以最终采用自定义相机实现拍照功能。 在工程的iOS的info.plist文件中添加相机、麦克风

    2024年02月21日
    浏览(37)
  • flutter开发实战-CustomClipper裁剪长图帧动画效果

    flutter开发实战-CustomClipper裁剪长图帧动画效果 在开发过程中,经常遇到帧动画的每一帧图显示在超长图上,需要处理这种帧动画效果。我这里使用的是CustomClipper CustomClipper继承于Listenable abstract class CustomClipper extends Listenable 我们实现CustomClipper子类来实现裁剪功能 getClip()是用

    2024年02月13日
    浏览(34)
  • flutter开发实战-svga播放svgaplayer_flutter直播礼物特效等效果使用

    flutter开发实战-svga播放svgaplayer_flutter直播礼物特效等效果使用 最近开发过程中用到了SVGA进行播放动画,这里记录一下svgaplayer_flutter使用过程。svga可以做一些非常精美的动画,包括直播的刷礼物(火箭、跑车特效动画)等等。 效果图如下 SVGA是什么呢? SVGA 是一种同时兼容 iOS、

    2024年02月16日
    浏览(29)
  • iOS按钮控件UIButton使用

    1.在故事板中添加按钮控件,步聚如下: 同时按钮Shift+Commad+L在出现在控件库中选择Button并拖入View Controller Scene中 将控件与变量btnSelect关联  关联后空心变实心  如何关联?直接到属性窗口拖按钮变量到控件上,出现一条线,然后松开,这样就关联成功了 关联成功后属性窗口如下,点

    2024年02月05日
    浏览(21)
  • 自定义类似微信效果Preference

    1. 为自定义Preference 添加背景:custom_preference_background.xml 2. 自定义layout: layout_custom_click_preference.xml 3. 自定义style: 4.自定义图标 5. 完整代码 6. 用法 7. 效果如图:

    2024年04月25日
    浏览(19)
  • flutter开发实战-自定义相机camera功能

    flutter开发实战-自定义相机camera功能。 Flutter 本质上只是一个 UI 框架,运行在宿主平台之上,Flutter 本身是无法提供一些系统能力,比如使用蓝牙、相机、GPS等,因此要在 Flutter 中调用这些能力就必须和原生平台进行通信。 实现相机功能,我们使用的是camera插件。 在pubspec.

    2024年02月15日
    浏览(34)
  • IOS 设置UIButton按钮的选中状态样式

    设置按钮的边框 设置按钮的文字样式 设置按钮的背景颜色 设置按钮的文字内容 附上按钮的各种状态及交互

    2024年04月27日
    浏览(26)
  • flutter开发实战-自定义Switch开关控件Widget

    flutter开发实战-自定义Switch开关控件 在flutter中实现自定义Switch,主要实现类似IOS的UISwitch样式的开关控件 实现自定义Switch的Widget,主要实现交织动画。 交织动画 有些时候我们可能会需要一些复杂的动画,这些动画可能由一个动画序列或重叠的动画组成。一个动画组合在不同

    2024年02月13日
    浏览(32)
  • flutter开发实战-webview自定义标题栏Appbar

    flutter开发实战-webview定义标题栏Appbar 在开发中,使用到webview,在之前实现webview使用,webview页面使用的时自定义标题栏,在上一个webview结合JsBridge实现交互忘记这个标题栏,这里记录一下。 flutter开发实战-webview定义标题栏Appbar,PreferredSizeWidget webview页面使用的时自定义标题

    2024年02月16日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包