-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathmax_width_box.dart
49 lines (43 loc) · 1.26 KB
/
max_width_box.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
import 'package:flutter/material.dart';
class MaxWidthBox extends StatelessWidget {
final double? maxWidth;
final Widget child;
/// Control child alignment.
/// Defaults to [Alignment.topCenter] because app
/// content is usually top aligned.
final AlignmentGeometry alignment;
final EdgeInsets? padding;
final Color? backgroundColor;
const MaxWidthBox(
{super.key,
required this.maxWidth,
required this.child,
this.alignment = Alignment.topCenter,
this.padding,
this.backgroundColor});
@override
Widget build(BuildContext context) {
MediaQueryData mediaQuery = MediaQuery.of(context);
if (maxWidth != null) {
if (mediaQuery.size.width > maxWidth!) {
mediaQuery = mediaQuery.copyWith(
size: Size(maxWidth! - (padding?.horizontal ?? 0),
mediaQuery.size.height - (padding?.vertical ?? 0)));
}
}
return Align(
alignment: alignment,
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxWidth ?? double.infinity),
child: Container(
color: backgroundColor,
padding: padding,
child: MediaQuery(
data: mediaQuery,
child: child,
),
),
),
);
}
}