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

【Flutter】AspectRatio组件Card组件按钮组件Wrap组件

🔥 本文由 程序喵正在路上 原创,CSDN首发!
💖 系列专栏:Flutter学习
🌠 首发时间:2024年5月25日
🦋 欢迎关注🖱点赞👍收藏🌟留言🐾

目录

  • AspectRatio组件
  • Card组件
    • Card实现一个通讯录的卡片
    • Card实现一个图文列表
  • 按钮组件
    • 按钮组件的属性
    • ElevatedButton、TextButton、OutlinedButton、IconButton
    • 带图标的按钮
    • 修改按钮的宽度高度
    • 自适应按钮
    • 配置圆角圆形按钮
    • 修改OutlinedButton边框
  • Wrap组件
    • 自定义一个按钮组件
    • Wrap组件的简单使用
    • Wrap组件搜索页面布局

AspectRatio组件

AspectRatio 的作用是根据设置调整子元素 child 的宽高比。

AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widget 的高度是由宽度和比率决定的,类似于 BoxFit 中的 contain,按照固定比率去尽量占满区域。

如果在满足所有限制条件过后无法找到一个可行的尺寸,AspectRatio 最终将会去优先适应布局限制条件,而忽略所设置的比率。

属性说明
aspectRatio宽高比,最终可能不会根据这个值去布局,具体要看综合因素,外层是否允许按照这种比率进行布局,这只是一个参考值
chlid子组件
class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return AspectRatio(aspectRatio: 3 / 1, //宽度为屏幕的宽度,高度为宽度的三分之一child: Container(color: Colors.blue,));}
}

在这里插入图片描述

Card组件

Card 是卡片组件块,内容可以由大多数类型的 Widget 构成,Card 具有圆角和阴影,这让它看起来有立体感。

属性说明
margin外边距
child子组件
elevation阴影值的深度
color背景颜色
shadowColor阴影颜色
clipBehavior内容溢出的剪切方式,Clip.none:不剪切;Clip.hardEdge:剪切但不应用抗锯齿;Clip.antiAlias:剪切而且抗锯齿;Clip.antiAliasWithSaveLayer:带有抗锯齿的剪辑,并在剪辑之后立即保存 saveLayer
ShapeCard 的阴影效果,默认的阴影效果为圆角的长方形边

Card实现一个通讯录的卡片

实现如下效果:

在这里插入图片描述

class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return ListView(children: [Card(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10),),elevation: 20, //阴影深度color: Colors.blue.shade100,margin: const EdgeInsets.all(10),child: const Column(children: [ListTile(title: Text("张三",style: TextStyle(fontSize: 28),),subtitle: Text("高级软件工程师"),),Divider(),ListTile(title: Text("电话:12345678910")),ListTile(title: Text("地址:北京市海淀区"))],),),Card(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10),),elevation: 20, //阴影深度color: Colors.green.shade100,margin: const EdgeInsets.all(10),child: const Column(children: [ListTile(title: Text("张三",style: TextStyle(fontSize: 28),),subtitle: Text("高级软件工程师"),),Divider(),ListTile(title: Text("电话:12345678910")),ListTile(title: Text("地址:北京市海淀区"))],),),Card(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10),),elevation: 20, //阴影深度color: Colors.red.shade100,margin: const EdgeInsets.all(10),child: const Column(children: [ListTile(title: Text("张三",style: TextStyle(fontSize: 28),),subtitle: Text("高级软件工程师"),),Divider(),ListTile(title: Text("电话:12345678910")),ListTile(title: Text("地址:北京市海淀区"))],),),],);}
}

Card实现一个图文列表

实现如下效果:

在这里插入图片描述

import 'package:flutter/material.dart';
import './res/listData.dart';void main() {runApp(MaterialApp(debugShowCheckedModeBanner: false,home: Scaffold(appBar: AppBar(title: const Text("Card实现图文列表")),body: const MyApp(),),));
}class MyApp extends StatelessWidget {const MyApp({super.key});List<Widget> _initCardData() {var tempList = listData.map((value) {return Card(elevation: 20,shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),margin: const EdgeInsets.all(10),child: Column(children: [AspectRatio(aspectRatio: 16 / 9,child: Image.network(value["imageUrl"],fit: BoxFit.cover,)),ListTile(leading: CircleAvatar(backgroundImage: NetworkImage(value["imageUrl"]),),title: Text(value["title"]),subtitle: Text(value["author"]),),],),);});return tempList.toList();}Widget build(BuildContext context) {return ListView(children: _initCardData(),);}
}

按钮组件

按钮组件的属性

属性说明
onPressed必填参数,按下按钮时触发的回调,接收一个方法,传 null 表示按钮禁用,会显示禁用相关样式
child子组件
style通过 ButtonStyle 装饰

ButtonStylee里面的常用的参数

属性名称值类型属性值
foregroundColorColor文本颜色
backgroundColorColor按钮颜色
shadowColorColor阴影颜色
elevationdouble阴影的范围,值越大阴影范围越大
padding内边距
shape设置按钮的形状
side设置按钮边框

ElevatedButton、TextButton、OutlinedButton、IconButton

  • ElevatedButton 即 “凸起” 按钮,它默认带有阴影和灰色背景。按下后,阴影会变大
  • TextButton 即文本按钮,默认背景透明并不带阴影。按下后,会有背景色
  • OutlineButton 默认有一个边框,不带阴影且背景透明。按下后,边框颜色会变亮、同时出现背景和阴影
  • IconButton 是一个可点击的 Icon,不包括文字,默认没有背景,点击后会出现背景
class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return ListView(children: [Row(mainAxisAlignment: MainAxisAlignment.spaceAround,children: [ElevatedButton(onPressed: () {print("ElevatedButton");},child: const Text("普通按钮")),TextButton(onPressed: () {}, child: const Text("文本按钮")),OutlinedButton(onPressed: () {}, child: const Text("边框按钮")),IconButton(onPressed: () {}, icon: const Icon(Icons.thumb_up))],),]);}
}

在这里插入图片描述

带图标的按钮

ElevatedButtonTextButtonOutlineButton 都有一个 icon 构造函数,通过它可以轻松创建带图标的按钮。

class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return ListView(children: [Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ElevatedButton.icon(onPressed: () {},icon: const Icon(Icons.send),label: const Text("发送")),TextButton.icon(onPressed: () {},icon: const Icon(Icons.info),label: const Text("消息")),OutlinedButton.icon(onPressed: () {},icon: const Icon(Icons.add),label: const Text("增加"))]),]);}
}

在这里插入图片描述

修改按钮的宽度高度

按钮组件中没有属性可以让我们直接修改宽度和高度,但是我们可以将按钮写在一个 Container 或者 SizeBox 里面,以此来控制按钮的宽高。

class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return ListView(children: [Column(mainAxisAlignment: MainAxisAlignment.spaceAround,children: [SizedBox(height: 80,width: 200,child: ElevatedButton(style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.red),foregroundColor: MaterialStateProperty.all(Colors.black)),onPressed: () {},child: const Text('宽度高度'),),),],),]);}
}

在这里插入图片描述

自适应按钮

将按钮写在 Expanded 组件中,可以让按钮自适应,当还有其它组件时,按钮会根据屏幕自动调节大小

class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return ListView(children: [Row(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Expanded(child: Container(height: 60,margin: const EdgeInsets.all(10),child: ElevatedButton(child: const Text('自适应按钮'),onPressed: () {print("自适应按钮");},),),),],)]);}
}

在这里插入图片描述

配置圆角圆形按钮

class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return ListView(children: [Row(mainAxisAlignment: MainAxisAlignment.spaceAround,children: [ElevatedButton(style: ButtonStyle(shape: MaterialStateProperty.all(//圆角RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)))),onPressed: () {},child: const Text("圆角")),SizedBox(height: 80,width: 80,child: ElevatedButton(style: ButtonStyle(shape: MaterialStateProperty.all(//圆形const CircleBorder(side: BorderSide(width: 2, color: Colors.yellow)))),onPressed: () {},child: const Text("圆形")),),],),]);}
}

在这里插入图片描述

修改OutlinedButton边框

class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return ListView(children: [Row(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Expanded(child: Container(margin: const EdgeInsets.all(20),height: 50,child: OutlinedButton(style: ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.black),side: MaterialStateProperty.all(const BorderSide(width: 1, color: Colors.blue))),onPressed: () {},child: const Text("修改边框")),),)],)]);}
}

在这里插入图片描述

Wrap组件

自定义一个按钮组件

Wrap 可以实现流布局,单行的 WrapRow 表现几乎一致,单列的 Wrap 则跟 Column 表现几乎一致。但 RowColumn 都是单行单列的,Wrap 则突破了这个限制,mainAxis 上空间不足时,则向 crossAxis
去扩展显示。

属性说明
direction主轴的方向,默认水平
alignment主轴的对齐方式
spacing主轴方向上的间距
textDirection文本方向
verticalDirection定义了 children 的摆放顺序,默认是 down,见 Flex 相关属性介绍
runAlignmentrun 的对齐方式,run 可以理解为新的行或者列,如果是水平方向布局的话,run 可以理解为新的一行
runSpacingrun 的间距

下面我们通过实现一个案例来介绍 Wrap 组件的使用:

//自定义按钮
class MyButton extends StatelessWidget {String text; //按钮上的文本void Function()? onPressed;MyButton(this.text, {super.key, required this.onPressed});Widget build(BuildContext context) {return ElevatedButton(onPressed: onPressed,style: ButtonStyle(backgroundColor:MaterialStateProperty.all(const Color.fromARGB(255, 239, 237, 237)),foregroundColor: MaterialStateProperty.all(Colors.black45),),child: Text(text),);}
}

Wrap组件的简单使用

class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return Padding(padding: const EdgeInsets.all(10),child: Wrap(spacing: 5,		//水平方向间隔runSpacing: 5,	//垂直方向间隔children: [MyButton("第1集", onPressed: () {}),MyButton("第2集", onPressed: () {}),MyButton("第3集", onPressed: () {}),MyButton("第4集", onPressed: () {}),MyButton("第5集", onPressed: () {}),MyButton("第6集", onPressed: () {}),MyButton("第7集", onPressed: () {}),MyButton("第8集", onPressed: () {}),MyButton("第9集", onPressed: () {}),MyButton("第10集", onPressed: () {}),],),);}
}

在这里插入图片描述

Wrap组件搜索页面布局

实现如下效果:

在这里插入图片描述

class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return Padding(padding: const EdgeInsets.all(10),child: ListView(children: [Row(children: [Text("热搜", style: Theme.of(context).textTheme.headlineSmall)],),const Divider(),Wrap(spacing: 10,runSpacing: 12,children: [MyButton("T恤", onPressed: () {}),MyButton("笔记本", onPressed: () {}),MyButton("时尚", onPressed: () {}),MyButton("游戏", onPressed: () {}),MyButton("弹射风筝", onPressed: () {}),MyButton("斗篷伞", onPressed: () {}),MyButton("猫窝鱼缸", onPressed: () {}),MyButton("鸡腿帽", onPressed: () {}),],),const SizedBox(height: 10),Row(children: [Text("历史搜索", style: Theme.of(context).textTheme.headlineSmall)],),const Divider(),const Column(children: [ListTile(title: Text("时尚")),Divider(),ListTile(title: Text("潮流")),Divider(),],),const SizedBox(height: 40),Padding(padding: const EdgeInsets.all(40),child: OutlinedButton.icon(onPressed: () {},icon: const Icon(Icons.delete),label: const Text("清空历史记录"),style: ButtonStyle(foregroundColor: MaterialStateProperty.all(Colors.black38)),),)],),);}
}

相关文章:

【Flutter】AspectRatio组件Card组件按钮组件Wrap组件

&#x1f525; 本文由 程序喵正在路上 原创&#xff0c;CSDN首发&#xff01; &#x1f496; 系列专栏&#xff1a;Flutter学习 &#x1f320; 首发时间&#xff1a;2024年5月25日 &#x1f98b; 欢迎关注&#x1f5b1;点赞&#x1f44d;收藏&#x1f31f;留言&#x1f43e; 目…...

【IDEA软件应用篇】IDEA基础开发设置和开发快捷键

IDEA是一种集成开发环境&#xff0c;可以运行java代码。 本篇文章你将收获到下面的知识&#xff1a; &#xff08;1&#xff09;IDEA如何设置字体大小快捷键 &#xff08;2&#xff09;如何解决每次进IDEA时&#xff0c;进去的页面都是上次使用完时的那个页面 &#xff08;3&am…...

机器学习--数学部分笔记

前言 因为周三要考试,所以数学部分写一下笔记 正文 随机事件和随机实验 条件概率 • 在已知事件 &#x1d435; 发生的条件下&#xff0c;事件&#x1d434;发生的概率称为事件 &#x1d434; 的条件概率&#xff0c;记为&#x1d443;(&#x1d434;|&#x1d435;) 全概率…...

基于springboot的在线宠物用品交易网站源码数据库

基于springboot的在线宠物用品交易网站源码数据库 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了在线宠物用品交易网站的开发全过程。通过分析在线宠物用品交易网站管理的不足&#xff0c;创建了一个计算机管理在…...

【Pytorch】13.搭建完整的CIFAR10模型

项目源码 已上传至githubCIFAR10Model&#xff0c;如果有帮助可以点个star 简介 在前文【Pytorch】10.CIFAR10模型搭建我们学习了用Module来模拟搭建CIFAR10的训练流程 本节将会加入损失函数&#xff0c;梯度下降&#xff0c;TensorBoard来完整搭建一个训练的模型 基本步骤 搭建…...

护目镜佩戴自动识别预警摄像机

护目镜佩戴自动识别预警摄像机是一种智能监测设备&#xff0c;专门用于佩戴护目镜的工人进行作业时&#xff0c;能够自动识别有潜在风险的场景&#xff0c;并及时发出预警信号。该摄像机配备人脸识别和智能预警系统&#xff0c;可以检测危险情况并为工人提供实时安全保护&#…...

keep-alive的使用

Vue中的<keep-alive>组件是前端开发中的一个宝藏功能&#xff0c;它如同时光胶囊般保留组件的状态&#xff0c;让组件在切换时仿佛按下暂停键&#xff0c;再次回来时还能继续播放&#xff0c;极大地优化了用户体验和性能。&#x1f680;✨ 作用 状态保留&#xff1a;当包…...

【Linux】中的常见的重要指令(中)

目录 一、man指令 二、cp指令 三、cat指令 四、mv指令 五、more指令 六、less指令 七、head指令 八、tail指令 一、man指令 Linux的命令有很多参数&#xff0c;我们不可能全记住&#xff0c;我们可以通过查看联机手册获取帮助。访问Linux手册页的命令是 man 语法: m…...

营收净利双降、股东减持,大降价也救不了良品铺子

号称“高端零食第一股”的良品铺子(603719.SH)&#xff0c;正遭遇部分股东的“用脚投票”。 5月17日晚间&#xff0c;良品铺子连发两份减持公告&#xff0c;其控股股东宁波汉意创业投资合伙企业、持股5%以上股东达永有限公司&#xff0c;两者均计划减持。 其中&#xff0c;宁…...

【设计模式】设计模式的分类

通常设计模式的分类有创建型、行为型和结构型。 创建型 常用的有&#xff1a;单例模式、工厂模式&#xff08;工厂方法和抽象工厂&#xff09;、建造者模式。 不常用的有&#xff1a;原型模式。 创建型模式涉及到将对象实例化&#xff0c;这类模式都提供一个方法&#xff0c;将…...

TCP/UDP的连接机制

TCP/UDP的连接机制 TCP的连接机制 TCP&#xff08;Transmission Control Protocol&#xff09;是一种面向连接的协议&#xff0c;提供可靠的、按顺序的数据传输服务。TCP的连接机制包括连接建立、数据传输和连接终止。 1. 连接建立&#xff08;三次握手&#xff09; TCP通过…...

供应链金融模式学习资料

目录 产生背景 供应链金融的诞生 供应链金额的六大特征...

代码随想录-算法训练营day50【动态规划12:最佳买卖股票时机含冷冻期、买卖股票的最佳时机含手续费、股票问题总结】

代码随想录-035期-算法训练营【博客笔记汇总表】-CSDN博客 第九章 动态规划part12● 309.最佳买卖股票时机含冷冻期 ● 714.买卖股票的最佳时机含手续费 ●总结309.最佳买卖股票时机含冷冻期 本题加了一个冷冻期,状态就多了,有点难度,大家要把各个状态分清,思路才能清晰…...

Dilworth 定理

这是一个关于偏序集的定理&#xff0c;事实上它也可以扩展到图论&#xff0c;dp等中&#xff0c;是一个很有意思的东西 偏序集 偏序集是由集合 S S S以及其上的一个偏序关系 R R R定义的&#xff0c;记为 ( S , R ) (S,R) (S,R) 偏序关系&#xff1a; 对于一个二元关系 R ⊂…...

BUUCTF---web---[BJDCTF2020]ZJCTF,不过如此

1、点开连接&#xff0c;页面出现了提示 传入一个参数text&#xff0c;里面的内容要包括I have a dream。 构造&#xff1a;?/textI have a dream。发现页面没有显示。这里推测可能得使用伪协议 在文件包含那一行&#xff0c;我们看到了next.php的提示&#xff0c;我们尝试读取…...

力扣刷题---2206. 将数组划分成相等数对【简单】

题目描述&#x1f357; 给你一个整数数组 nums &#xff0c;它包含 2 * n 个整数。 你需要将 nums 划分成 n 个数对&#xff0c;满足&#xff1a; 每个元素 只属于一个 数对。 同一数对中的元素 相等 。 如果可以将 nums 划分成 n 个数对&#xff0c;请你返回 true &#xf…...

2461. 长度为 K 子数组中的最大和(c++)

给你一个整数数组 nums 和一个整数 k 。请你从 nums 中满足下述条件的全部子数组中找出最大子数组和&#xff1a; 子数组的长度是 k&#xff0c;且子数组中的所有元素 各不相同 。 返回满足题面要求的最大子数组和。如果不存在子数组满足这些条件&#xff0c;返回 0 。 子数…...

range for

1. 基于范围的for循环语法 C11标准引入了基于范围的for循环特性&#xff0c;该特性隐藏了迭代器 的初始化和更新过程&#xff0c;让程序员只需要关心遍历对象本身&#xff0c;其语法也 比传统for循环简洁很多&#xff1a; for ( range_declaration : range_expression ) {loo…...

leetcode230 二叉搜索树中第K小的元素

题目 给定一个二叉搜索树的根节点 root &#xff0c;和一个整数 k &#xff0c;请你设计一个算法查找其中第 k 个最小元素&#xff08;从 1 开始计数&#xff09;。 示例 输入&#xff1a;root [5,3,6,2,4,null,null,1], k 3 输出&#xff1a;3 解析 这道题应该是能做出…...

.Net Core学习笔记 框架特性(注入、配置)

注&#xff1a;直接学习的.Net Core 6&#xff0c;此版本有没有startup.cs相关的内容 项目Program.cs文件中 是定义项目加载 启动的地方 //通过builder对项目进行配置、服务的加载 var builder WebApplication.CreateBuilder(args); builder.Services.AddControllers();//将…...

谷歌浏览器插件

项目中有时候会用到插件 sync-cookie-extension1.0.0&#xff1a;开发环境同步测试 cookie 至 localhost&#xff0c;便于本地请求服务携带 cookie 参考地址&#xff1a;https://juejin.cn/post/7139354571712757767 里面有源码下载下来&#xff0c;加在到扩展即可使用FeHelp…...

C++_核心编程_多态案例二-制作饮品

#include <iostream> #include <string> using namespace std;/*制作饮品的大致流程为&#xff1a;煮水 - 冲泡 - 倒入杯中 - 加入辅料 利用多态技术实现本案例&#xff0c;提供抽象制作饮品基类&#xff0c;提供子类制作咖啡和茶叶*//*基类*/ class AbstractDr…...

css实现圆环展示百分比,根据值动态展示所占比例

代码如下 <view class""><view class"circle-chart"><view v-if"!!num" class"pie-item" :style"{background: conic-gradient(var(--one-color) 0%,#E9E6F1 ${num}%),}"></view><view v-else …...

黑马Mybatis

Mybatis 表现层&#xff1a;页面展示 业务层&#xff1a;逻辑处理 持久层&#xff1a;持久数据化保存 在这里插入图片描述 Mybatis快速入门 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6501c2109c4442118ceb6014725e48e4.png //logback.xml <?xml ver…...

渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止

<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet&#xff1a; https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...

蓝桥杯 2024 15届国赛 A组 儿童节快乐

P10576 [蓝桥杯 2024 国 A] 儿童节快乐 题目描述 五彩斑斓的气球在蓝天下悠然飘荡&#xff0c;轻快的音乐在耳边持续回荡&#xff0c;小朋友们手牵着手一同畅快欢笑。在这样一片安乐祥和的氛围下&#xff0c;六一来了。 今天是六一儿童节&#xff0c;小蓝老师为了让大家在节…...

LLM基础1_语言模型如何处理文本

基于GitHub项目&#xff1a;https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken&#xff1a;OpenAI开发的专业"分词器" torch&#xff1a;Facebook开发的强力计算引擎&#xff0c;相当于超级计算器 理解词嵌入&#xff1a;给词语画"…...

成都鼎讯硬核科技!雷达目标与干扰模拟器,以卓越性能制胜电磁频谱战

在现代战争中&#xff0c;电磁频谱已成为继陆、海、空、天之后的 “第五维战场”&#xff0c;雷达作为电磁频谱领域的关键装备&#xff0c;其干扰与抗干扰能力的较量&#xff0c;直接影响着战争的胜负走向。由成都鼎讯科技匠心打造的雷达目标与干扰模拟器&#xff0c;凭借数字射…...

css3笔记 (1) 自用

outline: none 用于移除元素获得焦点时默认的轮廓线 broder:0 用于移除边框 font-size&#xff1a;0 用于设置字体不显示 list-style: none 消除<li> 标签默认样式 margin: xx auto 版心居中 width:100% 通栏 vertical-align 作用于行内元素 / 表格单元格&#xff…...

高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数

高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数 在软件开发中,单例模式(Singleton Pattern)是一种常见的设计模式,确保一个类仅有一个实例,并提供一个全局访问点。在多线程环境下,实现单例模式时需要注意线程安全问题,以防止多个线程同时创建实例,导致…...