platform.dart 2.38 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8
import 'dart:io' show Platform;

import 'assertions.dart';

9
/// The platform that user interaction should adapt to target.
10 11
///
/// The [defaultTargetPlatform] getter returns the current platform.
12 13 14 15
enum TargetPlatform {
  /// Android: <https://www.android.com/>
  android,

16 17 18
  /// Fuchsia: <https://fuchsia.googlesource.com/>
  fuchsia,

19 20 21
  /// iOS: <http://www.apple.com/ios/>
  iOS,
}
22

23 24 25 26 27 28 29
/// The [TargetPlatform] that matches the platform on which the framework is
/// currently executing.
///
/// In a test environment, the platform returned is [TargetPlatform.android]
/// regardless of the host platform. (Android was chosen because the tests were
/// originally written assuming Android-like behavior, and we added platform
/// adaptations for iOS later). Tests can check iOS behavior by using the
30
/// platform override APIs (such as [ThemeData.platform] in the material
31
/// library) or by setting [debugDefaultTargetPlatformOverride]. The value
32
/// can only be explicitly set in debug mode.
33
TargetPlatform get defaultTargetPlatform {
34 35 36 37 38
  TargetPlatform result;
  if (Platform.isIOS || Platform.isMacOS) {
    result = TargetPlatform.iOS;
  } else if (Platform.isAndroid || Platform.isLinux) {
    result = TargetPlatform.android;
39
  } else if (Platform.operatingSystem == 'fuchsia') {
40
    result = TargetPlatform.fuchsia;
41 42 43 44
  }
  assert(() {
    if (Platform.environment.containsKey('FLUTTER_TEST'))
      result = TargetPlatform.android;
45 46
    if (debugDefaultTargetPlatformOverride != null)
      result = debugDefaultTargetPlatformOverride;
47
    return true;
48
  }());
49 50 51 52 53 54 55 56
  if (result == null) {
    throw new FlutterError(
      'Unknown platform.\n'
      '${Platform.operatingSystem} was not recognized as a target platform. '
      'Consider updating the list of TargetPlatforms to include this platform.'
    );
  }
  return result;
57
}
58 59 60 61 62 63 64 65 66
/// Override the [defaultTargetPlatform].
///
/// Setting this to null returns the [defaultTargetPlatform] to its original
/// value (based on the actual current platform).
///
/// This setter is only available intended for debugging purposes. To change the
/// target platform in release builds, use the platform override APIs (such as
/// [ThemeData.platform] in the material library).
TargetPlatform debugDefaultTargetPlatformOverride;