platform.dart 1.67 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 10 11 12 13
/// The platform that user interaction should adapt to target.
enum TargetPlatform {
  /// Android: <https://www.android.com/>
  android,

14 15 16
  /// Fuchsia: <https://fuchsia.googlesource.com/>
  fuchsia,

17 18 19
  /// iOS: <http://www.apple.com/ios/>
  iOS,
}
20

21 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
/// platform override APIs (like in [ThemeData.platform] in the material
/// library).
30
TargetPlatform get defaultTargetPlatform {
31 32 33 34 35
  TargetPlatform result;
  if (Platform.isIOS || Platform.isMacOS) {
    result = TargetPlatform.iOS;
  } else if (Platform.isAndroid || Platform.isLinux) {
    result = TargetPlatform.android;
36 37
  } else if (Platform.operatingSystem == "fuchsia") {
    result = TargetPlatform.fuchsia;
38 39 40 41 42 43 44 45 46 47 48 49 50 51
  }
  assert(() {
    if (Platform.environment.containsKey('FLUTTER_TEST'))
      result = TargetPlatform.android;
    return true;
  });
  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;
52
}