-
Notifications
You must be signed in to change notification settings - Fork 1
/
relative_scale_base.dart
169 lines (148 loc) · 4.26 KB
/
relative_scale_base.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
import 'package:flutter/material.dart';
/// DEPRECATED - Use `RelativeBuilder` instead.
/// See example below:
/// ```dart
/// @override
/// Widget build(BuildContext context) {
/// return RelativeBuilder(
/// builder: (context, height, width, sy, sx) {
/// return Container(
/// height: height,
/// width: width,
/// child: Center(
/// child: Column(
/// mainAxisSize: MainAxisSize.min,
/// children: [
/// SizedBox(
/// height: sy(30),
/// width: sy(30),
/// ),
/// SizedBox(
/// height: sx(30),
/// width: sx(30),
/// ),
/// ],
/// ),
/// ),
/// );
/// },
/// );
/// }
/// ```
@Deprecated(
"This mixin is no longer recommended. Use \"RelativeBuilder\" instead.")
mixin RelativeScale {
double? _screenHeight;
double? _screenWidth;
/// `RelativeScale.screenHeight` -> the height of the screen.
double? get screenHeight => _screenHeight;
/// `RelativeScale.screenWidth` -> the width of the screen.
double? get screenWidth => _screenWidth;
/// Initialize `RelativeScale`.
initRelativeScaler(BuildContext context) {
var size = MediaQuery.of(context).size;
_screenHeight = size.height;
_screenWidth = size.width;
}
/// Size relative to the `height` of the screen.
double sy(double value) {
return (_screenHeight! * _calculate(value)).roundToDouble();
}
/// Size relative to the `width` of the screen.
double sx(double value) {
return (_screenWidth! * _calculate(value)).roundToDouble();
}
double _calculate(double value) {
return (value / 100) / 5.333333333333333;
}
}
typedef _RelativeBuilderFunction = Widget Function(
BuildContext,
double,
double,
double Function(double),
double Function(double),
);
/// A relative builder specially made for stateless widget which can't
/// inherit from the mixin RelativeScale.
class RelativeBuilder extends StatelessWidget {
/// Scale the sizes the way you want. Defaults to `1.0`.
/// If `RelativeBuilder` turns your sizes a bit bigger or smaller
/// you can decrease or increase the scale to match your expected sizes.
final double? scale;
/// ## Parameters
/// `context` - The [BuildContext] for this builder widget.
///
/// `height` - The height of the screen.
///
/// `width` - The width of the screen.
///
/// `sy(double)` - A function which scales the value specified
/// based on screen height.
///
/// `sx(double)` - A function which scales the value specified
/// based on screen width.
///
/// ## Example
/// ```dart
/// builder: (context, height, width, sy, sx) {
/// return Container(
/// height: height,
/// width: width,
/// child: Text(
/// 'Scaled Text!!',
/// style: TextStyle(fontSize: sy(12)),
/// ),
/// );
/// }
/// ```
final _RelativeBuilderFunction builder;
/// A relative builder specially made for stateless widget which can't
/// inherit from the mixin RelativeScale.
const RelativeBuilder({
Key? key,
this.scale,
required this.builder,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return _RelativeBuilder(scale: scale, builder: builder);
}
}
class _RelativeBuilder extends StatefulWidget {
final double? scale;
final _RelativeBuilderFunction? builder;
const _RelativeBuilder({
Key? key,
this.scale,
this.builder,
}) : super(key: key);
@override
_RelativeBuilderState createState() => _RelativeBuilderState();
}
class _RelativeBuilderState extends State<_RelativeBuilder> {
@override
Widget build(BuildContext context) {
return widget.builder!(
context,
_screenHeight,
_screenWidth,
_sy,
_sx,
);
}
Size get _size {
return MediaQuery.of(context).size;
}
double get _screenHeight => _size.height;
double get _screenWidth => _size.width;
double _sy(double value) {
return (_screenHeight * _calculate(value)).roundToDouble();
}
double _sx(double value) {
return (_screenWidth * _calculate(value)).roundToDouble();
}
double _calculate(double value) {
return ((value / 100) / 5.333333333333333) * (widget.scale ?? 1);
}
}