Skip to content

Commit ebd2729

Browse files
author
Ellet
authored
Fix platform checking logic (#1518)
* Fix platform checking logic * Update _defaultOnTapOutside
1 parent db406d4 commit ebd2729

File tree

18 files changed

+303
-72
lines changed

18 files changed

+303
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- **Breaking change**: the `customButtons` in the `QuillToolbarConfigurations` is now of type `List<QuillToolbarCustomButtonOptions>`
44
- Bug fixes with the new `8.0.0` update
55
- Update `README.md`
6+
- Improve the platform checking
67

78
## [8.3.0]
89
- Added a `iconButtonFactor` property to `QuillToolbarBaseButtonOptions` to customise how big the button is compared to its icon size (defaults to `kIconButtonFactor` which is the same as previous releases)

example/lib/pages/home_page.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ class _HomePageState extends State<HomePage> {
414414
placeholder: 'Add content',
415415
readOnly: _isReadOnly,
416416
autoFocus: false,
417-
enableSelectionToolbar: isMobile(),
417+
enableSelectionToolbar: isMobile(supportWeb: false),
418418
expands: false,
419419
padding: EdgeInsets.zero,
420420
onImagePaste: _onImagePaste,
@@ -500,7 +500,7 @@ class _HomePageState extends State<HomePage> {
500500
// afterButtonPressed: _focusNode.requestFocus,
501501
);
502502
}
503-
if (isDesktop()) {
503+
if (isDesktop(supportWeb: false)) {
504504
return QuillToolbar(
505505
configurations: QuillToolbarConfigurations(
506506
customButtons: customButtons,

example/lib/pages/read_only_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class _ReadOnlyPageState extends State<ReadOnlyPage> {
2323
@override
2424
Widget build(BuildContext context) {
2525
return DemoScaffold(
26-
documentFilename: isDesktop()
26+
documentFilename: isDesktop(supportWeb: false)
2727
? 'assets/sample_data_nomedia.json'
2828
: 'sample_data_nomedia.json',
2929
builder: _buildContent,

flutter_quill_extensions/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.6.5
2+
- Support the new improved platform checking of `flutter_quill`
3+
14
## 0.6.4
25
- Update `QuillImageUtilities`
36
- Add new extension on `QuillController` to access `QuillImageUtilities` instance easier

flutter_quill_extensions/lib/presentation/embeds/editor/image/image.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class QuillEditorImageEmbedBuilder extends EmbedBuilder {
4141
final style = node.style.attributes['style'];
4242

4343
if (style != null) {
44-
final attrs = base.isMobile()
44+
final attrs = base.isMobile(supportWeb: false)
4545
? base.parseKeyValuePairs(style.value.toString(), {
4646
Attribute.mobileWidth,
4747
Attribute.mobileHeight,
@@ -56,21 +56,21 @@ class QuillEditorImageEmbedBuilder extends EmbedBuilder {
5656
});
5757
if (attrs.isNotEmpty) {
5858
final width = double.tryParse(
59-
(base.isMobile()
59+
(base.isMobile(supportWeb: false)
6060
? attrs[Attribute.mobileWidth]
6161
: attrs[Attribute.width.key]) ??
6262
'',
6363
);
6464
final height = double.tryParse(
65-
(base.isMobile()
65+
(base.isMobile(supportWeb: false)
6666
? attrs[Attribute.mobileHeight]
6767
: attrs[Attribute.height.key]) ??
6868
'',
6969
);
70-
final alignment = base.getAlignment(base.isMobile()
70+
final alignment = base.getAlignment(base.isMobile(supportWeb: false)
7171
? attrs[Attribute.mobileAlignment]
7272
: attrs[Attribute.alignment]);
73-
final margin = (base.isMobile()
73+
final margin = (base.isMobile(supportWeb: false)
7474
? double.tryParse(Attribute.mobileMargin)
7575
: double.tryParse(Attribute.margin)) ??
7676
0.0;
@@ -106,7 +106,7 @@ class QuillEditorImageEmbedBuilder extends EmbedBuilder {
106106
}
107107

108108
if (!readOnly &&
109-
(base.isMobile() ||
109+
(base.isMobile(supportWeb: false) ||
110110
configurations.forceUseMobileOptionMenuForImageClick)) {
111111
return GestureDetector(
112112
onTap: () {
@@ -188,7 +188,8 @@ class QuillEditorImageEmbedBuilder extends EmbedBuilder {
188188
getImageStyleString(controller),
189189
width: w,
190190
height: h,
191-
isMobile: base.isMobile(),
191+
isMobile:
192+
base.isMobile(supportWeb: false),
192193
);
193194
controller
194195
..skipRequestKeyboard = true
@@ -220,7 +221,7 @@ class QuillEditorImageEmbedBuilder extends EmbedBuilder {
220221
if (!readOnly || isImageBase64(imageUrl)) {
221222
// To enforce using it on the desktop and other platforms
222223
// and that is up to the developer
223-
if (!base.isMobile() &&
224+
if (!base.isMobile(supportWeb: false) &&
224225
configurations.forceUseMobileOptionMenuForImageClick) {
225226
return _menuOptionsForReadonlyImage(
226227
context: context,

flutter_quill_extensions/lib/presentation/embeds/toolbar/image_button/select_image_source.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SelectImageSourceDialog extends StatelessWidget {
2828
'Take a photo using your phone camera',
2929
),
3030
leading: const Icon(Icons.camera),
31-
enabled: !isDesktop(),
31+
enabled: !isDesktop(supportWeb: false),
3232
onTap: () => Navigator.of(context).pop(InsertImageSource.camera),
3333
),
3434
ListTile(

flutter_quill_extensions/lib/presentation/embeds/toolbar/video_button/select_video_source.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SelectVideoSourceDialog extends StatelessWidget {
2828
'Record a video using your phone camera',
2929
),
3030
leading: const Icon(Icons.camera),
31-
enabled: !isDesktop(),
31+
enabled: !isDesktop(supportWeb: false),
3232
onTap: () => Navigator.of(context).pop(InsertVideoSource.camera),
3333
),
3434
ListTile(

flutter_quill_extensions/lib/presentation/models/config/editor/image/image.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ class QuillEditorImageEmbedConfigurations {
112112

113113
static ImageEmbedBuilderOnRemovedCallback get defaultOnImageRemovedCallback {
114114
return (imageUrl) async {
115-
final mobile = isMobile();
115+
if (isWeb()) {
116+
return;
117+
}
118+
119+
final mobile = isMobile(supportWeb: false);
116120
// If the platform is not mobile, return void;
117121
// Since the mobile OS gives us a copy of the image
118122

@@ -134,10 +138,6 @@ class QuillEditorImageEmbedConfigurations {
134138
// it without
135139
// permission
136140

137-
if (isWeb()) {
138-
return;
139-
}
140-
141141
final dartIoImageFile = File(imageUrl);
142142

143143
final isFileExists = await dartIoImageFile.exists();

lib/src/utils/platform.dart

Lines changed: 94 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,113 @@ import 'package:device_info_plus/device_info_plus.dart';
44
import 'package:flutter/foundation.dart'
55
show kIsWeb, TargetPlatform, defaultTargetPlatform;
66

7-
bool isWeb() {
8-
return kIsWeb;
7+
/// If you want to override the [kIsWeb] use [overrideIsWeb]
8+
bool isWeb({bool? overrideIsWeb}) {
9+
return overrideIsWeb ?? kIsWeb;
910
}
1011

11-
bool isMobile([TargetPlatform? targetPlatform]) {
12-
if (isWeb()) return false;
13-
targetPlatform ??= defaultTargetPlatform;
14-
return {TargetPlatform.iOS, TargetPlatform.android}.contains(targetPlatform);
12+
/// [supportWeb] is a parameter that ask you if we should care about web support
13+
/// if the value is true then we will return the result no matter if we are
14+
/// on web or using a native app to run the flutter app
15+
bool isMobile({
16+
required bool supportWeb,
17+
TargetPlatform? platform,
18+
bool? overrideIsWeb,
19+
}) {
20+
if (isWeb(overrideIsWeb: overrideIsWeb) && !supportWeb) return false;
21+
platform ??= defaultTargetPlatform;
22+
return {TargetPlatform.iOS, TargetPlatform.android}.contains(platform);
1523
}
1624

17-
bool isDesktop([TargetPlatform? targetPlatform]) {
18-
if (isWeb()) return false;
19-
targetPlatform ??= defaultTargetPlatform;
25+
/// [supportWeb] is a parameter that ask you if we should care about web support
26+
/// if the value is true then we will return the result no matter if we are
27+
/// on web or using a native app to run the flutter app
28+
bool isDesktop({
29+
required bool supportWeb,
30+
TargetPlatform? platform,
31+
bool? overrideIsWeb,
32+
}) {
33+
if (isWeb(overrideIsWeb: overrideIsWeb) && !supportWeb) return false;
34+
platform ??= defaultTargetPlatform;
2035
return {TargetPlatform.macOS, TargetPlatform.linux, TargetPlatform.windows}
21-
.contains(targetPlatform);
36+
.contains(platform);
2237
}
2338

24-
bool isKeyboardOS([TargetPlatform? targetPlatform]) {
25-
targetPlatform ??= defaultTargetPlatform;
26-
return isDesktop(targetPlatform) || targetPlatform == TargetPlatform.fuchsia;
39+
/// [supportWeb] is a parameter that ask you if we should care about web support
40+
/// if the value is true then we will return the result no matter if we are
41+
/// on web or using a native app to run the flutter app
42+
bool isKeyboardOS({
43+
required bool supportWeb,
44+
TargetPlatform? platform,
45+
bool? overrideIsWeb,
46+
}) {
47+
platform ??= defaultTargetPlatform;
48+
return isDesktop(
49+
platform: platform,
50+
supportWeb: supportWeb,
51+
overrideIsWeb: overrideIsWeb) ||
52+
platform == TargetPlatform.fuchsia;
2753
}
2854

29-
bool isAppleOS([TargetPlatform? targetPlatform]) {
30-
if (isWeb()) return false;
31-
targetPlatform ??= defaultTargetPlatform;
55+
/// [supportWeb] is a parameter that ask you if we should care about web support
56+
/// if the value is true then we will return the result no matter if we are
57+
/// on web or using a native app to run the flutter app
58+
bool isAppleOS({
59+
required bool supportWeb,
60+
TargetPlatform? platform,
61+
bool? overrideIsWeb,
62+
}) {
63+
if (isWeb(overrideIsWeb: overrideIsWeb) && !supportWeb) return false;
64+
platform ??= defaultTargetPlatform;
3265
return {
3366
TargetPlatform.macOS,
3467
TargetPlatform.iOS,
35-
}.contains(targetPlatform);
68+
}.contains(platform);
3669
}
3770

38-
bool isMacOS([TargetPlatform? targetPlatform]) {
39-
if (isWeb()) return false;
40-
targetPlatform ??= defaultTargetPlatform;
41-
return TargetPlatform.macOS == targetPlatform;
71+
/// [supportWeb] is a parameter that ask you if we should care about web support
72+
/// if the value is true then we will return the result no matter if we are
73+
/// on web or using a native app to run the flutter app
74+
bool isMacOS({
75+
required bool supportWeb,
76+
TargetPlatform? platform,
77+
bool? overrideIsWeb,
78+
}) {
79+
if (isWeb(overrideIsWeb: overrideIsWeb) && !supportWeb) return false;
80+
platform ??= defaultTargetPlatform;
81+
return TargetPlatform.macOS == platform;
4282
}
4383

44-
bool isIOS([TargetPlatform? targetPlatform]) {
45-
if (isWeb()) return false;
46-
targetPlatform ??= defaultTargetPlatform;
47-
return TargetPlatform.iOS == targetPlatform;
84+
/// [supportWeb] is a parameter that ask you if we should care about web support
85+
/// if the value is true then we will return the result no matter if we are
86+
/// on web or using a native app to run the flutter app
87+
bool isIOS({
88+
required bool supportWeb,
89+
TargetPlatform? platform,
90+
bool? overrideIsWeb,
91+
}) {
92+
if (isWeb(overrideIsWeb: overrideIsWeb) && !supportWeb) return false;
93+
platform ??= defaultTargetPlatform;
94+
return TargetPlatform.iOS == platform;
4895
}
4996

50-
bool isAndroid([TargetPlatform? targetPlatform]) {
51-
if (isWeb()) return false;
52-
targetPlatform ??= defaultTargetPlatform;
53-
return TargetPlatform.android == targetPlatform;
97+
/// [supportWeb] is a parameter that ask you if we should care about web support
98+
/// if the value is true then we will return the result no matter if we are
99+
/// on web or using a native app to run the flutter app
100+
bool isAndroid({
101+
required bool supportWeb,
102+
TargetPlatform? platform,
103+
bool? overrideIsWeb,
104+
}) {
105+
if (isWeb(overrideIsWeb: overrideIsWeb) && !supportWeb) return false;
106+
platform ??= defaultTargetPlatform;
107+
return TargetPlatform.android == platform;
54108
}
55109

56-
bool isFlutterTest() {
57-
if (isWeb()) return false;
58-
return Platform.environment.containsKey('FLUTTER_TEST');
59-
}
60-
61-
Future<bool> isIOSSimulator() async {
62-
if (!isAppleOS()) {
110+
Future<bool> isIOSSimulator({
111+
bool? overrideIsWeb,
112+
}) async {
113+
if (!isAppleOS(supportWeb: false, overrideIsWeb: overrideIsWeb)) {
63114
return false;
64115
}
65116

@@ -73,3 +124,10 @@ Future<bool> isIOSSimulator() async {
73124
}
74125
return false;
75126
}
127+
128+
bool isFlutterTest({
129+
bool? overrideIsWeb,
130+
}) {
131+
if (isWeb(overrideIsWeb: overrideIsWeb)) return false;
132+
return Platform.environment.containsKey('FLUTTER_TEST');
133+
}

lib/src/widgets/cursor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ class CursorPainter {
291291

292292
final caretHeight = editable!.getFullHeightForCaret(position);
293293
if (caretHeight != null) {
294-
if (isAppleOS()) {
294+
if (isAppleOS(supportWeb: true)) {
295295
// Center the caret vertically along the text.
296296
caretRect = Rect.fromLTWH(
297297
caretRect.left,

0 commit comments

Comments
 (0)