Flutter基础控件之Container

Container源码

class Container extends StatelessWidget {
  /// Creates a widget that combines common painting, positioning, and sizing widgets.
  ///
  /// The `height` and `width` values include the padding.
  ///
  /// The `color` argument is a shorthand for `decoration: new
  /// BoxDecoration(color: color)`, which means you cannot supply both a `color`
  /// and a `decoration` argument. If you want to have both a `color` and a
  /// `decoration`, you can pass the color as the `color` argument to the
  /// `BoxDecoration`.
  Container({
    Key key,
    this.alignment,
    this.padding,
    Color color,
    Decoration decoration,
    this.foregroundDecoration,
    double width,
    double height,
    BoxConstraints constraints,
    this.margin,
    this.transform,
    this.child,
  }) : assert(margin == null || margin.isNonNegative),
       assert(padding == null || padding.isNonNegative),
       assert(decoration == null || decoration.debugAssertIsValid()),
       assert(constraints == null || constraints.debugAssertIsValid()),
       assert(color == null || decoration == null,
         'Cannot provide both a color and a decoration\n'
         'The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".'
       ),
       decoration = decoration ?? (color != null ? BoxDecoration(color: color) : null),
       constraints =
        (width != null || height != null)
          ? constraints?.tighten(width: width, height: height)
            ?? BoxConstraints.tightFor(width: width, height: height)
          : constraints,
       super(key: key);

从源码中可以看出Container同样是一个StatelessWidget类型的widget,可以根据颜色、内边距、外边距、装饰(阴影、圆角、背景图、边框、渐变色等)、宽高、子视图等参数设置你想要的视图外观形状,功能非常强大。

Container使用

class ContainerDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
//      color: Colors.grey,
      decoration: BoxDecoration(
          image: DecorationImage(
            image: NetworkImage(
                'https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D500/sign=d778213e9225bc312f5d01986ede8de7/d000baa1cd11728bbd9d78d2c3fcc3cec2fd2c8a.jpg'),
            alignment: Alignment.topCenter,
//          repeat: ImageRepeat.repeat,
          fit: BoxFit.cover,
          )),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center, // 主轴对齐方式
        children: [
          Container(
            child: Icon(
              Icons.poll,
              color: Colors.black,
            ),
            padding: EdgeInsets.all(10.0),
            margin: EdgeInsets.all(50.0),
            width: 100,
            height: 100,
            decoration: BoxDecoration(
              // 装饰盒子
              color: Colors.grey,
//              image: DecorationImage(
//                image: NetworkImage(
//                    'https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D500/sign=d778213e9225bc312f5d01986ede8de7/d000baa1cd11728bbd9d78d2c3fcc3cec2fd2c8a.jpg'),
//                fit: BoxFit.fill,
//              ),
              border: Border(
                // 边框
                top: BorderSide(
                  color: Colors.red,
                  width: 5.0,
                  style: BorderStyle.solid,
                ),
              ),
              borderRadius: BorderRadius.only(
                // 圆角
                topLeft: Radius.circular(20.0),
                bottomRight: Radius.circular(20),
              ),
              boxShadow: [
                // 阴影
                BoxShadow(
                  offset: Offset(6.0, 7.0),
                  color: Color.fromRGBO(16, 20, 188, 1.0),
                  blurRadius: 10.0,
                  spreadRadius: -5, // 扩大或缩小阴影面积,正的扩大,负数缩小
                ),
              ],
              shape: BoxShape.rectangle,
              // 改变盒子的形状
//              gradient: RadialGradient( // 环形渐变
//                  colors: [
//                    Color.fromRGBO(7, 182, 255, 1.0),
//                    Color.fromRGBO(3, 28, 120, 1.0),
//                  ],
//              ),
              gradient: LinearGradient(
                // 线性渐变
                colors: [
                  Color.fromRGBO(7, 182, 255, 1.0),
                  Color.fromRGBO(3, 28, 120, 1.0),
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

效果图

Container

你可能感兴趣的:(Flutter基础控件之Container)