Flutter StatelessWidget与基础组件(1.1)

在这里插入图片描述

Container

容器组件,继承自StatelessWidget

Container参数类型说明
keyKeyContainer 唯一标识符,用于查找更新
alignmentAlignment控制Container的对齐方式
paddingEdgeInsetsEdgeInsets.only(left: )控制左边padding,EdgeInsets.all控制当前周围padding
marginEdgeInsetsEdgeInsets.only(left: )控制左边margin,EdgeInsets.all控制当前周围margin
decorationBoxDecoration绘制在 child 后面的装饰设置decoration后不能设置color否则会编译冲突导致报错
colorColor设置背景色(不能和decoration装饰器的颜色重复,否则会报错)
widthdoubleContainer 的宽度,设置为 double.infinity 可以强制在宽度上撑满,不设置, 则根据 child 和父节点两者一起布局
heightdoubleContainer 的高度,设置为 double.infinity 即可以强制在高度上撑满
childWidgetContainer 中的内容 Widget
BoxDecoration参数说明
color设置背景色
borderRadius设置容器圆角BorderRadius.all(Radius.circular(double)) /BorderRadius.only(left:,top:,right:,bottom)

来看看代码吧:
在这里插入图片描述
注释写的很清楚,来看看最终的效果:

在这里插入图片描述

Text

文本组件

注:加粗的是必填参数

Text参数类型说明
dateString显示的内容
maxLinesint显示最大行数,默认为0
styleTextStyle文本样式,可定义文本的字体大小、颜色、粗细等
textAlignTextAlign文本的对齐方式
textScaleFactordouble字体缩放系数,例如1.3比原来文字大30%
overflowTextOverflowTextOverflow.clip:剪切溢出的文本以修复其容器。TextOverflow.ellipsis:使用省略号表示文本已溢出。TextOverflow.fade:将溢出的文本淡化为透明。
textDirectionTextDirection文字显示样式,第二行从左边显示TextDirection.ltr,从右到左使用TextDirection.rtl默认是从左到右
TextStyle参数类型说明
colorColor文本颜色
decorationTextDecoration下划线(TextDecoration.underline)
上划线(TextDecoration.overline)
删除线(TextDecoration.lineThrough)
无(TextDecoration.none)
decorationColorColor绘制文本装饰的颜色
decorationStyleTextDecorationStyle画一条虚线 TextDecorationStyle.dashed
画一条虚线 TextDecorationStyle.dotted
画两条线 TextDecorationStyle.double
画一条实线 TextDecorationStyle.solid
画一条正弦线(波浪线) TextDecorationStyle.wavy
fontWeightFontWeightFontWeight.bold字体加粗
fontStyleFontStyleFontStyle.italic 使用斜体
FontStyle.normal 使用直立
textBaselineTextBaselineTextBaseline.alphabetic:文本基线是标准的字母基线
TextBaseline.ideographic:文字基线是表意字基线
fontSizedouble字体大小默认为14pt
letterSpacingdouble水平字母之前的距离负数会让字母离得更近,以至于重合
wordSpacingdouble单词之间的距离负数会让字单词得更近,以至于重合
backgroundPaint文本背景色
foregroundPaint文本前景色,不能与color共存
shadowsList<Shadow>边框、圆角、阴影、形状、渐变、背景图像等

直接上代码:
在这里插入图片描述
效果图:
(黄色背景是Container背景)
在这里插入图片描述

Icon

图标组件

Icon参数属性说明
keyKey参考官方文档.
sizedouble因为图片是正方形,所以设置的是宽和高,默认是24dp
colorColor图标颜色
textDirectionTextDirection文字显示样式,第二行从左边显示TextDirection.ltr,从右到左使用TextDirection.rtl默认是从左到右

在这里插入图片描述
效果图:
在这里插入图片描述

CloseButton和BackButton

关闭按钮组件 返回按钮组件
在这里插入图片描述

ColseButton()关闭按钮没有颜色设置,默认是黑色
BackButton()返回按钮设置,可以随意设置颜色

Chip

标签组件

Chip参数类型说明
avatarWidget左边图标一般使用Icon()或CircleAvatar()设置左边文字
lableWidget文字属性
deleteIconWidget右边删除按钮
deleteButtonTooltipMessageString长按删除按钮弹出
onDeletedVoidCallback删除按钮响应事件
shapeShapeBorderChip形状
elevationdoubleChip阴影
backgroundColorColor背景颜色
paddingEdgeInsetsGeometry内边距

CircleAvatar组件是将图标裁成圆角

CircleAvatar参数类型说明
childWidget一般情况都是Text
backgroundColorcolor圆形背景
radiusdouble圆的半径
 Chip(
//              avatar: Icon(Icons.ac_unit),//设置左边图标
              avatar: CircleAvatar(
                //设置左边图标
                child: Text("C"),
                backgroundColor: Colors.red,
                radius: 30, //设置圆的大小
              ),
              label: Text("Chip"),
              //设置显示文字
              labelStyle: TextStyle(fontSize: 30),
              //设置文字样式
              labelPadding: EdgeInsets.all(10),
              //设置文字内边距
              deleteIcon: Icon(
                Icons.delete,
                size: 50,
              ),
              //设置右边点击按钮
              deleteButtonTooltipMessage: "长按弹出",
              onDeleted: () {
                //设置Chip点击事件
                print('当前下标为${mChipIndex++}');
              },
              //设置点击事件长按弹出框
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(20))),
              //设置Chip圆角
              elevation: 20,
              //设置阴影

              backgroundColor: Colors.greenAccent,
              //设置Chip背景颜色
              padding: EdgeInsets.all(10),
              
            ),

看看最终的效果吧:
在这里插入图片描述

Divider

分割线组件

Divider参数类型说明
colorColor分割线颜色
heightdouble分割线高度
thickessdouble分割线宽度
indentdouble分割线距离左边距离
endIndentdouble分割线距离右边距离
Container(
              color: Colors.black,
              margin: EdgeInsets.only(top: 30),
              child: Divider(
                color: Colors.pink,//设置颜色
                height: 60,//设置高度
                thickness: 10,//设置分割线宽度
                indent: 20,//设置距离左边20dp
                endIndent: 100,//设置距离右边100dp
              ),
            )

效果图:
在这里插入图片描述

Card

卡片组件

Card参数类型说明
colorColor背景颜色
elevationdouble阴影
childWidget子Widget
shapeShapeBorder一般使用RoundedRectangleBorder()改变圆角
Card(
              color: Colors.pink,//Card背景色
              elevation: 10,//阴影
              child: Container(
                width: 200,//宽
                height: 100,//高
                child: Text(
                  "卡片布局",
                  textAlign: TextAlign.center,//文字居中
                ),
              ),
              shape: RoundedRectangleBorder(//圆角
                  borderRadius: new BorderRadius.all(new Radius.circular(20))),
            )

效果图:
在这里插入图片描述

AlertDialog

弹框组件

AlertDialog参数类型说明
titleWidgetAlertDialog标题
contentWidgetAlertDialog显示内容
actionsList<Widget>子Widget(一般为右下角按钮或文字)
elevationdoubleAlertDialog阴影
backgroundColorColor背景颜色
shapeShapeBorderAlertDialog形状
contentPaddingEdgeInsetsGeometry文字内边距
contentTextStyleTextStyle文本样式
AlertDialog(
                        title: Text("AlertDialog弹框"),//标题
                        content: Text("这里是弹框内容"),//文本内容
                        actions: <Widget>[//子Widget
                          Container(
                            child: Text("确定"),
                          ),
                          Container(
                            child: Text("取消"),
                          ),
                        ],
                        backgroundColor: Colors.green,//背景颜色
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),//圆角
                        elevation: 20,//阴影
                        contentPadding: EdgeInsets.all(20),//文本内容内边距
                        contentTextStyle: TextStyle(fontSize: 20,color: Colors.white),//文本内容样式
                      ),

效果图:
在这里插入图片描述
注意事项:
写了这么多组件,占满了整个屏幕,所以会导致出现一个溢出错误,所以需要在外层加一个ListView()组件,这个组件到后边会详细解释,

!!!报错信息!!!
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.

Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.

点击查看完整代码

下一章:Flutter StatefluWidget与基础组件(1.2)

原创不易,你们就是我的动力,请各位点个赞在走8,谢谢啦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

s10g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值