DevicePreset.fromJson constructor
Decodes a preset from the JSON produced by toJson.
Unknown keys are ignored; missing required keys or malformed values throw a FormatException.
Implementation
factory DevicePreset.fromJson(Map<String, Object?> json) {
final TargetPlatform platform = decodeEnum(
json['platform'],
TargetPlatform.values,
'platform',
);
// Bars that do not name their platform get the device's: paint-time
// behavior (Android tints bar backgrounds, iOS never does) must follow
// the simulated device, not the app's host. Specs in `device_specs/`
// rely on this — they never repeat the platform inside `systemUi`.
SystemUiSimulation? systemUi = json['systemUi'] == null
? null
: SystemUiSimulation.fromJson(decodeMap(json['systemUi'], 'systemUi'));
if (systemUi != null && systemUi.platform == null) {
systemUi = SystemUiSimulation(
statusBar: systemUi.statusBar,
navigationBar: systemUi.navigationBar,
platform: platform,
);
}
return DevicePreset(
id: decodeString(json['id'], 'id'),
name: decodeString(json['name'], 'name'),
brand: json['brand'] == null
? null
: decodeString(json['brand'], 'brand'),
year: json['year'] == null ? null : decodeInt(json['year'], 'year'),
platform: platform,
frame: json['frame'] == null
? null
: DeviceFrame.fromJson(decodeMap(json['frame'], 'frame')),
systemUi: systemUi,
portraitSize: decodeSize(json['portraitSize'], 'portraitSize'),
devicePixelRatio: decodeDouble(
json['devicePixelRatio'],
'devicePixelRatio',
),
portraitPadding: json['portraitPadding'] == null
? EdgeInsets.zero
: decodeEdgeInsets(json['portraitPadding'], 'portraitPadding'),
portraitViewPadding: json['portraitViewPadding'] == null
? null
: decodeEdgeInsets(
json['portraitViewPadding'],
'portraitViewPadding',
),
landscapePadding: json['landscapePadding'] == null
? null
: decodeEdgeInsets(json['landscapePadding'], 'landscapePadding'),
landscapeViewPadding: json['landscapeViewPadding'] == null
? null
: decodeEdgeInsets(
json['landscapeViewPadding'],
'landscapeViewPadding',
),
systemGestureInsets: json['systemGestureInsets'] == null
? EdgeInsets.zero
: decodeEdgeInsets(
json['systemGestureInsets'],
'systemGestureInsets',
),
portraitKeyboardHeight: json['portraitKeyboardHeight'] == null
? null
: decodeDouble(
json['portraitKeyboardHeight'],
'portraitKeyboardHeight',
),
landscapeKeyboardHeight: json['landscapeKeyboardHeight'] == null
? null
: decodeDouble(
json['landscapeKeyboardHeight'],
'landscapeKeyboardHeight',
),
displayFeatures: json['displayFeatures'] == null
? const <SimulatedDisplayFeature>[]
: List<SimulatedDisplayFeature>.unmodifiable(
decodeList(json['displayFeatures'], 'displayFeatures').map(
(Object? e) => SimulatedDisplayFeature.fromJson(
decodeMap(e, 'displayFeatures[]'),
),
),
),
kind: json['kind'] == null
? DeviceKind.phone
: decodeEnum(json['kind'], DeviceKind.values, 'kind'),
);
}