Container —— 容器類
一、使用方法
-
如果視窗小部件沒有孩子,沒有對齊,而是一個height,width或 限制提供,則Container試圖給出這些限制和家長的約束相結合,以儘可能小。
Container(
margin: const EdgeInsets.all(10.0),
color: const Color(0xFF00FF00),
width: 48.0,
height: 48.0,
)
複製程式碼
-
如果視窗小部件沒有子視窗,沒有height,沒有width,沒有約束,沒有對齊,但是父視窗提供了有界約束,那麼Container會擴充套件以適應父視窗提供 的約束。
Container(
margin: const EdgeInsets.all(10.0),
color: const Color(0xFF00FF00),
)
複製程式碼
-
如果子視窗有寬高、對齊,父視窗沒有寬高、對齊,則父視窗會嘗試圍繞子視窗調整自身大小。
Container(
margin: const EdgeInsets.all(10.0),
color: const Color(0xFFFFFF00),
child: Container(
constraints: BoxConstraints.expand(
height: 50,
width: 70,
),
margin: const EdgeInsets.all(20.0),
color: const Color(0xFF00FF00),
),
)
複製程式碼
-
如果子視窗沒有寬高對齊,父視窗提供有有寬高、對齊,則子視窗會嘗試展開以適合父視窗,然後根據對齊方式將子項置於其自身內部。
Container(
constraints: BoxConstraints.expand(
width: 180,
height: 180,
),
margin: const EdgeInsets.all(10.0),
color: const Color(0xFF1251F0),
child: Container(
margin: const EdgeInsets.all(20.0),
color: const Color(0xFFee34e5),
),
)
複製程式碼
-
一個比較完整的container例項
Container(
//BoxConstraints 盒子模型約束 ,設定邊框約束
constraints: BoxConstraints.expand(
height: 700,
width: 400
),
//設定padding
padding: const EdgeInsets.all(8.0),
color: Colors.teal.shade700,
//設定子容器的對其方式
alignment: Alignment.center,
// Widget 設定子控制元件
child: Text('Hello World', style: Theme.of(context).textTheme.display1.copyWith(color: Colors.white)),
//設定前景盒子裝飾
foregroundDecoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: DecorationImage(
image: NetworkImage('http://c.hiphotos.baidu.com/image/pic/item/a8773912b31bb0516a13ec1d387adab44aede0d4.jpg'),
fit: BoxFit.cover,
alignment: Alignment.topCenter
),
border: Border.all(
color: Colors.black,
width: 8.0,
),
),
//設定變換
transform: Matrix4.rotationZ(0.2),
),
複製程式碼
二、常用的屬性
- alignment :設定對其方式
alignment:Alignment.center
複製程式碼
- child : 設定容器子控制元件
child: Text('Hello World', style: Theme.of(context).textTheme.display1.copyWith(color: Colors.white))
複製程式碼
- constraints :設定約束
constraints: BoxConstraints.expand(
height: 700,
width: 400
)
複製程式碼
- decoration :設定背景裝飾
Container(
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: DecorationImage(
image: ExactAssetImage('images/flowers.jpeg'),
fit: BoxFit.cover,
),
border: Border.all(
color: Colors.black,
width: 8.0,
),
),
)
複製程式碼
- foregroundDecoration:設定前景裝飾
foregroundDecoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: DecorationImage(
image: NetworkImage('http://c.hiphotos.baidu.com/image/pic/item/a8773912b31bb0516a13ec1d387adab44aede0d4.jpg'),
fit: BoxFit.cover,
alignment: Alignment.topCenter
),
border: Border.all(
color: Colors.black,
width: 8.0,
),
)
複製程式碼
- margin 設定外邊距
margin: const EdgeInsets.all(20.0)
複製程式碼
- padding 設定內邊距
padding: const EdgeInsets.all(8.0)
複製程式碼
- transform :設定變換
transform: Matrix4.rotationZ(0.2),
複製程式碼
7.一些常用的方法
- build(BuildContext context) → Widget 描述此視窗小部件表示的使用者介面部分
@override
Widget build ( BuildContext context){
}
複製程式碼
三、一個完整的例子
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
//描述此視窗小部件表示的使用者介面部分
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
child:
// //container 示例1
// 如果視窗小部件沒有孩子,沒有對齊,而是一個height,width或 限制提供,則集裝箱試圖給出這些限制和家長的約束相結合,以儘可能小。
// Container(
// margin: const EdgeInsets.all(10.0),
// color: const Color(0xFF00FF00),
// width: 48.0,
// height: 48.0,
// )
// //container 示例2
// // 如果視窗小部件沒有子視窗,沒有height,沒有width,沒有約束,沒有對齊,但是父視窗提供了有界約束,那麼Container會擴充套件以適應父視窗提供 的約束。
// Container(
// margin: const EdgeInsets.all(10.0),
// color: const Color(0xFF00FF00),
// )
//container 示例3
// 如果視窗小部件具有對齊,並且父視窗提供無限制約束,則Container會嘗試圍繞子視窗調整自身大小。
// Container(
//// margin: const EdgeInsets.all(10.0),
// color: const Color(0xFFFFFF00),
// child: Container(
// constraints: BoxConstraints.expand(
// height: 50,
// width: 70,
// ),
// margin: const EdgeInsets.all(20.0),
// color: const Color(0xFF00FF00),
// ),
// )
// //container 示例4
// // 如果視窗小部件具有對齊,並且父視窗提供有界約束(有寬高),則Container會嘗試展開以適合父視窗,然後根據對齊方式將子項置於其自身內部
// Container(
// constraints: BoxConstraints.expand(
// width: 180,
// height: 180,
// ),
// margin: const EdgeInsets.all(10.0),
// color: const Color(0xFF1251F0),
// child: Container(
// margin: const EdgeInsets.all(20.0),
// color: const Color(0xFFee34e5),
// ),
// )
//container 示例5 一個比較完整的例項
Container(
//BoxConstraints 盒子模型約束 ,設定邊框約束
constraints: BoxConstraints.expand(
height: 700,
width: 400
),
margin: const EdgeInsets.all(20.0),
//設定padding
padding: const EdgeInsets.all(10.0),
color: Colors.teal.shade700,
//設定子容器的對其方式
alignment: Alignment.center,
// Widget 設定子控制元件
child: Text('Hello World', style: Theme.of(context).textTheme.display1.copyWith(color: Colors.white)),
//設定前景盒子裝飾
foregroundDecoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: DecorationImage(
image: NetworkImage('http://c.hiphotos.baidu.com/image/pic/item/a8773912b31bb0516a13ec1d387adab44aede0d4.jpg'),
fit: BoxFit.cover,
alignment: Alignment.topCenter
),
border: Border.all(
color: Colors.black,
width: 8.0,
),
),
//設定變換
transform: Matrix4.rotationZ(0.2),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
複製程式碼