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来设置边距和边框,可以在Text的stytle中来设置美化样式,对于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()
              ],
            )
相关推荐
恋猫de小郭9 小时前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
程序员老刘1 天前
Flutter版本选择指南:尽早升级3.47.9 | 2026年11月
flutter·ai编程·客户端
恋猫de小郭1 天前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
恋猫de小郭2 天前
Flutter A2UI 1.0 ,原来的 genui 也正式换成了这一套 SDK 了
android·前端·flutter
LinXunFeng2 天前
A2UI 1.0 详解:让 AI 生成 Flutter 界面,还不怕它乱写
前端·flutter·agent
好的佩奇3 天前
Dart 之元数据与注解
android·flutter·dart
恋猫de小郭5 天前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
好的佩奇6 天前
蓝牙之设备角色与连接
android·flutter·蓝牙
SoaringHeart7 天前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter
恋猫de小郭7 天前
Flutter 3.50.0 Release 的第一个崩溃,修好了,还没 hotfix
android·前端·flutter