custom_divider.dart
1.72 KB
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
import 'package:flutter/material.dart';
class CustomDivider extends LeafRenderObjectWidget {
const CustomDivider({super.key, this.height, this.color});
final Color? color;
final double? height;
@override
RenderObject createRenderObject(BuildContext context) {
return RenderDivider(
color ?? Theme.of(context).colorScheme.outline,
MediaQuery.sizeOf(context).width,
height ?? 2,
);
}
@override
void updateRenderObject(
BuildContext context,
covariant RenderDivider renderObject,
) {
renderObject.color = color ?? Theme.of(context).colorScheme.outline;
renderObject.height = height ?? 2;
renderObject.width = MediaQuery.sizeOf(context).width;
}
}
class RenderDivider extends RenderBox {
RenderDivider(Color color, double width, double height)
: _color = color,
_height = height,
_width = width;
Color _color;
double _height;
double _width;
set color(Color value) {
if (value == _color) {
return;
}
_color = value;
markNeedsPaint();
}
set height(double value) {
if (value == _height) {
return;
}
_height = value;
markNeedsLayout();
}
set width(double value) {
if (value == _width) {
return;
}
_width = value;
markNeedsLayout();
}
@override
Size computeDryLayout(BoxConstraints constraints) {
return BoxConstraints.tightFor(
width: null,
height: _height,
).enforce(constraints).smallest;
}
@override
void performLayout() {
size = getDryLayout(constraints);
}
@override
void paint(PaintingContext context, Offset offset) {
context.canvas.drawRect(
offset & Size(Rect.largest.size.width, _height),
Paint()..color = _color,
);
}
}