Description
Currently, the Flutter framework relies on hardcoded platform identity checks (e.g., defaultTargetPlatform or Theme.of(context).platform) to determine behavior. This creates friction for out-of-tree custom embedders, who are forced to spoof their OS identity to inherit basic capabilities.
Proposal:
- Replace hardcoded platform identity checks within the core framework with capability-based queries (e.g.,
supportsPhysicsBasedScrolling).
- Establish and document a mechanism for custom embedders to declare their supported capabilities to the framework.
- Allow existing Flutter-maintained platforms (like Android) to serve as an extensible "base." This would allow custom platforms that share most (but not all) behaviors with a supported platform to inherit its capabilities and deviate slightly only where needed.
Here are several examples:
-
FlexibleSpaceBar uses Theme.of(context).platform to decide whether to center titles.
|
bool _getEffectiveCenterTitle(ThemeData theme) { |
|
return widget.centerTitle ?? |
|
switch (theme.platform) { |
|
TargetPlatform.android || |
|
TargetPlatform.fuchsia || |
|
TargetPlatform.linux || |
|
TargetPlatform.windows => false, |
|
TargetPlatform.iOS || TargetPlatform.macOS => true, |
|
}; |
|
} |
-
Drawer relies on platform checks to determine layout, semantics, and system back button presence, ex:
|
final String? label = switch (defaultTargetPlatform) { |
|
TargetPlatform.iOS || TargetPlatform.macOS => semanticLabel, |
|
TargetPlatform.android || |
|
TargetPlatform.fuchsia || |
|
TargetPlatform.linux || |
|
TargetPlatform.windows => semanticLabel ?? MaterialLocalizations.of(context).drawerLabel, |
|
}; |
-
TextMagnifier hardcodes the magnifier implementation by switching on defaultTargetPlatform, returning null on unrecognized platforms.
|
switch (defaultTargetPlatform) { |
|
case TargetPlatform.iOS: |
|
return CupertinoTextMagnifier(controller: controller, magnifierInfo: magnifierInfo); |
|
case TargetPlatform.android: |
|
return TextMagnifier(magnifierInfo: magnifierInfo); |
|
case TargetPlatform.fuchsia: |
|
case TargetPlatform.linux: |
|
case TargetPlatform.macOS: |
|
case TargetPlatform.windows: |
|
return null; |
|
} |
-
The Radio.adaptive constructor uses a switch statement to return a CupertinoRadio for Apple platforms and defaults to the standard Material radio for all others.
|
switch (theme.platform) { |
|
case TargetPlatform.android: |
|
case TargetPlatform.fuchsia: |
|
case TargetPlatform.linux: |
|
case TargetPlatform.windows: |
|
break; |
|
case TargetPlatform.iOS: |
|
case TargetPlatform.macOS: |
|
return CupertinoRadio<T>( |
|
value: widget.value, |
|
groupValue: widget.groupValue, |
|
onChanged: widget.onChanged, |
|
mouseCursor: widget.mouseCursor, |
|
toggleable: widget.toggleable, |
|
activeColor: widget.activeColor, |
|
focusColor: widget.focusColor, |
|
focusNode: _focusNode, |
|
autofocus: widget.autofocus, |
|
useCheckmarkStyle: widget.useCupertinoCheckmarkStyle, |
|
groupRegistry: _effectiveRegistry, |
|
enabled: _enabled, |
|
); |
|
} |
-
Autocomplete assigns keyboard shortcuts by switching on defaultTargetPlatform.
|
...switch (defaultTargetPlatform) { |
|
TargetPlatform.iOS => _appleShortcuts, |
|
TargetPlatform.macOS => _appleShortcuts, |
|
TargetPlatform.android => _nonAppleShortcuts, |
|
TargetPlatform.linux => _nonAppleShortcuts, |
|
TargetPlatform.windows => _nonAppleShortcuts, |
|
TargetPlatform.fuchsia => _nonAppleShortcuts, |
|
}, |
Description
Currently, the Flutter framework relies on hardcoded platform identity checks (e.g.,
defaultTargetPlatformorTheme.of(context).platform) to determine behavior. This creates friction for out-of-tree custom embedders, who are forced to spoof their OS identity to inherit basic capabilities.Proposal:
supportsPhysicsBasedScrolling).Here are several examples:
FlexibleSpaceBarusesTheme.of(context).platformto decide whether to center titles.flutter/packages/flutter/lib/src/material/flexible_space_bar.dart
Lines 191 to 200 in 00b14ee
Drawerrelies on platform checks to determine layout, semantics, and system back button presence, ex:flutter/packages/flutter/lib/src/material/drawer.dart
Lines 253 to 259 in 00b14ee
TextMagnifierhardcodes the magnifier implementation by switching ondefaultTargetPlatform, returning null on unrecognized platforms.flutter/packages/flutter/lib/src/material/magnifier.dart
Lines 47 to 57 in 00b14ee
The
Radio.adaptiveconstructor uses a switch statement to return a CupertinoRadio for Apple platforms and defaults to the standard Material radio for all others.flutter/packages/flutter/lib/src/material/radio.dart
Lines 518 to 540 in 00b14ee
Autocompleteassigns keyboard shortcuts by switching ondefaultTargetPlatform.flutter/packages/flutter/lib/src/widgets/autocomplete.dart
Lines 412 to 419 in 00b14ee