Flutter中AspectRatio、Card 卡片组件
1. AspectRatio 组件
AspectRatio 的作用是根据设置调整子元素 child 的宽高比。
AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widget 的高度是由宽度和比率决定的,类似于 BoxFit 中的 contain,按照固定比率去尽量占满区域。
如果在满足所有限制条件过后无法找到一个可行的尺寸,AspectRatio 最终将会去优先适应布局限制条件,而忽略所设置的比率。
常见属性:
1. aspectRatio 宽高比。
页面最终也许不会按这个值去布局, 具体则要看综合因素,这只是一个参考值;
2. child 子组件;
代码示例:
import 'package:flutter/material.dart';void main(){runApp(MyApp());
}// 抽离成一个单独的组件
class MyApp extends StatelessWidget{@overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(// 导航条appBar:AppBar(title:Text('Flutter App')),// 主体body:HomeContent(),),// 主题theme: ThemeData(primarySwatch:Colors.yellow),);}
}// AspectRatio组件的用法
class HomeContent extends StatelessWidget{@overrideWidget build(BuildContext context) {return AspectRatio(// 高度与宽度的比值aspectRatio: 5.0/1.0 ,child:Container(color:Colors.red));}
}
效果图如下:
2. Card 组件
Card 是卡片组件块,内容可以由大多数类型的 Widget 构成,Card 具有圆角和阴影,这让它看起来有立体感。
常见属性:
1. margin 外边距;
2. shape 阴影效果;
3. child 子组件;
代码示例:
import 'package:flutter/material.dart';void main(){runApp(MyApp());
}// 抽离成一个单独的组件
class MyApp extends StatelessWidget{@overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(// 导航条appBar:AppBar(title:Text('Flutter App')),// 主体body:HomeContent(),),// 主题theme: ThemeData(primarySwatch:Colors.yellow),);}
}// Card组件的用法
class HomeContent extends StatelessWidget{@overrideWidget build(BuildContext context) {return ListView(children: <Widget>[Card(margin:EdgeInsets.all(10),child:Column(children: <Widget>[ListTile(title:Text("张三",style:TextStyle(fontSize:28)),subtitle: Text("高级工程师"),),ListTile(title:Text("电话:13999999xxx",style:TextStyle(fontSize:28)),), ListTile(title:Text("北京中关村",style:TextStyle(fontSize:28)),)],)),Card(margin:EdgeInsets.all(10),child:Column(children: <Widget>[ListTile(title:Text("李四",style:TextStyle(fontSize:28)),subtitle: Text("中级工程师"),),ListTile(title:Text("电话:13666666xxx",style:TextStyle(fontSize:28)),), ListTile(title:Text("北京中关村",style:TextStyle(fontSize:28)),)],))],);}
}
效果图如下:
3. Card 组件应用实例
利用Card组件实现图文列表。
import 'package:flutter/material.dart';void main(){runApp(MyApp());
}// 抽离成一个单独的组件
class MyApp extends StatelessWidget{@overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(// 导航条appBar:AppBar(title:Text('Flutter App')),// 主体body:HomeContent(),),// 主题theme: ThemeData(primarySwatch:Colors.yellow),);}
}// 利用Card组件实现图文列表
class HomeContent extends StatelessWidget{@overrideWidget build(BuildContext context) {return ListView(children: <Widget>[Card(margin:EdgeInsets.all(10),child:Column(children: <Widget>[AspectRatio(// 图片宽高比aspectRatio: 16/9,child:Image.network("http://www.itying.com/images/flutter/1.png",fit: BoxFit.cover), ),ListTile(leading:ClipOval(child:Image.network("http://www.itying.com/images/flutter/1.png",fit: BoxFit.cover,width:40,height:40),),title:Text("经典传奇"),subtitle:Text("这是新中国第一大案..."))])),Card(margin:EdgeInsets.all(10),child:Column(children: <Widget>[AspectRatio(// 图片宽高比aspectRatio: 16/9,child:Image.network("http://www.itying.com/images/flutter/1.png",fit: BoxFit.cover), ),ListTile(// 圆形头像组件leading:CircleAvatar(backgroundImage: NetworkImage("http://www.itying.com/images/flutter/1.png"),),title:Text("经典传奇"),subtitle:Text("这是新中国第一大案..."))]))]);}
}
效果图如下:
4. 用本地模拟数据列表实现卡片列表
模拟数据列表:
// lib/res/listData.dart
List listData = [{"title": 'Candy Shop',"author": 'Mohamed Chahin',"imageUrl": 'https://www.itying.com/images/flutter/1.png',"description":'Flutter is Google’s mobile UI framework for crafting high-quality native experiences on iOS and Android in record time. Flutter works with existing',},{"title": 'Childhood in a picture',"author": 'Google',"imageUrl": 'https://www.itying.com/images/flutter/2.png',"description":'Flutter is Google’s mobile UI framework for crafting high-quality native experiences on iOS and Android in record time. Flutter works with existing',},{"title": 'Alibaba Shop',"author": 'Alibaba',"imageUrl": 'https://www.itying.com/images/flutter/3.png',"description":'Dart is a client-optimized language for fast apps on any platform... Dart is a client-optimized language for fast apps on any platform Optimizedfor',},{"title": 'Candy Shop',"author": 'Mohamed Chahin',"imageUrl": 'https://www.itying.com/images/flutter/4.png',"description":'Dart is a client-optimized language for fast apps on any platform... Dart is a client-optimized language for fast apps on any platform Optimizedfor',},{"title": 'Tornado',"author": 'Mohamed Chahin',"imageUrl": 'https://www.itying.com/images/flutter/5.png',"description":'Flutter is Google’s mobile UI framework for crafting high-quality native experiences on iOS and Android in record time. Flutter works with existing',},{"title": 'Undo',"author": 'Mohamed Chahin',"imageUrl": 'https://www.itying.com/images/flutter/6.png',"description":'Flutter is Google’s mobile UI framework for crafting high-quality native experiences on iOS and Android in record time. Flutter works with existing',},{"title": 'white-dragon',"author": 'Mohamed Chahin',"imageUrl": 'https://www.itying.com/images/flutter/7.png',"description":'Dart is a client-optimized language for fast apps on any platform... Dart is a client-optimized language for fast apps on any platform Optimizedfor',}
];
卡片列表示例:
import 'package:flutter/material.dart';
import 'res/listData.dart';
void main(){runApp(MyApp());
}// 抽离成一个单独的组件
class MyApp extends StatelessWidget{@overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(// 导航条appBar:AppBar(title:Text('Flutter App')),// 主体body:HomeContent(),),// 主题theme: ThemeData(primarySwatch:Colors.yellow),);}
}// 渲染数据列表实现卡片列表
class HomeContent extends StatelessWidget{@overrideWidget build(BuildContext context) {return ListView(// 遍历数据children:listData.map((obj){return Card(margin:EdgeInsets.all(10),child:Column(children: <Widget>[AspectRatio(// 图片宽高比aspectRatio: 16/9,child:Image.network(obj["imageUrl"],fit: BoxFit.cover), ),ListTile(// 圆形头像组件leading:CircleAvatar(backgroundImage: NetworkImage(obj["imageUrl"]),),// 标题title:Text(obj["title"]),// 副标题subtitle:Text(obj["description"],overflow:TextOverflow.ellipsis,maxLines:1,))]));}).toList());}
}
效果图如下:
Flutter中AspectRatio、Card 卡片组件相关推荐
- Flutter页面布局:Flutter AspectRatio、Card卡片组件、卡片图文列表
1.Flutter AspectRatio组件 AspectRatio 的作用是根据设置调整子元素 child 的宽高比. AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,wid ...
- Flutter 中神奇的 AbsorbPointer 组件
Flutter 中神奇的 AbsorbPointer 组件
- Flutter中通过循环渲染组件
class ContactsState extends State<Contacts>{List formList;initState() {super.initState();formL ...
- React 组件封装之 Card 卡片
React 组件封装之 Card 一.Card 卡片 二.使用案例 三.API 使用指南 四.源代码 一.Card 卡片 组件说明: 使列表成为卡片形式的布局. 效果展示: 二.使用案例 import ...
- 官方文档——一篇文章弄懂Flutter中的布局
来自Flutter中文资源主页https://flutter.cn/ 原文:https://flutter.cn/docs/development/ui/layout Flutter 中的布局 要点 ...
- Flutter 中文文档:Flutter 中的布局
要点 widgets 是用于构建 UI 的类. widgets 可以用于布局和展示 UI 元素. 通过组合简单的 widgets 来构建复杂的 widgets. Flutter 布局的核心机制是 wi ...
- flutter bloc_如何使用BLoC模式处理Flutter中的状态
flutter bloc Last year, I picked up Flutter and I must say it has been an awesome journey so far. Fl ...
- 【Flutter】【widget】【card】卡片组件的使用和练习代码
文章目录 前言 一.card是什么? 二.使用步骤 1.基础的使用 2.提升 总结 前言 一.card是什么? 卡片组件,可以制作很多卡片类型的widget,比如商品,个人信息卡片等 二.使用步骤 1 ...
- Flutter 中 Card 设置圆角
Flutter 中 Card 设置圆角 圆角设置非常常用,本文介绍 Card 容器组件的圆角等设置方法. 圆角设置 //shape 设置边,可以设置圆角 shape: RoundedRectangle ...
最新文章
- 排序算法c语言和oc实现的,几种常用的排序算法,OC实现
- python 超时重试方法
- svm的错误理解之一
- Linux加载Linux的引导程序,Linux的引导-引导加载程序:LILO和GRUB
- 通过异常捕获判断字符串是不是数字格式
- DDD:如何更好的使用值对象
- 通过命令修改wampserver的mysql密码
- Linux:关于头文件的位置
- 使用Combres 库 ASP.NET 网站优化
- linux 查看neihe版本_linux查看系统内核版本号
- tb项目管理实践_项目经理与项目管理整理
- oracle创建用户及授权等相关操作
- 让 API 端点的响应速度提高 50 倍!
- 面向对象【林老师版】:绑定方法与非绑定方法(十七)
- 电脑输入法变成繁体中文如何改回来
- 元宇宙时代,服装品牌们如何成为“头号玩家”?
- 【论文精读】TACRED Revisited: A Thorough Evaluation of the TACRED Relation Extraction Task
- 游戏 找CALL技巧 突破口
- CAPS发布了完全支持OpenACC的编译器了!
- 283移动零之Leecode—数组篇(双指针)
热门文章
- 163邮箱怎么开通注册?公司邮箱格式选择,可以邮箱注册微信吗?
- 简谐振动的能量与合成(大学物理笔记)
- 信创办公–基于WPS的PPT最佳实践系列 (将幻灯片组织成节的形式)
- 如何看计算机加密方式,电脑常见的几种加密的方法
- Java微信公众号开发(附源码!!!)
- Centos7.5 系统使用pptpd搭建服务器
- 国内的IT生意还真TMD难做,想生存发展大家还得多思考一下,出路在哪里?稍微迷茫了一点点...
- 使用H5Streamer轻松搭建视频监控项目
- cout 输出 uint8_t 整形值
- 输入法自定义短语笔记/md常用配置/搜狗输入法自定义词库推荐