Flutter学习之旅 - 页面布局Padding、Column、Flex、Expanded

Padding

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return const Padding(
      padding: EdgeInsets.all(50),
      child: Text("你好"),
    );
  }
}

就是给上下左右来点边距,功能单一但是占用内存少

线性布局组件(Column和Row)

class _IconContainerWidget extends StatelessWidget {
  const _IconContainerWidget({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      height: 100,
      width: 100,
      color: Colors.red,
      child: const Icon(
        Icons.home,
        color: Colors.white,
        size: 28,
      ),
    );
  }
}

自定义组件传参

class _IconContainerWidget extends StatelessWidget {
  Color color;
  IconData icon;
  _IconContainerWidget(this.icon, {Key? key, required this.color})
      : super(key: key);

  
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      height: 100,
      width: 100,
      color: color,
      child: Icon(
        icon,
        color: Colors.white,
        size: 28,
      ),
    );
  }
}
水平布局组件(Row)
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Row(
      children: [
        _IconContainerWidget(
          Icons.home,
          color: Colors.red,
        ),
        _IconContainerWidget(
          Icons.settings,
          color: Color.fromRGBO(200, 200, 200, 1),
        )
      ],
    );
  }
}

在这里插入图片描述

垂直布局组件(Column)

写法是_IconContainerWidget(Icons.white,color:Colors.red)

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Column(
      children: [
        _IconContainerWidget(
          Icons.home,
          color: Colors.red,
        ),
        _IconContainerWidget(
          Icons.settings,
          color: Color.fromRGBO(200, 200, 200, 1),
        )
      ],
    );
  }
}

在这里插入图片描述

弹性布局(Flex&Expanded)

Expanded组件必须放在Row(行)Column(列)又或者Flex组件中

Expanded
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          flex: 1,
          child: _IconContainerWidget(
            Icons.home,
            color: Colors.red,
          ),//在这个Expanded组件中设置宽高是没有用的
        ),
        Expanded(
          flex: 2,
          child: _IconContainerWidget(
            Icons.settings,
            color: Color.fromRGBO(200, 200, 0, 1),
          ),
        )
      ],
    );
  }
}

在这里插入图片描述

Flex
class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Flex(
      direction: Axis.horizontal, //Axis.horizontal:行;Axis.vertical:列
      children: [
        Expanded(
          flex: 1,
          child: _IconContainerWidget(
            Icons.home,
            color: Colors.red,
          ), //在这个Expanded组件中设置宽高是没有用的
        ),
        Expanded(
          flex: 2,
          child: _IconContainerWidget(
            Icons.search, //搜索
            color: Color.fromRGBO(200, 200, 0, 1),
          ),
        )
      ],
    );
  }
}

可以写行(Axis.horizontal)或者写列(Axis.vertical)

达到父元素的尺寸

double.infinitydouble.maxFinite都能达到父元素的尺寸

Container(
      alignment: Alignment.center,
      height: double.infinity,
      width: double.infinity,
      ...
);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

结城明日奈是我老婆

支持一下一直热爱程序的菜鸟吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值