flutter实战之美化 container

我们来看一个代码

Dart 复制代码
Column(
              children: [
                ...transaction.map((e) => Card(
                  child:Row(
                    children: [
                      Container(child: Text(e.amout.toString()),),
                      Column(
                        children: [
                          Text(e.title),
                          Text(e.date.toString()),
                        ],
                      )
                    ],
                  ),
                )).toList()
              ],
            )

看看效果

感觉很不好

我们要去美化一下container

  • symmetric({vertical, horizontal}):用于设置对称方向的填充,vertical指top和bottom,horizontal指left和right。

另外,首先我们要了解可以通过container来设置边距和边框,可以在Textstytle中来设置美化样式,对于Column 中可以设置对称轴的对齐方式

我们来看看代码

Dart 复制代码
Column(
              children: [
                ...transaction
                    .map((e) => Card(
                          child: Row(
                            children: [
                              Container(
                                padding: EdgeInsets.all(10),
                                child: Text(
                                  e.amout.toString(),
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                      color: Colors.purple),
                                ),
                                margin: EdgeInsets.symmetric(
                                    vertical: 10, horizontal: 15),
                                decoration: BoxDecoration(
                                    border: Border.all(
                                        color: Colors.purple, width: 2)),
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(e.title,
                                  style: TextStyle(
                                    fontSize: 10,
                                    color: Colors.grey
                                  ),),
                                  Text(e.date.toString()),
                                ],
                              )

效果如下:

对于价钱的格式我们可以借助

Dart 复制代码
Text(
                                  "\$${e.amout}",
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                      color: Colors.purple),
                                ),

来实现相同的效果

我们看看最后的美化代码

Dart 复制代码
Column(
              children: [
                ...transaction
                    .map((e) => Card(
                          child: Row(
                            children: [
                              Container(
                                padding: EdgeInsets.all(10),
                                child: Text(
                                  "\$${e.amout}",
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontSize: 16,
                                      color: Colors.purple),
                                ),
                                margin: EdgeInsets.symmetric(
                                    vertical: 10, horizontal: 15),
                                decoration: BoxDecoration(
                                    border: Border.all(
                                        color: Colors.purple, width: 2)),
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    e.title,
                                    style: TextStyle(
                                        fontSize: 10, color: Colors.grey),
                                  ),
                                  Text(DateFormat().format(e.date)),
                                ],
                              )
                            ],
                          ),
                        ))
                    .toList()
              ],
            )
相关推荐
消失的旧时光-194311 小时前
Flutter 组件:Row / Column
flutter
程序员老刘14 小时前
Flutter版本选择指南:3.35稳定,3.38发布 | 2025年11月
flutter·客户端
kirk_wang15 小时前
Flutter 3.38和Dart 3.10中最大的更新
flutter
前端小伙计16 小时前
Flutter 配置国内镜像,加速项目加载!
flutter
zonda的地盘19 小时前
开发 Flutter Plugin 之 初始配置
flutter
消失的旧时光-19432 天前
Flutter TextField 从入门到精通:掌握输入框的完整指南
flutter
wordbaby2 天前
Flutter Form Builder 完全指南:告别 Controller 地狱
前端·flutter
tbit2 天前
fluwx 拉起小程序WXLog:Error:fail to load Keychain status:-25300, keyData null:1
flutter·ios·微信小程序
QuantumLeap丶2 天前
《Flutter全栈开发实战指南:从零到高级》- 19 -手势识别
flutter·ios·前端框架
卢叁2 天前
Flutter之阿里云视频播放器支持 iOS模拟器解决方案
flutter