-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathios_settings_tile.dart
296 lines (274 loc) · 9.05 KB
/
ios_settings_tile.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:settings_ui/settings_ui.dart';
class IOSSettingsTile extends StatefulWidget {
const IOSSettingsTile({
required this.tileType,
required this.leading,
required this.title,
required this.description,
required this.onPressed,
required this.onToggle,
required this.value,
required this.initialValue,
required this.activeSwitchColor,
required this.enabled,
required this.trailing,
required this.compact,
Key? key,
}) : super(key: key);
final SettingsTileType tileType;
final Widget? leading;
final Widget? title;
final Widget? description;
final Function(BuildContext context)? onPressed;
final Function(bool value)? onToggle;
final Widget? value;
final bool? initialValue;
final bool enabled;
final Color? activeSwitchColor;
final Widget? trailing;
final bool compact;
@override
_IOSSettingsTileState createState() => _IOSSettingsTileState();
}
class _IOSSettingsTileState extends State<IOSSettingsTile> {
bool isPressed = false;
@override
Widget build(BuildContext context) {
final additionalInfo = IOSSettingsTileAdditionalInfo.of(context);
final theme = SettingsTheme.of(context);
return IgnorePointer(
ignoring: !widget.enabled,
child: Column(
children: [
buildTitle(
context: context,
theme: theme,
additionalInfo: additionalInfo,
),
if (widget.description != null)
buildDescription(
context: context,
theme: theme,
additionalInfo: additionalInfo,
),
],
),
);
}
Widget buildTitle({
required BuildContext context,
required SettingsTheme theme,
required IOSSettingsTileAdditionalInfo additionalInfo,
}) {
Widget content = buildTileContent(context, theme, additionalInfo);
DevicePlatform platform = PlatformUtils.detectPlatform(context);
if (platform != DevicePlatform.iOS) {
content = Material(
color: Colors.transparent,
child: content,
);
}
return ClipRRect(
borderRadius: BorderRadius.vertical(
top: additionalInfo.enableTopBorderRadius
? Radius.circular(12)
: Radius.zero,
bottom: additionalInfo.enableBottomBorderRadius
? Radius.circular(12)
: Radius.zero,
),
child: content,
);
}
Widget buildDescription({
required BuildContext context,
required SettingsTheme theme,
required IOSSettingsTileAdditionalInfo additionalInfo,
}) {
final scaleFactor = MediaQuery.of(context).textScaleFactor;
return Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.only(
left: 18,
right: 18,
top: 8 * scaleFactor,
bottom: additionalInfo.needToShowDivider ? 24 : 8 * scaleFactor,
),
decoration: BoxDecoration(
color: theme.themeData.settingsListBackground,
),
child: DefaultTextStyle(
style: TextStyle(
color: theme.themeData.titleTextColor,
fontSize: 13,
),
child: widget.description!,
),
);
}
Widget buildTrailing({
required BuildContext context,
required SettingsTheme theme,
}) {
final scaleFactor = MediaQuery.of(context).textScaleFactor;
return Row(
children: [
if (widget.trailing != null) widget.trailing!,
if (widget.tileType == SettingsTileType.switchTile)
CupertinoSwitch(
value: widget.initialValue ?? true,
onChanged: widget.onToggle,
activeColor: widget.enabled
? widget.activeSwitchColor
: theme.themeData.inactiveTitleColor,
),
if (widget.tileType == SettingsTileType.navigationTile &&
widget.value != null)
DefaultTextStyle(
style: TextStyle(
color: widget.enabled
? theme.themeData.trailingTextColor
: theme.themeData.inactiveTitleColor,
fontSize: 17,
),
child: widget.value!,
),
if (widget.tileType == SettingsTileType.navigationTile)
Padding(
padding: const EdgeInsetsDirectional.only(start: 6, end: 2),
child: IconTheme(
data: IconTheme.of(context)
.copyWith(color: theme.themeData.leadingIconsColor),
child: Icon(
CupertinoIcons.chevron_forward,
size: 18 * scaleFactor,
),
),
),
],
);
}
void changePressState({bool isPressed = false}) {
if (mounted) {
setState(() {
this.isPressed = isPressed;
});
}
}
Widget buildTileContent(
BuildContext context,
SettingsTheme theme,
IOSSettingsTileAdditionalInfo additionalInfo,
) {
final scaleFactor = MediaQuery.of(context).textScaleFactor;
final double tilePadding = widget.compact ? 7 : 12.5;
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: widget.onPressed == null
? null
: () {
changePressState(isPressed: true);
widget.onPressed!.call(context);
Future.delayed(
Duration(milliseconds: 100),
() => changePressState(isPressed: false),
);
},
onTapDown: (_) =>
widget.onPressed == null ? null : changePressState(isPressed: true),
onTapUp: (_) =>
widget.onPressed == null ? null : changePressState(isPressed: false),
onTapCancel: () =>
widget.onPressed == null ? null : changePressState(isPressed: false),
child: Container(
color: isPressed
? theme.themeData.tileHighlightColor
: theme.themeData.settingsSectionBackground,
padding: EdgeInsetsDirectional.only(start: 18),
child: Row(
children: [
if (widget.leading != null)
Padding(
padding: const EdgeInsetsDirectional.only(end: 12.0),
child: IconTheme.merge(
data: IconThemeData(
color: widget.enabled
? theme.themeData.leadingIconsColor
: theme.themeData.inactiveTitleColor,
),
child: widget.leading!,
),
),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsetsDirectional.only(end: 16),
child: Row(
children: [
Expanded(
child: Padding(
padding: EdgeInsetsDirectional.only(
top: tilePadding * scaleFactor,
bottom: tilePadding * scaleFactor,
),
child: DefaultTextStyle(
style: TextStyle(
color: widget.enabled
? theme.themeData.settingsTileTextColor
: theme.themeData.inactiveTitleColor,
fontSize: 16,
),
child: widget.title!,
),
),
),
buildTrailing(context: context, theme: theme),
],
),
),
if (widget.description == null &&
additionalInfo.needToShowDivider)
Divider(
height: 0,
thickness: 0.7,
color: theme.themeData.dividerColor,
),
],
),
),
],
),
),
);
}
}
class IOSSettingsTileAdditionalInfo extends InheritedWidget {
final bool needToShowDivider;
final bool enableTopBorderRadius;
final bool enableBottomBorderRadius;
IOSSettingsTileAdditionalInfo({
required this.needToShowDivider,
required this.enableTopBorderRadius,
required this.enableBottomBorderRadius,
required Widget child,
}) : super(child: child);
@override
bool updateShouldNotify(IOSSettingsTileAdditionalInfo old) => true;
static IOSSettingsTileAdditionalInfo of(BuildContext context) {
final IOSSettingsTileAdditionalInfo? result = context
.dependOnInheritedWidgetOfExactType<IOSSettingsTileAdditionalInfo>();
// assert(result != null, 'No IOSSettingsTileAdditionalInfo found in context');
return result ??
IOSSettingsTileAdditionalInfo(
needToShowDivider: true,
enableBottomBorderRadius: true,
enableTopBorderRadius: true,
child: SizedBox(),
);
}
}