flutter 常用布局方式

一、单子 Widget 嵌套布局:

单子 Widget是指只有一个子组件的Widget,这样布局Widget有三个:Container、Padding、Center


1、Container

Container,是一种允许在其内部添加其他控件的控件,也是 UI 框架中的一个常见概念。
在 Flutter 中,Container 本身可以单独作为控件存在(比如单独设置背景色、宽高),也可以作为其他控件的父级存在:Container 可以定义布局过程中子 Widget 如何摆放,以及如何展示。与其他框架不同的是,Flutter 的 Container 仅能包含一个子 Widget。

先简单写一个Container,尺寸200x200;在内部居左上放一个子Container,尺寸80x80

  • 代码如下:
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: _container(), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

 Container _container(){
    return Container(
      width: 200,
      height: 200,
      color: Colors.yellow,
      alignment: Alignment.topLeft,
      child: Container(
        color: Colors.red,
        width: 80,
        height: 80,
      ),
    );
  }
  • 展示效果:

a、padding

padding可以理解为是Container的"内边框",他会影响内部子控件的位置,示例代码:

  Container _container(){
    return Container(
      width: 200,
      height: 200,
      padding: EdgeInsets.all(20),
      color: Colors.yellow,
      child: Container(
        color: Colors.red,
      ),
    );
  }

上面代码把子Container的宽高限制去掉,这样它就可以尽可能的充满父Container,但是父Container有padding,导致它无法充满,效果如下:

b、margin

margin可以理解为是Container

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值