Skip to content

Commit 516a239

Browse files
committed
feat: LayoutBuilder
1 parent f5251a8 commit 516a239

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## **LayoutBuilder**
2+
3+
>
4+
构建可依赖于父控件大小的控件树
5+
6+
### 构造方法
7+
``` dart
8+
const LayoutBuilder({
9+
Key key,
10+
@required this.builder
11+
})
12+
```
13+
14+
### 属性介绍
15+
* builder: 传入一个LayoutWidgetBuilder类,builder: (BuildContext context, BoxConstraints viewportConstraints)
16+
* 通过viewportConstraints可以获取到父控件的尺寸
17+
* builder: 在布局时调用来构造小部件树,该构建器不得返回nul

lib/widget/regular/index.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'flow/index.dart' as Flow;
1414
import 'stack/index.dart' as Stack;
1515
import 'listview/index.dart' as ListView;
1616
import 'listbody/index.dart' as ListBody;
17+
import 'layoutbuilder/index.dart' as LayoutBuilder;
1718

1819
const nameSpaces = '/regular_';
1920

@@ -92,6 +93,11 @@ List widgets = [
9293
widget: ListBody.Index(),
9394
code: 57440, // art_track
9495
title: ListBody.Index.title
96+
),
97+
ItemInfo(
98+
widget: LayoutBuilder.Index(),
99+
code: 59484, // assessment
100+
title: LayoutBuilder.Index.title
95101
)
96102
];
97103

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 height = 360.0;
10+
11+
@override
12+
Widget build(BuildContext context) {
13+
return Scaffold(
14+
appBar: AppBar(title: Text('LayoutBuilder'),),
15+
body: ListView(
16+
children: <Widget>[
17+
InkWell(
18+
onTap: () {
19+
this.setState(() {
20+
height == 360.0 ? height = 200.0 : height = 360.0;
21+
});
22+
},
23+
child: Text('父视口高度: $height', style: TextStyle(fontSize: 16.0),),
24+
),
25+
Container(
26+
width: double.infinity,
27+
height: height,
28+
color: Colors.blueGrey,
29+
margin: EdgeInsets.only(top: 10.0),
30+
child: LayoutBuilder(
31+
builder: (BuildContext context, BoxConstraints viewportConstraints) {
32+
return SingleChildScrollView(
33+
child: ConstrainedBox(
34+
constraints:
35+
BoxConstraints(minHeight: viewportConstraints.maxHeight),
36+
child: Column(
37+
mainAxisSize: MainAxisSize.min,
38+
mainAxisAlignment: MainAxisAlignment.spaceAround,
39+
children: <Widget>[
40+
Container(
41+
child: Image.network('http://pic1.win4000.com/wallpaper/2019-02-15/5c664c3e1d90c.jpg', fit: BoxFit.cover,),
42+
height: 120,
43+
width: double.infinity,
44+
),
45+
Container(
46+
child: Image.network('http://pic1.win4000.com/wallpaper/2019-02-15/5c664c48c73d0.jpg', fit: BoxFit.cover,),
47+
height: 160,
48+
width: double.infinity,
49+
)
50+
],
51+
),
52+
),
53+
);
54+
}
55+
),
56+
)
57+
],
58+
)
59+
);
60+
}
61+
}
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+
import 'package:efox_flutter/components/widgetComp.dart' as WidgetComp;
3+
import 'demo.dart' as Demo;
4+
5+
class Index extends StatefulWidget {
6+
static String title = 'LayoutBuilder';
7+
static String originCodeUrl = 'https://docs.flutter.io/flutter/widgets/LayoutBuilder-class.html';
8+
static String mdUrl = 'docs/widget/regular/layoutbuilder/index.md';
9+
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+
],
24+
);
25+
}
26+
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ flutter:
7777
- docs/widget/regular/stack/
7878
- docs/widget/regular/listview/
7979
- docs/widget/regular/listbody/
80+
- docs/widget/regular/layoutbuilder/
8081
- docs/widget/navigator/appbar/
8182
- docs/widget/navigator/scaffold/
8283
# An image asset can refer to one or more resolution-specific "variants", see

0 commit comments

Comments
 (0)