Flutter基础()

导航栏

appBar: AppBar()

复制代码
title: const Text('搜索')  //标题
复制代码
backgroundColor: Colors.blue   //背景颜色
复制代码
centerTitle: true //标题居中

leading 属性

作用 :

放置在应用栏左侧的控件,通常是一个图标按钮,用于导航或打开菜单。

Dart 复制代码
AppBar(
  leading: IconButton(
    icon: Icon(Icons.menu),
    onPressed: () {
      Scaffold.of(context).openDrawer(); // 打开侧边栏
    },
  ),
)

actions 属性

作用 :

放置在应用栏右侧的一组控件,通常是图标按钮,用于展示常用操作。

Dart 复制代码
AppBar(
  actions: [
    IconButton(
      icon: Icon(Icons.search),
      onPressed: () {
        // 打开搜索功能
      },
    ),
    IconButton(
      icon: Icon(Icons.more_vert),
      onPressed: () {
        // 打开更多选项菜单
      },
    ),
  ],
)

123


body

居中

Dart 复制代码
Scaffold(
  appBar: AppBar(title: Text('标题')),
  body: Center(
    child: Text('这是主要内容'),
  ),
);

垂直布局(Column)

  • MainAxisAlignment.start:子组件在主轴方向上对齐到起始位置。

  • MainAxisAlignment.end:子组件在主轴方向上对齐到结束位置。

  • MainAxisAlignment.spaceAround:子组件之间有等间距,但第一个和最后一个子组件与容器边缘的间距是其他间距的一半。

  • MainAxisAlignment.spaceBetween:子组件之间有等间距,但第一个和最后一个子组件分别对齐到容器的起始和结束位置。

  • MainAxisAlignment.spaceEvenly:子组件之间和子组件与容器边缘的间距都相等。

左边

Dart 复制代码
Scaffold(
  body: Column(
    mainAxisAlignment: MainAxisAlignment.center,   //mainAxisAlignment:这是 Row 或 Column 布局组件中的一个属性,用来指定子组件在主轴方向上的对齐方式。
    children: [
      Text('标题'),
      SizedBox(height: 16),
      ElevatedButton(
        onPressed: () {},
        child: Text('按钮'),
      ),
    ],
  ),
);

居中

Dart 复制代码
body: Center(
  child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      Text('标题'),
      SizedBox(height: 66),
      ElevatedButton(
        onPressed: () {},
        child: Text('按钮'),
      ),
    ],
  ),
)

123

复制代码
SizedBox
Dart 复制代码
Column(
  children: [
    Text('标题'),
    SizedBox(height: 16),
    Text('内容'),
  ],
)

效果 :Text('标题') 和 Text('内容') 之间会有一个高度为 16 像素的垂直间距。

水平布局(Row)

Dart 复制代码
Scaffold(
  body: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: [
      Icon(Icons.star, size: 48),
      Icon(Icons.star, size: 48),
      Icon(Icons.star, size: 48),
    ],
  ),
);

居中

Dart 复制代码
return Scaffold(
      appBar: AppBar(title: const Text('个人中心')),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Icon(Icons.star, size: 48),
            Icon(Icons.star, size: 48),
            Icon(Icons.star, size: 48),
          ],
        )
      ),
    );

容器

Dart 复制代码
Scaffold(
  body: Container(
    padding: EdgeInsets.all(16), //意思就是容器内 空白16个像素点 空格 设置内边距为上下左右各16像素
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start, // 子组件在水平方向上对齐到起始位置
      children: [
        Text('标题', style: TextStyle(fontSize: 24)), // 显示标题,字体大小为24
        SizedBox(height: 16), // 添加一个高度为16像素的垂直间距
        Expanded( // 让子组件(ListView)占据剩余空间
          child: ListView(
            children: [
              ListTile(title: Text('项目 1')), // 列表项1
              ListTile(title: Text('项目 2')), // 列表项2
              ListTile(title: Text('项目 3')), // 列表项3
            ],
          ),
        ),
      ],
    ),
  ),
);

123

相关推荐
恋猫de小郭20 小时前
Android CLI 支持 AI Agent 通过 Device Streaming 调试云真机
android·前端·flutter
m0_738185822 天前
Flutter 鸿蒙化实战:flutter_scankit 适配 OpenHarmony,华为 ScanKit 扫码
flutter·华为·harmonyos·鸿蒙
事圆则缓2 天前
Flutter 开发鸿蒙实战:从 OpenHarmony 适配版到 HAP 构建与插件接入
flutter·华为·harmonyos
天空之城--2 天前
Android一周动态:Android 18首次官宣、Compose Material3 1.4转正(5趋势+5资讯)
android·人工智能·flutter·架构·android jetpack
恋猫de小郭2 天前
Meta 分享怎么用 AI 迁移 Compose 项目不烧心
android·前端·flutter
EatFan2 天前
Flutter 鸿蒙适配实践 2026:从环境搭建、混合渲染到 OpenTelemetry 性能基建
flutter·华为·harmonyos·openharmony·opentelemetry·鸿蒙适配·impeller
m0_738185822 天前
Flutter 鸿蒙化实战:flutter_secure_storage 适配 OpenHarmony,敏感数据安全存储
安全·flutter·华为·harmonyos·鸿蒙
传奇开心果编程3 天前
【现代声明式UI学与练】第1课 从命令式UI到声明式UI
学习·flutter·ui·swiftui·react·android jetpack
m0_738185823 天前
Flutter 鸿蒙化实战:flutter_nfc_kit 适配 OpenHarmony,NFC 读写一步到位
flutter·华为·harmonyos·鸿蒙
恋猫de小郭3 天前
KMP 又改了编译流程, Separate Compilation 禁止了 `commonMain` 的依赖穿透
android·前端·flutter