Flutter之Container

1、Container介绍

我们先看它的构造方法

      Container({
        Key key,
        this.alignment,
        this.padding, //容器内补白,属于decoration的装饰范围
        Color color, // 背景色
        Decoration decoration, // 背景装饰
        Decoration foregroundDecoration, //前景装饰
        double width,//容器的宽度
        double height, //容器的高度
        BoxConstraints constraints, //容器大小的限制条件
        this.margin,//容器外补白,不属于decoration的装饰范围
        this.transform, //变换
        this.child,
        this.clipBehavior = Clip.none,
      })

Container是一个组合类容器,它本身不对应具体的RenderObject,它是DecoratedBox、ConstrainedBox、Transform、Padding、Align等组件组合的一个多功能容器,所以我们只需通过一个Container组件可以实现同时需要装饰、变换、限制的场景


2、代码测试

代码测试1、

      @override
      Widget build(BuildContext context) {
          return MaterialApp(
              title: 'open url',
              home: Scaffold(
                appBar: AppBar(
                  title: Text('hello flutter'),
                ),
                body: Container(
                   margin: EdgeInsets.only(top: 50, left: 50),
                   constraints: BoxConstraints.tightFor(width: 200, height: 150),
                   decoration: BoxDecoration(
                        gradient: RadialGradient( //背景径向渐变
                            colors: [Colors.red, Colors.orange],
                            center: Alignment.topLeft,
                            radius: .98
                        ),
                        borderRadius:BorderRadius.all(Radius.circular(5)),
                        boxShadow: [ //卡片阴影
                          BoxShadow(
                              color: Colors.black54,
                              offset: Offset(2.0, 2.0),
                              blurRadius: 4.0
                          )
                        ]
                   ),
                   alignment: Alignment.center,
                   child: Text("chenyu", style: TextStyle(color: Colors.white, fontSize: 40.0)),
                ),
              ),
          );
      }

代码测试2、

        @override
      Widget build(BuildContext context) {
          return MaterialApp(
              title: 'open url',
              home: Scaffold(
                appBar: AppBar(
                  title: Text('hello flutter'),
                ),
                body: Padding(
                  padding: EdgeInsets.all(30),
                  child: DecoratedBox(
                    decoration: BoxDecoration(
                       color: Colors.blue
                    ),
                    child: Text("chenyu", style: TextStyle(color: Colors.white, fontSize: 40.0)),
                  ),
                )
              ),
          );
      }

代码测试3、

        @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'open url',
          home: Scaffold(
              appBar: AppBar(
                title: Text('hello flutter'),
              ),
              body: DecoratedBox(
                decoration: BoxDecoration(
                  color: Colors.blue
                ),
                child: Padding(
                   padding: EdgeInsets.all(40),
                   child: Text("chenyu", style: TextStyle(color: Colors.white, fontSize: 40.0)),
                ),
              ),
          ),
        );
      }

更多请见:http://www.mark-to-win.com/tutorial/51851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值