• flutter自定义按钮-文本按钮


    目录

    前言

    需求

    实现


    前言

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

    需求

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

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

    实现

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

    以下是具体的代码实现:

    1. import 'package:flutter/material.dart';
    2. class CustomTextButton extends StatefulWidget {
    3. //按钮的宽度
    4. final double? width;
    5. //按钮的长度
    6. final double? height;
    7. final String text;
    8. final double? textSize;
    9. final Color textColor;
    10. final Color backgroundColor;
    11. final Color pressedBackgroundColor;
    12. final VoidCallback onClick;
    13. final double borderRadius;
    14. final Color borderColor;
    15. const CustomTextButton(
    16. {super.key,
    17. required this.onClick,
    18. required this.text,
    19. this.textSize = 16,
    20. this.width = double.infinity,
    21. required this.height,
    22. this.backgroundColor = Colors.white,
    23. this.pressedBackgroundColor = Colors.white,
    24. this.borderRadius = 0.0,
    25. this.borderColor = Colors.white,
    26. this.textColor = Colors.black});
    27. @override
    28. State createState() => _CustomTextButtonState();
    29. }
    30. class _CustomTextButtonState extends State<CustomTextButton> {
    31. bool _isPressed = false;
    32. @override
    33. Widget build(BuildContext context) {
    34. return ConstrainedBox(
    35. constraints:
    36. BoxConstraints.expand(width: widget.width, height: widget.height),
    37. child: GestureDetector(
    38. onTap: () {
    39. widget.onClick();
    40. },
    41. onTapDown: (details) {
    42. setState(() {
    43. _isPressed = true;
    44. });
    45. },
    46. onTapUp: (details) {
    47. setState(() {
    48. _isPressed = false;
    49. });
    50. },
    51. child: Container(
    52. alignment: Alignment.center,
    53. decoration: BoxDecoration(
    54. color: _isPressed
    55. ? widget.pressedBackgroundColor
    56. : widget.backgroundColor,
    57. borderRadius: BorderRadius.circular(widget.borderRadius),
    58. border: Border.fromBorderSide(
    59. BorderSide(width: 1, color: widget.borderColor))),
    60. child: Text(
    61. widget.text,
    62. maxLines: 1,
    63. overflow: TextOverflow.ellipsis,
    64. style: TextStyle(
    65. color: widget.textColor,
    66. fontSize: widget.textSize,
    67. fontStyle: FontStyle.normal,
    68. ),
    69. ),
    70. ),
    71. ),
    72. );
    73. }
    74. }
    75. extension HexColor on Color {
    76. /// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
    77. static Color fromHex(String hexString) {
    78. final buffer = StringBuffer();
    79. if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    80. buffer.write(hexString.replaceFirst('#', ''));
    81. return Color(int.parse(buffer.toString(), radix: 16));
    82. }
    83. }

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

    1. Container(
    2. margin: const EdgeInsets.only(left: 20, right: 20),
    3. child: CustomTextButton(
    4. text: '注册',
    5. textSize: 18,
    6. textColor: Colors.white,
    7. backgroundColor: HexColor.fromHex("F9AC00"),
    8. pressedBackgroundColor: HexColor.fromHex("E0CE32"),
    9. height: 44,
    10. borderRadius: 30,
    11. onClick: () {
    12. Fluttertoast.showToast(
    13. msg: "您按了注册",
    14. toastLength: Toast.LENGTH_SHORT,
    15. gravity: ToastGravity.BOTTOM,
    16. timeInSecForIosWeb: 1,
    17. backgroundColor: Colors.black12,
    18. textColor: Colors.black,
    19. fontSize: 14.0);
    20. },
    21. ),
    22. ),

  • 相关阅读:
    套接字选项
    操作系统导论-第四章作业(待更)
    Node.js 零基础入门 Node.js 零基础入门第二天 2.4 模块的加载机制
    GitHub -- 新增 SSH 密钥到 GitHub 帐户
    Python:基于dlib,numpy进行换脸实践
    Android Sutdio依赖Snapshot版本,无法同步最新的包
    统计信号处理基础 习题解答11-6
    python日志管理
    selenium学习
    110 个主流 Java 组件和框架整理,常用的都有,建议收藏!!
  • 原文地址:https://blog.csdn.net/sunjundelove/article/details/132602924