-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
layout_flow_aware_padding.dart
56 lines (49 loc) · 1.37 KB
/
layout_flow_aware_padding.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
import 'package:flutter/widgets.dart';
/// Padding widget that takes into account current layout flow.
/// If nearest ancestor is [Row] (or [Flex] with [Flex.direction] equal to
/// [Axis.horizontal]) – horizontal paddings are ignored.
/// Otherwise vertical paddings are ignored.
class LayoutFlowAwarePadding extends StatelessWidget {
final Widget child;
final EdgeInsets padding;
const LayoutFlowAwarePadding({
Key? key,
required this.child,
required this.padding,
}) : super(key: key);
@override
Widget build(BuildContext context) {
Axis? axis;
context.visitAncestorElements((element) {
if (element.widget is Row) {
axis = Axis.horizontal;
return false;
} else if (element.widget is Column) {
axis = Axis.vertical;
return false;
} else if (element.widget is Flex) {
axis = (element.widget as Flex).direction;
return false;
}
return true;
});
EdgeInsets finalPadding;
if (axis == null) {
finalPadding = padding;
} else if (axis == Axis.horizontal) {
finalPadding = EdgeInsets.only(
left: padding.left,
right: padding.right,
);
} else {
finalPadding = EdgeInsets.only(
top: padding.top,
bottom: padding.bottom,
);
}
return Padding(
padding: finalPadding,
child: child,
);
}
}