P07. Flutter Container容器组件的使用

目录
文章目录隐藏
  1. Alignment 属性
  2. 设置宽、高和颜色属性
  3. padding 属性
  4. margin 属性
  5. decoration 属性

Container(容器控件)在 Flutter 是经常使用的控件,它就相当于我们 HTML 里的<div>标签,每个页面或者说每个视图都离不开它。

Alignment 属性

其实容器的作用就是方便我们进行布局的,Flutter 这点也作的很好,我们先来看容器属性中的Alignment

这个属性针对的是Containerchild的对齐方式,也就是容器子内容的对齐方式,并不是容器本身的对齐方式。

先作一个效果:建立一个容器,然后容器内加入一段文字Hello Mybj, 并让它居中对齐。

import 'package:flutter/material.dart';
void main () => runApp(MyApp());

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context ){
      return MaterialApp(
        title:'Text widget',
        home:Scaffold(
          body:Center(
           child:Container(
             child:new Text('Hello Mybj',style: TextStyle(fontSize: 40.0),),
             alignment: Alignment.center,
           ),
          ),
        ),
      );
  }
}

效果如下:

Alignment 属性

这时候可以看见,我们的文本已经居中显示在手机屏幕上了。当然它的对齐方式还有如下几种:

  • bottomCenter:下部居中对齐。
  • botomLeft: 下部左对齐。
  • bottomRight:下部右对齐。
  • center:纵横双向居中对齐。
  • centerLeft:纵向居中横向居左对齐。
  • centerRight:纵向居中横向居右对齐。
  • topLeft:顶部左侧对齐。
  • topCenter:顶部居中对齐。
  • topRight: 顶部居左对齐。

设置宽、高和颜色属性

设置宽、高和颜色属性是相对容易的,只要在属性名称后面加入浮点型数字就可以了,比如要设置宽是 500,高是 400,颜色为亮蓝色。代码如下:

child:Container(
  child:new Text('Hello Mybj',style: TextStyle(fontSize: 40.0),),
  alignment: Alignment.center,
  width:500.0,
  height:400.0,
  color: Colors.lightBlue,
),

效果如下:

flutter 设置 width、height 和 color 属性

padding 属性

padding的属性就是一个内边距,它和你使用的前端技术 CSS 里的padding表现形式一样,指的是Container边缘和child内容的距离。先来看一个内边距为 20 的例子。具体代码如下:

child:Container(
  child:new Text('Hello Mybj',style: TextStyle(fontSize: 40.0),),
  alignment: Alignment.topLeft,
  width:500.0,
  height:400.0,
  color: Colors.lightBlue,
  padding:const EdgeInsets.all(20.0),
),

效果如下:

flutter padding 属性的使用

上面主要的 padding 代码就一句。

padding:const EdgeInsets.all(20.0),

这句的意思是设置Container的内边距是 20,左右上下全部为 20,这看起来非常容易。那我们再加大一点难度。如果上边距为 30,左边距为 20,这时候EdgeInsets.all()就满足不了我们了。

这个时候我们用EdgeInsets.fromLTRB(value1,value2,value3,value4) 可以满足我们的需求,LTRB分别代表左、上、右、下。

那我们设置上边距为 30,左边距为 20,就可以用下面的代码来编写。

padding:const EdgeInsets.fromLTRB(20.0,30.0,0.0,0.0),

这时候我们的结果就变成了下图。

flutter padding 上下左右效果

有了这两个属性,基本就可以满足我们的工作需要了。

margin 属性

会了padding属性的设置,margin就变的非常容易了,因为方法基本上一样。不过margin是外边距,只的是 container 和外部元素的距离。

现在要把container的外边距设置为 10 个单位,代码如下:

child:Container(
  child:new Text('Hello Mybj',style: TextStyle(fontSize: 40.0),),
  alignment: Alignment.topLeft,
  width:500.0,
  height:400.0,
  color: Colors.lightBlue,
  padding:const EdgeInsets.fromLTRB(10.0,30.0,0.0,0.0),
  margin: const EdgeInsets.all(10.0),
),

此时我们的效果变成:

flutter margin 属性使用

图中可以看出,已经有了明显的外边距。

主要代码还是:

margin: const EdgeInsets.all(10.0),

当然你也可以分别设置不同的外边距,方法也是使用EdgeInsets.fromLTRB(value1,value2,value3,value4,)

那我们设置上边距为 30,左边距为 20,右边距 15,下边距 10 就可以用下面的代码来编写。

margin: const EdgeInsets.fromLTRB(30.0, 20.0, 15.0, 10.0),

效果如下:

flutter margin 属性的不同的外边距设置

decoration 属性

decoration是 container 的修饰器,主要的功能是设置背景和边框。

比如你需要给背景加入一个渐变,这时候需要使用BoxDecoration这个类,代码如下(需要注意的是如果你设置了decoration,就不要再设置color属性了,因为这样会冲突)。

child:Container(
  child:new Text('Hello Mybj',style: TextStyle(fontSize: 40.0),),
  alignment: Alignment.topLeft,
  width:500.0,
  height:400.0,
  padding:const EdgeInsets.fromLTRB(10.0,30.0,0.0,0.0),
  margin: const EdgeInsets.fromLTRB(30.0, 20.0, 15.0, 10.0),
  decoration:new BoxDecoration(
    gradient:const LinearGradient(
      colors:[Colors.lightBlue,Colors.greenAccent,Colors.purple]
    )
  ),
),

上面的代码去掉了 color 的设置,这时候 container 的背景就变成了渐变颜色,如下图。

flutter 的 decoration 属性使用

设置边框

设置边框可以在decoration里设置border属性,比如你现在要设置一个红色边框,宽度为 2。代码如下:

child:Container(
  child:new Text('Hello Mybj',style: TextStyle(fontSize: 40.0),),
  alignment: Alignment.topLeft,
  width:500.0,
  height:400.0,
  padding:const EdgeInsets.fromLTRB(10.0,30.0,0.0,0.0),
  margin: const EdgeInsets.fromLTRB(30.0, 20.0, 15.0, 10.0),
  decoration:new BoxDecoration(
    gradient:const LinearGradient(
      colors:[Colors.lightBlue,Colors.greenAccent,Colors.purple]
    ),
    border:Border.all(width:2.0,color:Colors.red)
  ),
),

 

flutter decoration 里设置 border 属性

关键代码其实就是:

border:Border.all(width:2.0,color:Colors.red),

本文就到这里,希望小伙伴们都能动手练习起来。在这里附上全部代码,方便小伙伴们学习。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Text widget',
      home: Scaffold(
        body: Center(
          child: Container(
            child: Container(
              child: new Text(
                'Hello Mybj',
                style: TextStyle(fontSize: 40.0),
              ),
              alignment: Alignment.topLeft,
              width: 500.0,
              height: 400.0,
              // color: Colors.lightBlue,
              padding: const EdgeInsets.fromLTRB(20.0, 30.0, 0.0, 0.0),
              margin: const EdgeInsets.fromLTRB(30.0, 20.0, 15.0, 10.0),
              decoration: new BoxDecoration(
                  gradient: const LinearGradient(colors: [
                    Colors.lightBlue,
                    Colors.greenAccent,
                    Colors.purple
                  ]),
                  border: Border.all(width: 2.0, color: Colors.red)),
            ),
            alignment: Alignment.center,
          ),
        ),
      ),
    );
  }
}

「点点赞赏,手留余香」

0

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » P07. Flutter Container容器组件的使用

发表回复