首页 > 其他分享 >一统天下 flutter - widget 按钮类: TextButton - 文本按钮

一统天下 flutter - widget 按钮类: TextButton - 文本按钮

时间:2023-03-22 10:33:30浏览次数:59  
标签:widget const TextButton style MaterialStateProperty Colors 按钮 side

一统天下 flutter https://github.com/webabcd/flutter_demo
作者 webabcd

一统天下 flutter - widget 按钮类: TextButton - 文本按钮

示例如下:

lib\widget\button\text_button.dart

/*
 * TextButton - 文本按钮
 *
 * TextButton 是默认啥都没有的按钮,OutlinedButton 是默认有边框的按钮,ElevatedButton 是默认有背景色的按钮
 */

import 'package:flutter/material.dart';
import 'package:flutter_demo/helper.dart';

class TextButtonDemo extends StatefulWidget {
  const TextButtonDemo({Key? key}) : super(key: key);

  @override
  _TextButtonDemoState createState() => _TextButtonDemoState();
}

class _TextButtonDemoState extends State<TextButtonDemo> {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        TextButton(
          child: const Text("button"),            /// 文本按钮上显示的文本
          autofocus: false,                       /// 是否自动获取焦点
          onPressed: () { log("onPressed"); },    /// 单击事件,此为 required 字段(设置为 null 则代表按钮是 disable 状态)
          onLongPress: () { log("onLongPress"); },/// 长按事件
          style: TextButton.styleFrom(            /// 样式,通过 TextButton.styleFrom() 实现,如需要为按钮的不同状态指定不同的样式请参见后面的 ButtonStyle() 中的说明
            padding: const EdgeInsets.all(2),     ///   内边距
            foregroundColor: Colors.white,        ///   按钮文字颜色
            backgroundColor: Colors.blue,         ///   按钮背景颜色
            alignment: Alignment.center,          ///   按钮文字相对于按钮整体的对齐方式
            minimumSize: const Size(200, 0),      ///   最小尺寸
            maximumSize: const Size(1000, 40),    ///   最大尺寸
            fixedSize: const Size(0, 100),        ///   按钮尺寸,受 minimumSize 和 maximumSize 的限制
            textStyle: const TextStyle(           ///   按钮文字样式
                fontSize: 14,
                decoration: TextDecoration.none
            ),
            side: const BorderSide(               ///   按钮边框的大小和颜色
                color: Colors.red,
                width: 2,
            ),
            shape: const StadiumBorder(           ///   按钮的边框的样式(注:style 的 side 的优先级比 style 的 shape 的 side 的优先级要高)
              side: BorderSide(                   ///   由于本例指定了 style 的 side,所以此处的 style 的 shape 的 side 是无效的
                width: 100,
                color: Colors.green
              ),
            ),
          ),
        ),
        TextButton(
          child: const Text("button"),
          onPressed: () { },
          style: ButtonStyle(                                             /// 样式,通过 ButtonStyle() 实现,可以为按钮的不同状态指定不同的样式
            /// MaterialStateProperty - 用于为不同的状态设置不同的值
            backgroundColor: MaterialStateProperty.resolveWith((states) { ///   按钮背景颜色
              /// 按钮被单击时的背景色
              if (states.contains(MaterialState.pressed)) {
                return Colors.green;
              }
              /// 正常状态的背景色
              return Colors.blue;
            }),
            foregroundColor: MaterialStateProperty.resolveWith((states) { ///   按钮的文字的颜色
              if (states.contains(MaterialState.pressed)) {
                return Colors.red;
              }
              return Colors.white;
            }),
            textStyle: MaterialStateProperty.resolveWith((states) {       ///   按钮的文字的样式(注:在此处设置文字颜色是无效的,请通过 foregroundColor 设置)
              if (states.contains(MaterialState.pressed)) {
                return TextStyle(fontSize: 48);
              }
              return TextStyle(fontSize: 24);
            }),
            overlayColor: MaterialStateProperty.resolveWith((states) {    ///   按钮被按下时,一个点击动画效果的颜色
              if (states.contains(MaterialState.pressed)) {
                return Colors.purple;
              }
              return null;
            }),
            shadowColor: MaterialStateProperty.all(Colors.orange),        ///   阴影的颜色
            elevation: MaterialStateProperty.all(10),                     ///   阴影的垂直方向的偏移量
            alignment: Alignment.center,                                  ///   按钮文字相对于按钮整体的对齐方式
            animationDuration: Duration(milliseconds: 200)                ///   按钮的各种状态转换时的动画效果的时长
          ),
        ),
        TextButton(
          child: const Text("button"),
          onPressed: () { },
          style: ButtonStyle(
            padding: MaterialStateProperty.all(EdgeInsets.all(5)),        ///   内边距
            minimumSize: MaterialStateProperty.all(Size(200, 0)),         ///   最小尺寸
            maximumSize: MaterialStateProperty.all(Size(1000, 40)),       ///   最大尺寸
            fixedSize: MaterialStateProperty.all(Size(0, 100)),           ///   按钮尺寸,受 minimumSize 和 maximumSize 的限制
            side: MaterialStateProperty.all(                              ///   按钮边框的大小和颜色
              BorderSide(
                color: Colors.red,
                width: 2
              )
            ),
            shape: MaterialStateProperty.all(                             ///   按钮的边框的样式(注:style 的 side 的优先级比 style 的 shape 的 side 的优先级要高)
              const StadiumBorder(
                side: BorderSide(                                         ///   由于本例指定了 style 的 side,所以此处的 style 的 shape 的 side 是无效的
                  width: 100,
                  color: Colors.green
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

一统天下 flutter https://github.com/webabcd/flutter_demo
作者 webabcd

标签:widget,const,TextButton,style,MaterialStateProperty,Colors,按钮,side
From: https://www.cnblogs.com/webabcd/p/flutter_lib_widget_button_text_button.html

相关文章

  • 一统天下 flutter - widget 按钮类: OutlinedButton - 自带边框按钮
    一统天下flutterhttps://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget按钮类:OutlinedButton-自带边框按钮示例如下:lib\widget\bu......
  • fastadmin 添加自定义按钮
    更新1.index.html2.JS文件3.require-table.js文件......
  • Fastadmin 列表自定义按钮
    Fastadmin列表自定义按钮FastAdmin是一款基于ThinkPHP+Bootstrap的极速后台开发框架。文章目录前言一、单纯的调用接口按钮二、打开新的弹窗页面总结前言Fastadmin列表......
  • fastadmin页面执行js方法(点击按钮出现弹窗为例)
    fastadmin页面执行js方法(点击按钮出现弹窗为例)雯0609~于2023-01-0414:56:00发布339收藏文章标签:javascript前端html版权例子:在页面设置按钮,点击按钮出现alert弹......
  • 02--Qt按钮与窗口
    创建窗口首先需要创建一个程序,该程序中.cpp文件添加以下代码 //修改窗口大小的标题(第一个窗口) this->setWindowTitle("第一个窗口");//设置窗口的大小,设置完成......
  • TabWidget/TabHost的两种使用方法
    AndroidTabWidget/TabHost有两种使用方法:第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:<?xmlversion="1.0"encoding="u......
  • css实现3D弹性按钮以及box-shadow特性说明
    box-shadow在实现案例之前先了解css的阴影属性box-shadow,该属性可以为盒子设置阴影,它有五个参数,X轴偏移量、Y轴偏移量、模糊半径、扩散半径和颜色。box-shadow文档:https:......
  • fastadmin 自定义build_toolbar按钮
    fastadmin自定义build_toolbar按钮何渊渊于2020-09-2311:13:31发布1930收藏4分类专栏:fastadmin文章文章标签:javascriptphp版权fastadmin同时被2个专栏收......
  • 【Android开发】范例1-实现跟踪鼠标单击状态的图片按钮
    在Android中实现手指点击图片按钮与不点击图片按钮的时候,按钮的颜色或样式不一样,给用户一种按钮被按下并切换的感觉,提高用户体验。实现效果如图:手指......
  • unity 在toolbar处添加按钮
      usingSystem;usingSystem.Reflection;usingUnityEngine;usingUnityEditor;usingUnityEngine.UIElements;[InitializeOnLoad]publicstaticclassCruT......