1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class LayoutTest extends StatefulWidget {
@override
_LayoutTestState createState() => _LayoutTestState();
}
class _LayoutTestState extends State<LayoutTest> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Layout Test'),
),
body: Container(
padding: EdgeInsets.all(32.0),
child: Center(
child: Column(
children: <Widget>[
_renderBoxTest(),
_containerTestWidget()
],
),
),
),
);
}
Widget _renderBoxTest(){
return Container(
color: Colors.greenAccent,
constraints: BoxConstraints(
maxWidth: double.infinity,
minWidth: 200.0,
maxHeight: 200,
minHeight: 100.0),
child: Stingy(
child: Container(
color: Colors.pink[200],
),
),
);
}
// www.baidu.com/img/bd_logo1.png
Widget _containerTestWidget(){
return Container(
constraints: BoxConstraints.expand(
height: Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0,
),
padding: const EdgeInsets.all(8.0),
color: Colors.greenAccent.shade700,
alignment: Alignment.bottomCenter,
child: Text('Hello World', style: Theme.of(context).textTheme.display1.copyWith(color: Colors.orange[200])),
foregroundDecoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('https://www.example.com/images/frame.png'),
centerSlice: Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
),
),
transform: Matrix4.rotationZ(0.1),
);
}
}
class Stingy extends SingleChildRenderObjectWidget {
Stingy({Widget child}) : super(child: child);
@override
RenderObject createRenderObject(BuildContext context) {
// TODO: implement createRenderObject
return RenderStingy();
}
}
class RenderStingy extends RenderShiftedBox {
RenderStingy() : super(null);
// 绘制方法
@override
void paint(PaintingContext context, Offset offset) {
// TODO: implement paint
super.paint(context, offset);
}
// 布局方法
@override
void performLayout() {
// 布局 child 确定 child 的 size
child.layout(
BoxConstraints(
minHeight: 0.0,
maxHeight: constraints.minHeight,
minWidth: 0.0,
maxWidth: constraints.minWidth),
parentUsesSize: true);
print('constraints: $constraints');
// child 的 Offset
final BoxParentData childParentData = child.parentData;
childParentData.offset = Offset(constraints.maxWidth - child.size.width,
constraints.maxHeight - child.size.height);
print('childParentData: $childParentData');
// 确定自己(父节点)的大小,并重绘父节点。不重设,不能触发重绘,例如在热重载的情景中,重设container.maxHeight,无效
size = Size(constraints.maxWidth, constraints.maxHeight);
print('size: $size');
}
}