若是窗口小部件沒有孩子,沒有對齊,而是一個height,width或 限制提供,則Container試圖給出這些限制和家長的約束相結合,以儘量小。bash
Container(
margin: const EdgeInsets.all(10.0),
color: const Color(0xFF00FF00),
width: 48.0,
height: 48.0,
)
複製代碼
若是窗口小部件沒有子窗口,沒有height,沒有width,沒有約束,沒有對齊,可是父窗口提供了有界約束,那麼Container會擴展以適應父窗口提供 的約束。app
Container(
margin: const EdgeInsets.all(10.0),
color: const Color(0xFF00FF00),
)
複製代碼
若是子窗口有寬高、對齊,父窗口沒有寬高、對齊,則父窗口會嘗試圍繞子窗口調整自身大小。less
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),
),
)
複製代碼
若是子窗口沒有寬高對齊,父窗口提供有有寬高、對齊,則子窗口會嘗試展開以適合父窗口,而後根據對齊方式將子項置於其自身內部。ide
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實例ui
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.center
複製代碼
child: Text('Hello World', style: Theme.of(context).textTheme.display1.copyWith(color: Colors.white))
複製代碼
constraints: BoxConstraints.expand(
height: 700,
width: 400
)
複製代碼
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: 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: const EdgeInsets.all(20.0)
複製代碼
padding: const EdgeInsets.all(8.0)
複製代碼
transform: Matrix4.rotationZ(0.2),
複製代碼
7.一些經常使用的方法this
@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.
);
}
}
複製代碼