Flutter基础(基础)

[复制链接]
发表于 2025-9-11 17:52:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
导航栏

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

作用
放置在应用栏左侧的控件,通常是一个图标按钮,用于导航或打开菜单。
  1. AppBar(
  2.   leading: IconButton(
  3.     icon: Icon(Icons.menu),
  4.     onPressed: () {
  5.       Scaffold.of(context).openDrawer(); // 打开侧边栏
  6.     },
  7.   ),
  8. )
复制代码
actions 属性

作用
放置在应用栏右侧的一组控件,通常是图标按钮,用于展示常用操作。
  1. AppBar(
  2.   actions: [
  3.     IconButton(
  4.       icon: Icon(Icons.search),
  5.       onPressed: () {
  6.         // 打开搜索功能
  7.       },
  8.     ),
  9.     IconButton(
  10.       icon: Icon(Icons.more_vert),
  11.       onPressed: () {
  12.         // 打开更多选项菜单
  13.       },
  14.     ),
  15.   ],
  16. )
复制代码
123

body

居中
  1. Scaffold(
  2.   appBar: AppBar(title: Text('标题')),
  3.   body: Center(
  4.     child: Text('这是主要内容'),
  5.   ),
  6. );
复制代码
垂直布局(Column)

  •         MainAxisAlignment.start:子组件在主轴方向上对齐到起始位置。
  •         MainAxisAlignment.end:子组件在主轴方向上对齐到结束位置。
  •         MainAxisAlignment.spaceAround:子组件之间有等间距,但第一个和最后一个子组件与容器边缘的间距是其他间距的一半。
  •         MainAxisAlignment.spaceBetween:子组件之间有等间距,但第一个和最后一个子组件分别对齐到容器的起始和结束位置。
  •         MainAxisAlignment.spaceEvenly:子组件之间和子组件与容器边缘的间距都相称。
左边
  1. Scaffold(
  2.   body: Column(
  3.     mainAxisAlignment: MainAxisAlignment.center,   //mainAxisAlignment:这是 Row 或 Column 布局组件中的一个属性,用来指定子组件在主轴方向上的对齐方式。
  4.     children: [
  5.       Text('标题'),
  6.       
  7. SizedBox(height: 16),
  8.       ElevatedButton(
  9.         onPressed: () {},
  10.         child: Text('按钮'),
  11.       ),
  12.     ],
  13.   ),
  14. );
复制代码
居中
  1. body: Center(
  2.   child: Column(
  3.     mainAxisAlignment: MainAxisAlignment.center,
  4.     children: [
  5.       Text('标题'),
  6.       
  7. SizedBox(height: 66),
  8.       ElevatedButton(
  9.         onPressed: () {},
  10.         child: Text('按钮'),
  11.       ),
  12.     ],
  13.   ),
  14. )
复制代码
123
  1. SizedBox
复制代码
  1. Column(  children: [    Text('标题'),   
  2. SizedBox(height: 16),    Text('内容'),  ],)
复制代码
效果:Text('标题') 和 Text('内容') 之间会有一个高度为 16 像素的垂直间距。

程度布局(Row)
  1. Scaffold(
  2.   body: Row(
  3.     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  4.     children: [
  5.       Icon(Icons.star, size: 48),
  6.       Icon(Icons.star, size: 48),
  7.       Icon(Icons.star, size: 48),
  8.     ],
  9.   ),
  10. );
复制代码
居中
  1. return Scaffold(
  2.       appBar: AppBar(title: const Text('个人中心')),
  3.       body: Center(
  4.         child: Row(
  5.           mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  6.           children: [
  7.             Icon(Icons.star, size: 48),
  8.             Icon(Icons.star, size: 48),
  9.             Icon(Icons.star, size: 48),
  10.           ],
  11.         )
  12.       ),
  13.     );
复制代码
容器
  1. Scaffold(  body: Container(    padding: EdgeInsets.all(16), //意思就是容器内 空白16个像素点 空格 设置内边距为上下左右各16像素    child: Column(      crossAxisAlignment: CrossAxisAlignment.start, // 子组件在程度方向上对齐到起始位置      children: [        Text('标题', style: TextStyle(fontSize: 24)), // 表现标题,字体巨细为24        
  2. 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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表