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

flutter自定义按钮-文本按钮

目录

前言

需求

实现


前言

最近闲着无聊学习了flutter的一下知识,发现flutter和安卓之间,页面开发的方式还是有较大的差异的,众所周知,android的页面开发都是写在xml文件中的,而flutter直接写在代码里(dart文件中),从目前我的认识来看,两者在UI上的“套娃”方式,flutter更能套,简直就是“套中套”啊哈哈。比如今天的手写一个最简单的自定义按钮吧,效果如下图所示

需求

  • 带文本的普通按钮,可修改字体大小和字体颜色
  • 提供修改背景色,按下后的背景色支持
  • 支持圆角和边框
  • 提供点击事件的回调

 这几个需求还是毕竟常用的,目前没考虑渐变色和图标,具体需求具体改吧

实现

本次demo的代码本身属于练手,相当于flutter的"hello world"(毕竟我才刚了解flutter没几天)

以下是具体的代码实现:

import 'package:flutter/material.dart';class CustomTextButton extends StatefulWidget {//按钮的宽度final double? width;//按钮的长度final double? height;final String text;final double? textSize;final Color textColor;final Color backgroundColor;final Color pressedBackgroundColor;final VoidCallback onClick;final double borderRadius;final Color borderColor;const CustomTextButton({super.key,required this.onClick,required this.text,this.textSize = 16,this.width = double.infinity,required this.height,this.backgroundColor = Colors.white,this.pressedBackgroundColor = Colors.white,this.borderRadius = 0.0,this.borderColor = Colors.white,this.textColor = Colors.black});@overrideState<CustomTextButton> createState() => _CustomTextButtonState();
}class _CustomTextButtonState extends State<CustomTextButton> {bool _isPressed = false;@overrideWidget build(BuildContext context) {return ConstrainedBox(constraints:BoxConstraints.expand(width: widget.width, height: widget.height),child: GestureDetector(onTap: () {widget.onClick();},onTapDown: (details) {setState(() {_isPressed = true;});},onTapUp: (details) {setState(() {_isPressed = false;});},child: Container(alignment: Alignment.center,decoration: BoxDecoration(color: _isPressed? widget.pressedBackgroundColor: widget.backgroundColor,borderRadius: BorderRadius.circular(widget.borderRadius),border: Border.fromBorderSide(BorderSide(width: 1, color: widget.borderColor))),child: Text(widget.text,maxLines: 1,overflow: TextOverflow.ellipsis,style: TextStyle(color: widget.textColor,fontSize: widget.textSize,fontStyle: FontStyle.normal,),),),),);}
}extension HexColor on Color {/// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".static Color fromHex(String hexString) {final buffer = StringBuffer();if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');buffer.write(hexString.replaceFirst('#', ''));return Color(int.parse(buffer.toString(), radix: 16));}}

代码演示,如何使用,这个还是非常简单易懂的,有什么需要改进的地方,也请大佬指示改进。

Container(margin: const EdgeInsets.only(left: 20, right: 20),child: CustomTextButton(text: '注册',textSize: 18,textColor: Colors.white,backgroundColor: HexColor.fromHex("F9AC00"),pressedBackgroundColor: HexColor.fromHex("E0CE32"),height: 44,borderRadius: 30,onClick: () {Fluttertoast.showToast(msg: "您按了注册",toastLength: Toast.LENGTH_SHORT,gravity: ToastGravity.BOTTOM,timeInSecForIosWeb: 1,backgroundColor: Colors.black12,textColor: Colors.black,fontSize: 14.0);},),),

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

相关文章:

  • 无涯教程-Android - CheckBox函数
  • [Go版]算法通关村第十五关青铜——用4KB内存寻找重复元素
  • OJ练习第159题——消灭怪物的最大数量
  • Prometheus-Rules(规则)
  • 打卡智能中国(六):村里出了“飞行员”
  • 自动化运维工具Ansible之playbooks剧本
  • K8S访问控制------认证(authentication )、授权(authorization )、准入控制(admission control )体系
  • 开开心心带你学习MySQL数据库之第三篇上
  • Mysql批量插入大量数据的方法
  • centos安装nginx实操记录(加安全配置)
  • 【中等】49. 字母异位词分组
  • Python3 条件控制
  • IDEA自定义模板
  • 【Unity3D】UI Toolkit简介
  • QT 界面相关操作
  • nestjs:docker build时执行npm install sharp提示downloading libvips socket hang up
  • 图像分类学习笔记(七)——MobileNet
  • ssm+vue宠物领养系统源码和论文
  • 阜时科技联合客户发布全固态激光雷达面阵SPAD芯片及雷达整机
  • leetcode 189. 轮转数组
  • 亚马逊广告收入突破百亿美元,有望成为下一个收入支柱来源?
  • MATLAB中isequal函数转化为C语言
  • 【MTK平台】根据kernel log分析wifi scan的时候流程
  • CVE-2023-23752:Joomla未授权访问漏洞复现
  • MATLAB中circshift函数转化为C语言
  • 浅谈React生命周期
  • 基于龙格-库塔算法优化的BP神经网络(预测应用) - 附代码
  • C++ 获取进程信息
  • 【Redis从头学-13】Redis哨兵模式解析以及搭建指南
  • 【个人笔记js的原型理解】