Container是容器组件,类似于H5中的

Container包含属性

Container常用属性如下:

  • Container
    • child:子视图
    • alignment:子视图的对齐方式
      • topLeft:顶部左对齐
      • topCenter:顶部居中对齐
      • topRight:顶部右对齐
      • centerLeft:中间左对齐
      • center:中间对齐
      • centerRight:中间右对齐
      • bottomLeft:底部左对齐
      • bottomCenter:底部居中对齐
      • bottomRight:底部右对齐
    • color:背景颜色
    • width:宽度
    • height:高度
    • padding:子视图距Container的边距
    • margin:Container距父视图的边距
    • decoration:装饰

子视图对齐方式-alignment

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. title: 'Container Learn',
  6. home: Scaffold(
  7. body: Center(
  8. child: Container(
  9. child: Text(
  10. 'Alignment center',
  11. style: TextStyle(fontSize: 40.0),
  12. ),
  13. alignment: Alignment.center,
  14. width: 500.0,
  15. height: 400.0,
  16. color: Colors.lightBlue,
  17. ),
  18. ),
  19. ),
  20. );
  21. }
  22. }

效果图如下:
Flutter组件基础——Container - 图1Flutter组件基础——Container - 图2Flutter组件基础——Container - 图3

Container宽、高

width和height的设置,直接是固定的值。

没有类似H5那种’100%’的设置。所以如果想要设置Container为屏幕宽高时,可以用以下的方法:

方法一:

  1. import 'dart:ui';
  2. final width = window.physicalSize.width;
  3. final height = window.physicalSize.height;
  4. Container(
  5. color: Colors.red,
  6. width: width,
  7. child: Text("宽度有多宽"),
  8. )

方法二:

  1. Container(
  2. color: Colors.red,
  3. width: double.infinity,
  4. child: Text("宽度有多宽"),
  5. )

子视图距Container的边距-padding

padding设置的是子视图,距Container的边距,两种设置方式,通常有两种设置方式,EdgeInsets.all常用于设置所有边距都一致;EdgeInsets.fromLTRB用于设置指定边距(LTRB对应的Left、Top、Right、Bottom)。代码如下:

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. title: 'Container Learn',
  6. home: Scaffold(
  7. body: Center(
  8. child: Container(
  9. child: Text(
  10. 'padding left: 10, top: 20',
  11. style: TextStyle(fontSize: 40.0),
  12. ),
  13. alignment: Alignment.topLeft,
  14. width: 500.0,
  15. height: 400.0,
  16. color: Colors.lightBlue,
  17. padding: const EdgeInsets.fromLTRB(10.0, 20.0, 0.0, 0.0),
  18. // padding: const EdgeInsets.all(20),
  19. ),
  20. ),
  21. ),
  22. );
  23. }
  24. }

显示效果如下:

Flutter组件基础——Container - 图4

contianer距离父视图的边距-margin

margin的设置和padding相同,效果对比,可以先注释width和height,代码如下:

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. title: 'Container Learn',
  6. home: Scaffold(
  7. body: Center(
  8. child: Container(
  9. child: Text(
  10. 'margin all 30',
  11. style: TextStyle(fontSize: 40.0),
  12. ),
  13. alignment: Alignment.topLeft,
  14. // width: 500.0,
  15. // height: 400.0,
  16. color: Colors.lightBlue,
  17. // padding: const EdgeInsets.fromLTRB(10.0, 20.0, 0.0, 0.0),
  18. // padding: const EdgeInsets.all(20),
  19. margin: const EdgeInsets.all(30.0),
  20. ),
  21. ),
  22. ),
  23. );
  24. }
  25. }

效果如下:

Flutter组件基础——Container - 图5

container的decoration

decoration可用于设置背景色、背景渐变效果、边框效果等,需要注意decoration和color不能同时设置,如果需要设置,可以通过在decoration中设置color来实现,代码如下:

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. title: 'Container Learn',
  6. home: Scaffold(
  7. body: Center(
  8. child: Container(
  9. child: Text(
  10. 'margin all 30',
  11. style: TextStyle(fontSize: 40.0),
  12. ),
  13. alignment: Alignment.topLeft,
  14. width: 500.0,
  15. height: 400,
  16. // color: Colors.lightBlue,
  17. // padding: const EdgeInsets.fromLTRB(10.0, 20.0, 0.0, 0.0),
  18. // padding: const EdgeInsets.all(20),
  19. // margin: const EdgeInsets.all(30.0),
  20. decoration: BoxDecoration(
  21. gradient: const LinearGradient(colors: [
  22. Colors.lightBlue,
  23. Colors.greenAccent,
  24. Colors.purple,
  25. ]),
  26. border: Border.all(width: 10.0, color: Colors.red),
  27. color: Colors.lightBlue)),
  28. ),
  29. ),
  30. );
  31. }
  32. }

效果如下:

Flutter组件基础——Container - 图6

报错:

The following assertion was thrown building MyApp(dirty):
Cannot provide both a color and a decoration
To provide both, use “decoration: BoxDecoration(color: color)”.
‘package:flutter/src/widgets/container.dart’:
Failed assertion: line 274 pos 15: ‘color == null || decoration == null’

报错代码如下:

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return MaterialApp(
  5. title: 'Container Learn',
  6. home: Scaffold(
  7. body: Center(
  8. child: Container(
  9. child: Text(
  10. 'Container Text',
  11. style: TextStyle(fontSize: 40.0),
  12. ),
  13. alignment: Alignment.topLeft,
  14. color: Colors.lightBlue,
  15. padding: const EdgeInsets.fromLTRB(10.0, 30.0, 0.0, 0.0),
  16. margin: const EdgeInsets.all(30.0),
  17. decoration: BoxDecoration(
  18. gradient: const LinearGradient(colors: [
  19. Colors.lightBlue,
  20. Colors.greenAccent,
  21. Colors.purple,
  22. ]),
  23. border: Border.all(width: 10.0, color: Colors.red)),
  24. ),
  25. ),
  26. ),
  27. );
  28. }
  29. }

原因:Container的color和decoration不能同时设置,如果需要设置这两个,可以通过设置BoxDecoration(color: color)来实现

参考

Flutter Conatiner Doc
Flutter免费视频第二季-常用组件