Skip to content

Commit 091fad8

Browse files
committed
ConstrainedBox
1 parent d776542 commit 091fad8

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## **ConstrainedBox**
2+
3+
>
4+
一个基础布局控件,添加额外的限制条件(constraints)到child上
5+
6+
### 构造方法
7+
``` dart
8+
ConstrainedBox({
9+
Key key,
10+
@required this.constraints,
11+
Widget child
12+
})
13+
```
14+
15+
### 属性介绍
16+
* constraints: 添加到child上的额外限制条件,其类型为BoxConstraints,限制各种最大最小宽高
17+
* child: ConstrainedBox中的内容Widget
18+
19+
### 其他用法
20+
* constraints值为BoxConstraints.expand时,提供width或者height,那么限制(Constraints)将严格使用给出的width或者height值
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Index extends StatefulWidget {
4+
@override
5+
_IndexState createState() => _IndexState();
6+
}
7+
8+
class _IndexState extends State<Index> {
9+
var maxWidth = 200.0;
10+
var maxHeight = 200.0;
11+
@override
12+
Widget build(BuildContext context) {
13+
return Scaffold(
14+
appBar: AppBar(title: Text('ConstrainedBox'),),
15+
body: ListView(
16+
children: <Widget>[
17+
SizedBox(
18+
child: Text('修改maxWidth maxHeight 值'),
19+
),
20+
Row(
21+
children: <Widget>[
22+
FlatButton(
23+
child: Text('maxWidth: $maxWidth', style: TextStyle(fontSize: 14.0),),
24+
onPressed: (){
25+
setState(() {
26+
maxWidth == 200.0 ? maxWidth = 150.0 : maxWidth = 200.0;
27+
});
28+
},
29+
),
30+
FlatButton(
31+
child: Text('maxHeight: $maxHeight', style: TextStyle(fontSize: 14.0),),
32+
onPressed: (){
33+
setState(() {
34+
maxHeight == 200.0 ? maxHeight = 150.0 : maxHeight = 200.0;
35+
});
36+
},
37+
)
38+
],
39+
),
40+
Center(
41+
child: ConstrainedBox(
42+
constraints: BoxConstraints(
43+
minWidth: 100.0,
44+
minHeight: 100.0,
45+
maxWidth: maxWidth,
46+
maxHeight: maxHeight
47+
),
48+
child: Container(
49+
margin: EdgeInsets.all(10.0),
50+
decoration: BoxDecoration(
51+
image: DecorationImage(
52+
image: NetworkImage('http://sucimg.itc.cn/avatarimg/55d21fdc4b8d4838bef0da94ada78cab_1501139484387')
53+
),
54+
color: Theme.of(context).primaryColor,
55+
),
56+
),
57+
),
58+
),
59+
],
60+
),
61+
);
62+
}
63+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Index extends StatelessWidget {
4+
@override
5+
Widget build(BuildContext context) {
6+
return Scaffold(
7+
appBar: AppBar(title: Text('Expand'),),
8+
body: Center(
9+
child: ConstrainedBox(
10+
constraints: BoxConstraints.expand(
11+
width: 150.0,
12+
height: 150.0
13+
),
14+
child: Container(
15+
alignment: Alignment.center,
16+
color: Theme.of(context).primaryColor,
17+
child: Text(
18+
'Hello World',
19+
style: TextStyle(fontSize: 20.0, color: Colors.white, fontWeight: FontWeight.bold)
20+
),
21+
),
22+
),
23+
),
24+
);
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:efox_flutter/components/widgetComp.dart' as WidgetComp;
3+
import 'demo.dart' as Demo;
4+
import 'demo_expand.dart' as DemoExpand;
5+
6+
class Index extends StatefulWidget {
7+
static String title = 'ConstrainedBox';
8+
static String originCodeUrl = '';
9+
static String mdUrl = 'docs/widget/regular/constrainedbox/index.md';
10+
@override
11+
_IndexState createState() => _IndexState();
12+
}
13+
14+
class _IndexState extends State<Index> {
15+
@override
16+
Widget build(BuildContext context) {
17+
return WidgetComp.Index(
18+
title: Index.title,
19+
originCodeUrl: Index.originCodeUrl,
20+
mdUrl: Index.mdUrl,
21+
demoChild: <Widget>[
22+
Demo.Index(),
23+
DemoExpand.Index()
24+
],
25+
);
26+
}
27+
}

lib/widget/regular/index.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'align/index.dart' as Align;
77
import 'center/index.dart' as Center;
88
import 'fittedbox/index.dart' as FittedBox;
99
import 'aspectratio/index.dart' as AspectRatio;
10+
import 'constrainedbox/index.dart' as ConstrainedBox;
1011

1112
const nameSpaces = '/regular_';
1213

@@ -50,6 +51,11 @@ List widgets = [
5051
widget: AspectRatio.Index(),
5152
code: 58688, // local_bar
5253
title: AspectRatio.Index.title
54+
),
55+
ItemInfo(
56+
widget: ConstrainedBox.Index(),
57+
code: 57709, // low_priority
58+
title: ConstrainedBox.Index.title
5359
)
5460
];
5561

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ flutter:
6464
- docs/widget/regular/center/
6565
- docs/widget/regular/fittedbox/
6666
- docs/widget/regular/aspectratio/
67+
- docs/widget/regular/constrainedbox/
6768
# An image asset can refer to one or more resolution-specific "variants", see
6869
# https://flutter.io/assets-and-images/#resolution-aware.
6970

0 commit comments

Comments
 (0)