forked from flutter/gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptive.dart
42 lines (35 loc) · 1.44 KB
/
adaptive.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
enum DisplayType {
desktop,
mobile,
}
const _desktopPortraitBreakpoint = 700.0;
const _desktopLandscapeBreakpoint = 1000.0;
/// Returns the [DisplayType] for the current screen. This app only supports
/// mobile and desktop layouts, and as such we only have one breakpoint.
DisplayType displayTypeOf(BuildContext context) {
final orientation = MediaQuery.of(context).orientation;
final width = MediaQuery.of(context).size.width;
if ((orientation == Orientation.landscape &&
width > _desktopLandscapeBreakpoint) ||
(orientation == Orientation.portrait &&
width > _desktopPortraitBreakpoint)) {
return DisplayType.desktop;
} else {
return DisplayType.mobile;
}
}
/// Returns a boolean if we are in a display of [DisplayType.desktop]. Used to
/// build adaptive and responsive layouts.
bool isDisplayDesktop(BuildContext context) {
return displayTypeOf(context) == DisplayType.desktop;
}
/// Returns a boolean if we are in a display of [DisplayType.desktop] but less
/// than [_desktopLandscapeBreakpoint] width. Used to build adaptive and responsive layouts.
bool isDisplaySmallDesktop(BuildContext context) {
return isDisplayDesktop(context) &&
MediaQuery.of(context).size.width < _desktopLandscapeBreakpoint;
}