首页 > 其他分享 >15、Flutter 按钮组件

15、Flutter 按钮组件

时间:2023-11-21 13:56:28浏览次数:29  
标签:Widget const onPressed 按钮 child 15 Flutter 255

按钮组件的属性

ButtonStylee里面的常用的参数

 

ElevatedButton

ElevatedButton 即"凸起"按钮,它默认带有阴影和灰色背景。按下后,阴影会变大
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ElevatedButton(
        onPressed: (){},
        child:  Text("普通按钮"),)
    );
  }
}

TextButton

TextButton 即文本按钮,默认背景透明并不带阴影。按下后,会有背景色
class MyApp1 extends StatelessWidget {
  const MyApp1({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextButton(
        onPressed: (){},
        child:  Text("文本按钮"),)
    );
  }
}

OutlinedButton

OutlineButton 默认有一个边框,不带阴影且背景透明。按下后,边框颜色会变亮、同时出现背景和 阴影
class MyApp2 extends StatelessWidget {
  const MyApp2({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: OutlinedButton(
        onPressed: (){},
        child:  Text("边框按钮"),)
    );
  }
}

IconButton

IconButton 是一个可点击的Icon,不包括文字,默认没有背景,点击后会出现背景
class MyApp3 extends StatelessWidget {
  const MyApp3({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: IconButton(
        onPressed: (){},
        icon:  Icon(Icons.abc_rounded))
    );
  }
}

带图标的按钮

ElevatedButton 、 TextButton 、 OutlineButton 都有一个 icon 构造函数,通过它可以轻松创建 带图标的按钮
class MyApp4 extends StatelessWidget {
  const MyApp4({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(children: [
      ElevatedButton.icon(
        icon: Icon(Icons.send),
        label: Text("发送"),
        onPressed: () {},
      ),
      TextButton.icon(
          onPressed: () {},
          icon: Icon(Icons.access_alarm_outlined),
          label: Text("确定")),
      OutlinedButton.icon(
          onPressed: () {},
          icon: Icon(Icons.access_time_filled_sharp),
          label: Text("增加"))
    ]);
  }
}

修改按钮的宽度高度

class MyApp5 extends StatelessWidget {
  const MyApp5({super.key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 80,
      width: 200,
      child: ElevatedButton(
        style: ButtonStyle(
            backgroundColor: MaterialStateProperty.all(const Color.fromARGB(255, 86, 244, 54)), //按扭背景颜色
            foregroundColor: MaterialStateProperty.all(Colors.black)), //字体颜色
        onPressed: () {},
        child: Text("宽度高度"),
      ),
    );
  }
}

自适应按钮

class MyApp7 extends StatelessWidget {
  const MyApp7({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Container(
              color: Colors.green,
              height: 80,
margin: EdgeInsets.all(10), child: ElevatedButton( child: const Text('自适应按钮'), onPressed: () { print("自适应按钮"); })), ) ], ); } }

配置圆形圆角按钮

圆角按钮
class MyApp8 extends StatelessWidget {
  const MyApp8({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
        child: ElevatedButton(
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(
                  const Color.fromARGB(255, 33, 243, 61)), //背景颜色
              foregroundColor: MaterialStateProperty.all(
                  const Color.fromARGB(255, 255, 255, 255)), //字体或者图标颜色
              elevation: MaterialStateProperty.all(24), //是用来设置Material或Widget的阴影效果的  elevation的值是介于0到24之间的浮点数
              shape: MaterialStateProperty.all( //圆角
                RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10))),
            ),
            onPressed: () {
              print("圆角按钮");
            },
            child: const Text('圆角')));
  }
}
圆形按钮
class MyApp8 extends StatelessWidget {
  const MyApp8({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 80,
        child: ElevatedButton(
            style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(
                    const Color.fromARGB(255, 33, 243, 61)), //按钮背景颜色
                foregroundColor: MaterialStateProperty.all(
                    const Color.fromARGB(255, 255, 255, 255)), //字体或者图标颜色
                elevation: MaterialStateProperty.all(
                    24), //是用来设置Material或Widget的阴影效果的  elevation的值是介于0到24之间的浮点数
                shape: MaterialStateProperty.all(
                  CircleBorder(side: BorderSide(color: Colors.white)),
                )),
            onPressed: () {
              print("圆形按钮");
            },
            child: const Text('圆形按钮')));
  }
}

修改OutlinedButton边框

class MyApp9 extends StatelessWidget {
  const MyApp9({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Container(
            margin: EdgeInsets.all(20),
            height: 50,
            child: OutlinedButton(
                style: ButtonStyle(
                    foregroundColor: MaterialStateProperty.all(Colors.black),
                    side: MaterialStateProperty.all(  //边框颜色
                        const BorderSide(width: 1, color: Color.fromARGB(255, 54, 244, 95)))),
                onPressed: () {},
                child: const Text("注册 配置边框")),
          ),
        )
      ],
    );
  }
}

 

标签:Widget,const,onPressed,按钮,child,15,Flutter,255
From: https://www.cnblogs.com/xbinbin/p/17843566.html

相关文章

  • CMU15-445 project1 extendible_hash_table
    extendible_hash_tablehttps://zhuanlan.zhihu.com/p/622221722这篇文章讲了extendible_hash_table的数据插入、删除、查找的过程,看完之后可以了解global_depthlocal_depth是干什么的。简单来说,global_depth和local_depth的作用和掩码是类似的,代表目前只考虑哈希值的几个低......
  • wpf 自定义按钮模板
    <ButtonWidth="300"Height="100"Content="自定义按钮"Background="Bisque"FontSize="23"Foreground="Orchid"><Button.Template><ControlTemplateTargetType=&qu......
  • 去掉uniapp程序中顶部返回按钮
    去掉uniapp程序中顶部返回按钮1.javascript"autoBackButton":false在小程序下生效,H5不生效{"path":"pages/donation/list","style":{"navigationBarTitleText":"公益捐款"......
  • 世微 电动车摩托车灯 5-80V 1.2A 一切二降压恒流驱动器AP2915
    产品描述    AP2915是一款可以一路灯串切换两路灯串的降压恒流驱动器,高效率、外围简单、内置功率管,适用于5-80V输入的高精度降压LED恒流驱动芯片。内置功率管输出最大功率可达12W,最大电流1.2A。AP2915一路灯亮切换两路灯亮,其中一路灯亮可以全亮,可以半亮。AP2915......
  • UVA11526 H(n)
    题意求\(\sum_{i=1}^{n}\lfloor\frac{n}{i}\rfloor\)Sol整除分块。考虑\(1\ton\)里面固然有很多算重的。考虑去掉重复计算的东西,不难发现一个块内最大的数显然为\(\lfloor\frac{n}{\lfloor\frac{n}{i}\rfloor}\rfloor\)。实现是\(trivial\)的。Code#in......
  • 手机直播源码,Flutter 中的弹簧按钮效果
    手机直播源码,Flutter中的弹簧按钮效果import'package:flutter/material.dart'; classScaleAnimationextendsStatefulWidget   {  finalWidgetchild;  finalFunction()?onTap;  ScaleAnimation({requiredthis.child,requiredthis.onTap,Key?key}):supe......
  • 14、Flutter Card组件
    Card是卡片组件块,内容可以由大多数类型的Widget构成,Card具有圆角和阴影,这让它看起来有立体感。Card实现一个通讯录的卡片classMyApp2extendsStatelessWidget{constMyApp2({super.key});@overrideWidgetbuild(BuildContextcontext){returnListView(......
  • 【2023-11-15】亲情最美
    20:00年轻的时候以为不读书不足以了解人生,直到后来才发现如果不了解人生,是读不懂书的。读书的意义大概就是用生活所感去读书,用读书所得去生活吧。                                        ......
  • 11.15 监控目录文件变化
    监视对指定目录的更改,并将有关更改的信息打印到控制台,该功能的实现不仅可以在内核层,在应用层同样可以。程序中使用ReadDirectoryChangesW函数来监视目录中的更改,并使用FILE_NOTIFY_INFORMATION结构来获取有关更改的信息。ReadDirectoryChangesW是Windows操作系统提供的一个函数,......
  • 2023-2024-1 20231415 《计算机基础与程序设计》第八周学习总结
    这个作业属于哪个课程https://edu.cnblogs.com/campus/besti/2023-2024-1-CFAP/这个作业要求是什么https://www.cnblogs.com/rocedu/p/9577842.html#WEEK01作业目标自学教材《计算机科学概论》第9章,《C语言程序设计》第7章并完成云班课测试作业正文https://i.cnbl......