【flutter】Container组件

系统管理员 2021-09-24 05:44 588阅读 0赞

Container是flutter中广泛使用的容器类组件,在以下场景会用到Container:

  1. 设置宽高:flutter中大部分组件不能设置宽高,需要依赖容器。
  2. 添加背景颜色
  3. 添加背影图像
  4. 添加内外边距:padding和margin
  5. 添加边框
  6. 设置圆角
  7. 设置child对齐:居中、居左、居右、居上、居下或偏移
  8. 设置变换:旋转或变形

因此,Container是一个非常基础的组件,同时也是用途广泛的组件,熟悉它非常有必要。

布局特点

Container组件的大小取决于

  1. 如果设置的宽高不在constraints范围内,设置的宽高会无效;
  2. 如果它有child,它会尽可能小。如果不设置宽高和约束,相当于child的大小。如果设置了宽高和约束,以设置最小有效值为准;
  3. 如果它没有child,它会尽可能大。如果不设置宽度和约束,相当于parent的大小。如果设置了宽高和约束,以设置的最大有效值为准。

constraints是一个约束大小的属性,相当于一个size范围,使Container不会小于最小size,不会超过最大size。如果没有设置此属性,就是无限制:最小宽高是0,最大宽高是parent的宽高。

构造函数

  1. Container(
  2. { Key key,
  3. AlignmentGeometry alignment,
  4. EdgeInsetsGeometry padding,
  5. Color color,
  6. Decoration decoration,
  7. Decoration foregroundDecoration,
  8. double width,
  9. double height,
  10. BoxConstraints constraints,
  11. EdgeInsetsGeometry margin,
  12. Matrix4 transform,
  13. Widget child}
  14. )

属性

  • alignment:child对齐的属性,可以设置居中、居左、居右、居上、居下等等。
    alignment已经在我的另外一篇文章中详细说明:Flutter Stack组件
  • padding:内边距。width和height包含padding值。
  • color:背景颜色。
  • decoration:设置装饰,比如边框、圆角、背景图片等。不能给Container同时设置decoration和color属性,如果要同时设置,给decoration设置color就可以。
  • foregroundDecoration,设置前景装饰。
  • width:宽度。
  • height:高度。
  • constraints:大小范围约束,constraints有四个属性:minWidth、minHeight、maxWidth、maxHeight。
  • margin: 外边距。
  • transform:变换效果,比如翻转、旋转、变形等。
  • child:子组件,可以不设置。

示例一:约束、对齐与变换。

  1. Container(
  2. constraints: BoxConstraints.expand(
  3. height: Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0,
  4. ),
  5. padding: const EdgeInsets.all(8.0),
  6. color: Colors.blue[600],
  7. alignment: Alignment.center,
  8. child: Text('Hello World',
  9. style: Theme.of(context)
  10. .textTheme
  11. .display1
  12. .copyWith(color: Colors.white)),
  13. transform: Matrix4.rotationZ(0.1),
  14. )

运行效果:
在这里插入图片描述

示例二:给Container添加背景图片

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MaterialApp(
  3. home: Scaffold(
  4. appBar: AppBar(title: Text("Container demo")),
  5. body: new Container(
  6. constraints: new BoxConstraints.expand(width: 300,height: 300),
  7. decoration: new BoxDecoration(
  8. border: new Border.all(width: 2.0, color: Colors.red),
  9. color: Colors.grey,
  10. borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
  11. image: new DecorationImage(
  12. image: new AssetImage('graphyics/face.jpg'),
  13. ),
  14. ),
  15. padding: const EdgeInsets.all(8.0),
  16. alignment: Alignment.center,
  17. child: new Text('Hello Container',style: TextStyle(color: Colors.red),),
  18. ))));

运行效果:
在这里插入图片描述

示例三:不设置宽高和约束,有child,size会尽可能小

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MaterialApp(
  3. home: Scaffold(
  4. appBar: AppBar(title: Text("Container demo")),
  5. body: Container(
  6. child: Text("Hello,Container",
  7. style: TextStyle(fontSize: 20, color: Colors.white)),
  8. color: Colors.red))));

运行效果:
在这里插入图片描述

示例四:不设置宽高和约束,没有child,size会尽可能大

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MaterialApp(
  3. home: Scaffold(
  4. appBar: AppBar(title: Text("Container demo")),
  5. body: Container(
  6. color: Colors.green))));

运行效果:
在这里插入图片描述

示例五:宽高不在约束范围内会无效。

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MaterialApp(
  3. home: Scaffold(
  4. appBar: AppBar(title: Text("Container demo")),
  5. body: Container(
  6. constraints: BoxConstraints(
  7. minWidth: 200, minHeight: 200, maxWidth: 400, maxHeight: 400),
  8. width: 20,
  9. height: 20,
  10. child: Text("Hello,Container",
  11. style: TextStyle(fontSize: 20, color: Colors.white)),
  12. color: Colors.green))));

运行效果(绝色区域宽高是200):
在这里插入图片描述

示例六:没有设置宽高但是设置了child,会尽可能小,不会小于child的size。

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MaterialApp(
  3. home: Scaffold(
  4. appBar: AppBar(title: Text("Container demo")),
  5. body: Container(
  6. constraints: BoxConstraints(
  7. minWidth: 20, minHeight: 20, maxWidth: 400, maxHeight: 400),
  8. child: Text("Hello,Container",
  9. style: TextStyle(fontSize: 20, color: Colors.white)),
  10. color: Colors.green))));

在这里插入图片描述

示例七:设置了child和宽高会尽可能小,以有效的最小值为准(minWidth或width)

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MaterialApp(
  3. home: Scaffold(
  4. appBar: AppBar(title: Text("Container demo")),
  5. body: Container(
  6. constraints: BoxConstraints(
  7. minWidth: 10, minHeight: 10, maxWidth: 400, maxHeight: 400),
  8. width: 100,
  9. height: 100,
  10. child: Text("Hello,Container",
  11. style: TextStyle(fontSize: 20, color: Colors.white)),
  12. color: Colors.green))));

在这里插入图片描述
图中绿色部分宽是100,高是100。

发表评论

表情:
评论列表 (有 0 条评论,588人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue

    为什么使用组件 JavaScript能力的复用。Vue.js的组件提高重复性,让代码可以复用。 组件的用法 组件在使用前先需要注册。注册分为两种方式:全局注册和局

    相关 Storm

    一 Strom主从结构 ![SouthEast][] 二 主从结构与对称结构 主从结构:简单、高效,但主节点存在单点问题 对称结构:复杂、效率较低,但无单点问题,更加可

    相关 Vue

    Vue组件 1. 什么是组件 1. 组件的概念:组件即自定义控件,是Vue.js最强大的功能之一 2. 组件的用途:组件能够封装可重用代码,扩展html标签功能