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), ], ) ], ), ),