当前位置: 首页 > news >正文

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 initStatesuper.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来实现按下背景变换,松开背景恢复默认等效果。

学习记录,每天不停进步。

http://www.lryc.cn/news/108958.html

相关文章:

  • 深度优先搜索|1034, 1020, 1254
  • 都市信息供求网servlet+jsp新闻广告出售java源代码mysql
  • kubeadm init:failed to pull image registry.k8s.io/pause:3.6
  • 设计模式之简单工厂模式、工厂模式、抽象工厂模式
  • C# 控制台彩色深度打印 工具类
  • Pytorch Tensor维度变换方法
  • 微信小程序之点击文字文字自动转语音进行播放,微信小程序文字识别转语音播放
  • 主动学习、半监督学习、它们之间的区别?
  • linux快速安装Rabbitmq
  • spconv1.2.1库的编译与安装
  • java+springboot+mysql企业邮件管理系统
  • [CKA]考试之一个 Pod 封装多个容器
  • iphone备份用什么软件?好用的苹果数据备份工具推荐!
  • 一语道破 python 迭代器和生成器
  • 有哪些开源和非开源的项目管理工具?
  • 实战 01|「编写互动式界面」
  • 开源社区寻找八月创作之星!你准备好了吗~
  • appuploader不是开发者账号
  • MySQL - 10、其他命令
  • 输入框长度在XSS测试中如何绕过字符长度限制
  • JVM基础篇-直接内存
  • 【Java可执行命令】(十四)脚本执行工具jrunscript :在命令行环境下交互式执行一些简单的脚本或测试代码片段~
  • eclipse Java Editor Templates
  • vue SKU已知sku.tree算出sku.list类目值和id
  • error C4430 缺少类型说明符 - 假定为 int。注意 C++ 不支持默认 int
  • Embedding入门介绍以及为什么Embedding在大语言模型中很重要
  • 暑假刷题第20天--8/3
  • docker容器内的django启动celery任务队列
  • linux文件描述符fd
  • 【深度学习】各个开源库总结及实战-总目录