Container
是一个拥有绘制、定位、调整大小的widget
。android
padding和margin分别设置Container
的内边距和外边距。可取值包括下面四个:app
Scaffold( appBar: AppBar(title: Text('Container')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ Container( padding: EdgeInsets.all(50), decoration: BoxDecoration( border: Border.all(color: Colors.red, width: 1), borderRadius: BorderRadius.all(Radius.circular(20))), child: Text( "肯定", style: TextStyle(color: Colors.red), ), ), Container( padding: EdgeInsets.only(left: 50,right: 50), decoration: BoxDecoration( border: Border.all(color: Colors.red, width: 1), borderRadius: BorderRadius.all(Radius.circular(20))), child: Text( "肯定", style: TextStyle(color: Colors.red), ), ), Container( padding: EdgeInsets.fromLTRB(50, 10, 50, 10), decoration: BoxDecoration( border: Border.all(color: Colors.red, width: 1), borderRadius: BorderRadius.all(Radius.circular(20))), child: Text( "肯定", style: TextStyle(color: Colors.red), ), ), Container( padding: EdgeInsets.symmetric(vertical: 10, horizontal: 50), decoration: BoxDecoration( border: Border.all(color: Colors.red, width: 1), borderRadius: BorderRadius.all(Radius.circular(20))), child: Text( "肯定", style: TextStyle(color: Colors.red), ), ), ], ), )),
width
和height
指定宽高,若是不指定则为子widget
的宽高。若是想要彻底撑满父容器,能够将width
和height
设置为double.infinity
。spa
decoration
常常被用来改变一个Container
的展现效果。其概念相似与android中的shape。通常实际场景中会使用他的子类BoxDecoration。BoxDecoration提供了对背景色,边框,圆角,阴影和渐变等功能的定制能力。code
image: DecorationImage
设置一张图片做为背景。border: Border
设置边界。borderRadius: BorderRadius
设置边界圆角。当shape
是BoxShape.circle
设置borderRadius
将不起做用shape: BoxShape
设置形状。gradient
设置渐变。可选值包括三种类型的渐变LinearGradient
、RadialGradient
、SweepGradient
。Scaffold( appBar: AppBar(title: Text('BorderRadius')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ Container( height: 200, width: 200, decoration: BoxDecoration( color: Colors.yellow, //设置图片 image: DecorationImage( fit: BoxFit.fitWidth, image: NetworkImage( 'https://flutter.io/images/catalog-widget-placeholder.png', ), ), //设置边界 border: Border.all(color: Colors.deepOrange, width: 3), //设置阴影 boxShadow: const [ BoxShadow(blurRadius: 10), ], //设置边界圆角 borderRadius: BorderRadius.all(Radius.circular(18))), ), Container( height: 200, width: 200, decoration: BoxDecoration( gradient: RadialGradient( //渐变 colors: const [ Colors.green, Colors.deepOrange, Colors.pinkAccent, Colors.deepPurple ], ), //设置边界圆角 shape: BoxShape.circle, ), ) ], ), ), ),
Scaffold( appBar: AppBar(title: Text('Flutter')), body: Column( children: <Widget>[ Text("MainAxisAlignment.start",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( mainAxisAlignment: MainAxisAlignment.start, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), ], ), Text("MainAxisAlignment.center",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), ], ), Text("MainAxisAlignment.spaceAround",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), ], ), Text("MainAxisAlignment.spaceBetween",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), ], ), Text("MainAxisAlignment.spaceEvenly",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), ], ), Text("MainAxisAlignment.end",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), Icon(Icons.star,color: Colors.yellow, size: 50), ], ) ], ), ),
Scaffold( appBar: AppBar(title: Text('Flutter')), body: Column( children: <Widget>[ Text("CrossAxisAlignment.start",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 30), Icon(Icons.star,color: Colors.yellow, size: 60), Icon(Icons.star,color: Colors.yellow, size: 30), ], ), Text("CrossAxisAlignment.center",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 30), Icon(Icons.star,color: Colors.yellow, size: 60), Icon(Icons.star,color: Colors.yellow, size: 30), ], ), Text(" CrossAxisAlignment.end",style:TextStyle( color: Colors.blueAccent, fontSize: 18 )), Row( crossAxisAlignment: CrossAxisAlignment.end, children: <Widget>[ Icon(Icons.star,color: Colors.yellow, size: 30), Icon(Icons.star,color: Colors.yellow, size: 60), Icon(Icons.star,color: Colors.yellow, size: 30), ], ) ], ), ),