Flutter笔记(7)Container容器布局

Container概述

Container在flutter太常见了,官方给出的是一个结合了绘制Widget(padding)、定位Widget(positioning)以及尺寸Widget(sizing)的widget。

Container构造函数

  Container({
    Key key,
    this.alignment,
    this.padding,
    Color color,
    Decoration decoration,
    this.foregroundDecoration,
    double width,
    double height,
    BoxConstraints constraints,
    this.margin,
    this.transform,
    this.child,
  })

依据构造函数可以看出以下:

  • 对齐(alignment)
  • 调节自身的尺寸适应子节点
  • 采用width、height以及constraints布局
  • 扩展自身去适应父节点

Container示例代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Container布局容器示例',
      home: new LayoutDemo(),
    );
  }
}

class LayoutDemo extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    Widget container = new Container(
      //添加装饰效果
      decoration: new BoxDecoration(
        color: Colors.grey
      ),
      child: new Column(
        children: <Widget>[
          new Row(
            children: <Widget>[
              //使用Expanded防止内容溢出
              new Expanded(
                  child: new Container(
                    width: 150.0,
                    height: 150.0,
                    decoration: new BoxDecoration(
                      //上下左右边框设置为10.0.颜色为蓝色
                      border: new Border.all(width: 10.0,color: Colors.blueGrey),
                      //设置上下左右边框的弧度设置为8.0
                      borderRadius:
                        const BorderRadius.all(const Radius.circular(8.0)),
                    ),
                    //上下左右增加边距
                    margin: const EdgeInsets.all(4.0),
                    //添加图片
                    child: new Image.asset('images/icon_1.png'),
                  ),
              ),
              new Expanded(
                child: new Container(
                  width: 150.0,
                  height: 150.0,
                  decoration: new BoxDecoration(
                    //上下左右边框设置为10.0.颜色为蓝色
                    border: new Border.all(width: 10.0,color: Colors.blueGrey),
                    //设置上下左右边框的弧度设置为8.0
                    borderRadius:
                    const BorderRadius.all(const Radius.circular(8.0)),
                  ),
                  //上下左右增加边距
                  margin: const EdgeInsets.all(4.0),
                  //添加图片
                  child: new Image.asset('images/icon_2.png'),
                ),
              ),
              new Expanded(
                child: new Container(
                  width: 150.0,
                  height: 150.0,
                  decoration: new BoxDecoration(
                    //上下左右边框设置为10.0.颜色为蓝色
                    border: new Border.all(width: 10.0,color: Colors.blueGrey),
                    //设置上下左右边框的弧度设置为8.0
                    borderRadius:
                    const BorderRadius.all(const Radius.circular(8.0)),
                  ),
                  //上下左右增加边距
                  margin: const EdgeInsets.all(4.0),
                  //添加图片
                  child: new Image.asset('images/icon_3.png'),
                ),
              ),
              new Expanded(
                child: new Container(
                  width: 150.0,
                  height: 150.0,
                  decoration: new BoxDecoration(
                    //上下左右边框设置为10.0.颜色为蓝色
                    border: new Border.all(width: 10.0,color: Colors.blueGrey),
                    //设置上下左右边框的弧度设置为8.0
                    borderRadius:
                    const BorderRadius.all(const Radius.circular(8.0)),
                  ),
                  //上下左右增加边距
                  margin: const EdgeInsets.all(4.0),
                  //添加图片
                  child: new Image.asset('images/icon_1.png'),
                ),
              ),
            ],
          )
        ],
      ),
    );
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Container布局容器示例'),
      ),
      body: container,
    );
  }
}

更多文章资源,欢迎关注:程序猿阵线联盟

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

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

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

打赏作者

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

抵扣说明:

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

余额充值