Flutter中常用的UI设计

布局与基础控件

Scaffold 页面容器

Column/Row线性布局

Stack层叠布局

Text文本显示

Image图片显示

Button/ElevatedButton按钮

TextField输入框

ListView列表显示

GridView网格显示

AlertDialog弹窗对话

详细解释
Scaffold是页面骨架

使用场景:构建完整的页面时

Dart 复制代码
return Scaffold(
        //标题栏
       appBar: AppBar(
        leading: Image.asset("assets/images/apple.png"), //标题左侧图标
        title: Text("标题"),//导航栏标题
        actions: [ //右边的动作区域,可以放多个组件
          Image.asset("assets/images/apple.png"),
          Image.asset("assets/images/apple.png"),
          Image.asset("assets/images/apple.png"),
        ],
        elevation: 4.0,//控制下方阴影栏的坐标
        primary: true,//导航栏是否显示在任务栏顶部
        backgroundColor: Colors.red, //导航栏的背景颜色
        centerTitle: true,//标题是否居中
        titleSpacing: 2,//标题的间距
        toolbarOpacity: 0.8,//导航栏透明度,1.0表示完全不透明,0.0表示完全透明

      ),
      body: const Center( // 必须添加 body
        child: Text('页面内容区域'),
      ),
);

:当背景是渐变的,又需要使用标题栏的时候,就把标题栏的颜色设置成渐变的第一个颜色。

在组件之间加间距用SizedBox,根据需要加高或者宽

Dart 复制代码
SizedBox(height:x),
SizedBox(width:y),
Container是万能容器

使用场景:需要设置尺寸,边距,背景,边框时

Dart 复制代码
        Container(
            width:100, //宽
            height: 100,//高
            //外边距:有三个参数all(全部),only(单边),symmetric(水平或垂直),
            margin: EdgeInsets.symmetric(horizontal: 8),
            padding: EdgeInsets.all(8),//内边距的用法和外边距一样
            decoration: BoxDecoration(
              //颜色,withOpacity是用来设置透明度的,值的范围(0.0~1.0)
              color: Color(0xFF5091C9).withOpacity(0.3),
              //圆角效果
              borderRadius: BorderRadius.circular(20),
              //边距
              border: Border.all(color: Colors.black,width: 5),
              //设置形状
              shape: BoxShape.circle,
            ),
            child: Text(
               "内容"
            ),
          )

如果在容器内要设置文字在最中心,要用Center控件

情景1:设置渐变

Dart 复制代码
decoration:BoxDecoration(
    gradient:LinearGradient(
        begin:Alignment.centerLeft,
        end:Alignment.centerRight,
        colors:[
            Colors.red,
            Colors.black,
        ]
    )
)

//CentetLeft和centerRight是从左到右渐变
//topCenter和bottomCenter是从上到下的渐变

情景2:裁剪图片成圆形,不仅需要通过shape属性把容器设置成圆形,还需要裁剪图片

Dart 复制代码
ClipOval(child:Image.asset("assets/images/photo.png",width:90,height:90,fit:BoxFit.cover))

情景3:设置不同方向的圆角

Dart 复制代码
decoration: BoxDecoration(
  borderRadius: BorderRadius.only(
    topLeft:Radius.circular(8),
    topRight:Radius.circular(8),
    bottomLeft:Radius.circular(8),
    bottomRight:Radius.circular(8),
    ),
     
),                                                 

如果要加点击事件,要用GestureDetector包裹Container,让整行可点击需要加入一句代码

Dart 复制代码
behavior:HitTestBehavior:opaque
相关推荐
狂师11 小时前
比 Playwright 更给力,推荐一个AI Agent的浏览器自动化开源项目!
前端·开源·测试
IT_陈寒11 小时前
React hooks 闭包陷阱把我的状态吃掉了,原来问题出在这里
前端·人工智能·后端
壹方秘境11 小时前
使用ApiCatcher在 iOS 上像修改 hosts 一样自定义域名解析
前端·后端·客户端
柳杉11 小时前
可视化大屏设计器脚手架:从设计到交付的一站式方案
前端·three.js·数据可视化
kyriewen1 天前
我手写了一个 EventEmitter,面试官追问了 6 个问题——第 4 个我没答上来
前端·javascript·面试
IT_陈寒1 天前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
小林攻城狮1 天前
使用 Transport 节流解决 Vercel AI SDK 流式渲染卡死问题
前端·react.js
前端缘梦1 天前
告别 TS 运行时类型漏洞!Zod 完整入门实战教程(前端 / 全栈必备)
前端·react.js·全栈
the_answer1 天前
Webpack vs Vite 深度对比分析
前端·webpack
转转技术团队1 天前
验证码识别实战:前端不写页面,改训模型了?
前端