Flutter学习笔记三——Container 容器组件

官方文档地址

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

Alignment属性

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


建立一个容器,然后容器内加入一段文字 "Hello World", 并让它居中对齐。

 body: Center(
            child: Container(
              child: Text(
                "Hello World",
                style: TextStyle(fontSize: 40),
              ),
              alignment:Alignment.center,
            ),
          ),
alignment.png


我们从上图可以看到alignment的属性:

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

设置宽、高和颜色属性

设置宽高只需要在属性名称后面加入浮点型数字就可以了,颜色设置和之前一样。

  child: Container(
              child: Text(
                "Hello World",
                style: TextStyle(fontSize: 40.0,color: Colors.purple),
              ),
              alignment:Alignment.center,
              width: 400.0,
              height: 500.0,
              color: Colors.blueAccent,
            ),

padding属性

padding的属性就是一个内边距,指的是Container边缘和child内容的距离。

child: Container(
              child: Text(
                "Hello World",
                style: TextStyle(fontSize: 40.0,color: Colors.purple),
              ),
              alignment:Alignment.topLeft,
              width: 400.0,
              height: 500.0,
              color: Colors.blueAccent,
              padding: const EdgeInsets.all(20),
            ),

padding_all.png

或者使用EdgeInsets.fromLTRB(left, top, right, bottom),可以为左上右下设置不同的数值:

 child: Container(
              child: Text(
                "Hello World",
                style: TextStyle(fontSize: 40.0,color: Colors.purple),
              ),
              alignment:Alignment.topLeft,
              width: 400.0,
              height: 500.0,
              color: Colors.blueAccent,
//              padding: const EdgeInsets.all(20),
            padding: const EdgeInsets.fromLTRB(20, 50, 0, 0),
            ),

margin属性

margin是外边距,指的是container和外部元素的距离。用法与padding相同:

 child: Container(
              child: Text(
                "Hello World",
                style: TextStyle(fontSize: 40.0, color: Colors.purple),
              ),
              alignment: Alignment.topLeft,
              width: 400.0,
              height: 500.0,
              color: Colors.blueAccent,
//              padding: const EdgeInsets.all(20),
              padding: const EdgeInsets.fromLTRB(20, 50, 0, 0),
              margin: const EdgeInsets.all(20),
//            margin: const EdgeInsets.fromLTRB(20, 0, 0, 0),
            ),

decoration属性

decoration是 container 的修饰器,主要的功能是设置背景和边框。
需要注意的是,当使用decoration设置背景时,就不能使用color属性了。

 child: Container(
              child: Text(
                "Hello World",
                style: TextStyle(fontSize: 40.0, color: Colors.purple),
              ),
              alignment: Alignment.topLeft,
              width: 400.0,
              height: 500.0,
//              color: Colors.blueAccent,
//              padding: const EdgeInsets.all(20),
              padding: const EdgeInsets.fromLTRB(20, 50, 0, 0),
              margin: const EdgeInsets.all(20),
//            margin: const EdgeInsets.fromLTRB(20, 0, 0, 0),
              decoration: BoxDecoration(color: Colors.amber,),
            ),
decoration.png

下面把背景色设置为渐变色,代码如下:

 child: Container(
              child: Text(
                "Hello World",
                style: TextStyle(fontSize: 40.0, color: Colors.purple),
              ),
              alignment: Alignment.topLeft,
              width: 400.0,
              height: 500.0,
//              color: Colors.blueAccent,
//              padding: const EdgeInsets.all(20),
              padding: const EdgeInsets.fromLTRB(20, 50, 0, 0),
              margin: const EdgeInsets.all(20),
//            margin: const EdgeInsets.fromLTRB(20, 0, 0, 0),
              decoration: BoxDecoration(
                  color: const Color(0xFF00FFFF),
//              color: Color.fromRGBO(0, 255, 255, 0.1)
                  gradient: LinearGradient(
                      colors: [Colors.blueAccent, Colors.amber, Colors.purpleAccent],
                      begin: Alignment.centerLeft,
                      end: Alignment.centerRight,
                      tileMode: TileMode.mirror,
                      stops: [0.1, 0.5, 0.8])
//              gradient: RadialGradient(colors: [Colors.blueAccent,Colors.amber,Colors.purple])
//              gradient: SweepGradient(colors: [Colors.blueAccent,Colors.amber,Colors.purple],
//              center: Alignment.centerLeft)
                  ),
            ),
gradient.jpg

设置边框

设置边框可以在decoration里设置border属性:

 child: Container(
              child: Text(
                "Hello World",
                style: TextStyle(fontSize: 40.0, color: Colors.purple),
              ),
              alignment: Alignment.topLeft,
              width: 400.0,
              height: 500.0,
//              color: Colors.blueAccent,
//              padding: const EdgeInsets.all(20),
              padding: const EdgeInsets.fromLTRB(20, 50, 0, 0),
              margin: const EdgeInsets.all(20),
//            margin: const EdgeInsets.fromLTRB(20, 0, 0, 0),
//                  color: const Color(0xFF00FFFF),
              decoration: BoxDecoration(
//              color: Color.fromRGBO(0, 255, 255, 0.1)
                  gradient: LinearGradient(
                      colors: [
                        Colors.blueAccent,
                        Colors.purpleAccent,
                        Colors.amber,
                      ],
                      begin: Alignment.centerLeft,
                      end: Alignment.centerRight,
                      tileMode: TileMode.mirror,
                      stops: [0.1, 0.5, 0.8]),
//              gradient: RadialGradient(colors: [Colors.blueAccent,Colors.amber,Colors.purple])
//              gradient: SweepGradient(colors: [Colors.blueAccent,Colors.amber,Colors.purple],
//              center: Alignment.centerLeft)
//                border: Border.all(width: 2.0, color: Colors.red),
                  border: Border(
                      left: BorderSide(color: Colors.red, width: 3.0),
                      top: BorderSide(color: Colors.black, width: 3.0),
                      right: BorderSide(color: Colors.green, width: 3.0),
                      bottom: BorderSide(color: Colors.deepPurpleAccent, width: 5.0),
                  )),
            ),
border.jpg

从代码中可以看出,设置边界可以用Border.all(),或者分别设置左上右下的边界。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,699评论 1 92
  • HTML 5 HTML5概述 因特网上的信息是以网页的形式展示给用户的,因此网页是网络信息传递的载体。网页文件是用...
    阿啊阿吖丁阅读 10,156评论 0 0
  • CSS 指层叠样式表(Cascading Style Sheets),是一种用来为结构化文档(如 HTML 文档或...
    神齐阅读 6,485评论 0 14
  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 10,154评论 0 26
  • 浏览器与服务器的基本概念 浏览器(安装在电脑里面的一个软件) 作用: ①将网页内容渲染呈现给用户查看。 ②让用户通...
    云还灬阅读 4,895评论 0 0

友情链接更多精彩内容