Flutter 自定义 ImageButton

Flutter 自定义 ImageButtonFlutter 自定义的简单 ImageButton 代码 import package flutter cupertino dart import package flutter material dart

转载请注明出处:https://blog.csdn.net/a/article/details/

前言

Flutter 自定义的简单 ImageButton。支持图片按钮下面添加标题。

截图

在这里插入图片描述

代码

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; /* * 常用图片按钮 */ class SimpleImageButton extends StatefulWidget { final String normalImage; final String pressedImage; final Function onPressed; final double width; final String title; const SimpleImageButton({ Key key, @required this.normalImage, @required this.pressedImage, @required this.onPressed, @required this.width, this.title, }) : super(key: key); @override State 
  
    createState() { // TODO: implement createState return _SimpleImageButtonState(); } } class _SimpleImageButtonState extends State 
   
     { @override Widget build(BuildContext context) { // TODO: implement build return ImageButton( normalImage: Image( image: AssetImage(widget.normalImage), width: widget.width, height: widget.width, ), pressedImage: Image( image: AssetImage(widget.pressedImage), width: widget.width, height: widget.width, ), title: widget.title == null ? '' : widget.title, //文本是否为空 normalStyle: TextStyle( color: Colors.white, fontSize: 14, decoration: TextDecoration.none), pressedStyle: TextStyle( color: Colors.white, fontSize: 14, decoration: TextDecoration.none), onPressed: widget.onPressed, ); } } /* * 图片 按钮 */ class ImageButton extends StatefulWidget { //常规状态 final Image normalImage; //按下状态 final Image pressedImage; //按钮文本 final String title; //常规文本TextStyle final TextStyle normalStyle; //按下文本TextStyle final TextStyle pressedStyle; //按下回调 final Function onPressed; //文本与图片之间的距离 final double padding; ImageButton({ Key key, @required this.normalImage, @required this.pressedImage, @required this.onPressed, this.title, this.normalStyle, this.pressedStyle, this.padding, }) : super(key: key); @override _ImageButtonState createState() { // TODO: implement createState return _ImageButtonState(); } } class _ImageButtonState extends State 
    
      { var isPressed = false; @override Widget build(BuildContext context) { // TODO: implement build double padding = widget.padding == null ? 5 : widget.padding; return GestureDetector( child: Column( mainAxisSize: MainAxisSize.min, children: 
     
       [ isPressed ? widget.pressedImage : widget.normalImage, //不同状态显示不同的Image widget.title.isNotEmpty ? Padding(padding: EdgeInsets.fromLTRB(0, padding, 0, 0)) : Container(), widget.title.isNotEmpty //文本是否为空 ? Text( widget.title, style: isPressed ? widget.pressedStyle : widget.normalStyle, ) : Container(), ], ), onTap: widget.onPressed, onTapDown: (d) { //按下,更改状态 setState(() { isPressed = true; }); }, onTapCancel: () { //取消,更改状态 setState(() { isPressed = false; }); }, onTapUp: (d) { //抬起,更改按下状态 setState(() { isPressed = false; }); }, ); } } 
      
     
    
  

调用

  • 大部分情况下直接调用SimpleImageButton即可。参数也十分简单:
const SimpleImageButton({ Key key, @required this.normalImage, @required this.pressedImage, @required this.onPressed, @required this.width, this.title, }) : super(key: key); 
  • ImageButton参数要稍微复杂:
 ImageButton({ Key key, @required this.normalImage, @required this.pressedImage, @required this.onPressed, this.title, this.normalStyle, this.pressedStyle, this.padding, }) : super(key: key); 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211017.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午11:23
下一篇 2026年3月18日 下午11:23


相关推荐

  • Android Toast的立即取消与显示「建议收藏」

    Android Toast的立即取消与显示「建议收藏」我们很多时候要用到Toast来提示消息或者输出内容,但是比较让人烦恼的是Toast它有一定的显示时间,虽然我们可以设置显示时长,但要达到立即消失的目的,还是要用到Cancel方法,下面就介绍一下它使用中的注意要点。在显示消息的时候,最好用变量来实现比较好控制。Toastmtoast;if(mtoast!=null){  mtoast.cancel();//注销之前显示的那条信息  mtoas…

    2025年11月4日
    5
  • [springboot] 异步开发之异步请求

    [springboot] 异步开发之异步请求何为异步请求在 Servlet3 0 之前 Servlet 采用 Thread Per Request 的方式处理请求 即每一次 Http 请求都由某一个线程从头到尾负责处理 如果一个请求需要进行 IO 操作 比如访问数据库 调用第三方服务接口等 那么其所对应的线程将同步地等待 IO 操作完成 而 IO 操作是非常慢的 所以此时的线程并不能及时地释放回线程池以供后续使用 在并发量越来越大的情况下 这将带来严重的性能

    2026年3月16日
    1
  • 一文彻底搞懂webpack devtool

    一文彻底搞懂webpack devtool为什么需要SourceMap首先根据谷歌开发者文档的介绍,SourceMap一般与下列类型的预处理器搭配使用:转译器(Babel) 编译器(TypeScript) Minifiers(UglifyJS)为什么呢?因为通常我们运行在浏览器中的代码是经过处理的,处理后的代码可能与开发时代码相差很远,这就导致开发调试和线上排错变得困难。这时SourceMap就登场了,有了它浏览器就可以从转换后的代码直接定位到转换前的代码。在webpack中,可以通过devtool选项来配置SourceMap

    2026年4月15日
    6
  • CAS原理图_cas机制原理

    CAS原理图_cas机制原理cas原理流程图

    2022年8月31日
    7
  • 【转录调控网络】典型的基因转录调控网络推导方法——布尔网络

    【转录调控网络】典型的基因转录调控网络推导方法——布尔网络基因转录调控网络推导方法 布尔网络基因网络是由细胞中参与基因调控作用的 DNA RNA 蛋白质以及代谢中间物所形成的相互作用的网络 基因网络是从分子层次上对生物系统进行研究的 其研究目标是通过基因之间的相互作用从系统的角度全面说明基因组的功能和行为以揭示复杂的生命现象 基因网络有助于从基因组层次对生命过程进行详细的解释 从而达到系统地解释细胞活动 生命活动 解释疾病的发生 发展和治疗等目标 基因网络是基因组学研究的重要内容 也是当前生物学研究的前沿 因此在研究生物体的生长 发育以及疾病等过程方面受

    2026年3月16日
    2
  • Ps如何无痕修改图片上的文字内容?

    Ps如何无痕修改图片上的文字内容?

    2026年3月13日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号