/
customLayout.dart
185 lines (167 loc) · 5.66 KB
/
customLayout.dart
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class CustomLayoutPage extends StatefulWidget {
@override
_CustomLayoutPageState createState() => _CustomLayoutPageState();
}
class _CustomLayoutPageState extends State<CustomLayoutPage> {
TextEditingController editingController = TextEditingController();
StreamController streamController = StreamController();
@override
void dispose() {
streamController.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('自定义layout'),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
SizedBox(
height: 44,
child: TextField(
controller: editingController,
onChanged: (value) {
streamController.add(0);
},
),
),
SizedBox(height: 30),
StreamBuilder(
stream: streamController.stream,
builder: (_, __) {
return CustomRow(
children: <Widget>[
WithIDRenderObjectWidget(
uid: 'line',
child: VerticalDivider(
color: Colors.red,
),
),
Expanded(
child: WithIDRenderObjectWidget(
uid: 'text',
child: Text(editingController.text),
),
),
],
);
},
),
],
),
),
);
}
}
class CustomRow extends Row {
CustomRow({
Key key,
MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
MainAxisSize mainAxisSize = MainAxisSize.max,
CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
TextDirection textDirection,
VerticalDirection verticalDirection = VerticalDirection.down,
TextBaseline textBaseline = TextBaseline.alphabetic,
List<Widget> children = const <Widget>[],
}) : super(
children: children,
key: key,
mainAxisAlignment: mainAxisAlignment,
mainAxisSize: mainAxisSize,
crossAxisAlignment: crossAxisAlignment,
textDirection: textDirection,
verticalDirection: verticalDirection,
textBaseline: textBaseline,
);
@override
RenderFlex createRenderObject(BuildContext context) {
return CustomRenderFlex(
direction: direction,
mainAxisAlignment: mainAxisAlignment,
mainAxisSize: mainAxisSize,
crossAxisAlignment: crossAxisAlignment,
textDirection: getEffectiveTextDirection(context),
verticalDirection: verticalDirection,
textBaseline: textBaseline,
clipBehavior: clipBehavior,
);
}
}
class CustomRenderFlex extends RenderFlex {
CustomRenderFlex({
Axis direction = Axis.horizontal,
MainAxisSize mainAxisSize = MainAxisSize.max,
MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,
CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,
TextDirection textDirection,
VerticalDirection verticalDirection = VerticalDirection.down,
TextBaseline textBaseline,
Clip clipBehavior = Clip.none,
}) : super(
direction: direction,
mainAxisAlignment: mainAxisAlignment,
mainAxisSize: mainAxisSize,
crossAxisAlignment: crossAxisAlignment,
textDirection: textDirection,
verticalDirection: verticalDirection,
textBaseline: textBaseline,
clipBehavior: clipBehavior,
);
@override
void paint(PaintingContext context, Offset offset) {
defaultPaint(context, offset);
}
@override
void performLayout() {
final BoxConstraints constraints = this.constraints;
assert(constraints != null);
WithIDRenderObject lineChild;
WithIDRenderObject textChild;
RenderBox child = firstChild;
while (child != null) {
if (child is WithIDRenderObject) {
if (child.uid == 'line') {
lineChild = child;
} else if (child.uid == 'text') {
textChild = child;
}
}
final FlexParentData childParentData = child.parentData as FlexParentData;
child = childParentData.nextSibling;
}
///layout
textChild.layout(BoxConstraints(maxWidth: constraints.maxWidth - 28), parentUsesSize: true);
lineChild.layout(BoxConstraints(minWidth: 0, maxWidth: 28, maxHeight: textChild.size.height), parentUsesSize: true);
///设置this的size
size = Size(constraints.maxWidth, textChild.size.height);
///设置child.parentData.offset
final FlexParentData leftChildParentData = lineChild.parentData as FlexParentData;
leftChildParentData.offset = Offset(0, 0);
final FlexParentData rightChildParentData = textChild.parentData as FlexParentData;
rightChildParentData.offset = Offset(lineChild.size.width, 0);
}
}
class WithIDRenderObjectWidget extends SingleChildRenderObjectWidget {
final String uid;
const WithIDRenderObjectWidget({Key key, Widget child, this.uid}) : super(key: key, child: child);
@override
WithIDRenderObject createRenderObject(BuildContext context) {
return WithIDRenderObject(uid: uid);
}
@override
void updateRenderObject(BuildContext context, covariant WithIDRenderObject renderObject) {
renderObject.uid = uid;
}
}
class WithIDRenderObject extends RenderProxyBox {
String uid;
WithIDRenderObject({this.uid, RenderBox child}) : super(child);
}