Use new display API to detect device size.

This commit is contained in:
Shawn 2023-05-10 14:23:52 -06:00
parent 18e3cb731e
commit 57cda296e6
5 changed files with 17 additions and 11 deletions

View File

@ -23,11 +23,11 @@ To try the app you can download it from your favorite app store:
If you're new to Flutter the first thing you'll need is to follow the [setup instructions](https://flutter.dev/docs/get-started/install). If you're new to Flutter the first thing you'll need is to follow the [setup instructions](https://flutter.dev/docs/get-started/install).
Once Flutter is setup, you can use the latest `beta` channel: Once Flutter is setup, you can use the latest `master` channel:
* `flutter channel beta` * `flutter channel master`
* `flutter upgrade` * `flutter upgrade`
Once on `beta` you're ready to run the app on your local device or simulator: Once on `master` you're ready to run the app on your local device or simulator:
* `flutter run -d ios` * `flutter run -d ios`
* `flutter run -d android` * `flutter run -d android`

View File

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:ui';
import 'package:desktop_window/desktop_window.dart'; import 'package:desktop_window/desktop_window.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart'; import 'package:flutter_displaymode/flutter_displaymode.dart';
@ -77,14 +78,16 @@ class AppLogic {
} }
/// Called from the UI layer once a MediaQuery has been obtained /// Called from the UI layer once a MediaQuery has been obtained
void handleAppSizeChanged(Size size) { void handleAppSizeChanged() {
/// Disable landscape layout on smaller form factors /// Disable landscape layout on smaller form factors
bool isSmall = size.shortestSide < 500 && size != Size.zero; bool isSmall = display.size.shortestSide / display.devicePixelRatio < 600;
supportedOrientations = isSmall ? [Axis.vertical] : [Axis.vertical, Axis.horizontal]; supportedOrientations = isSmall ? [Axis.vertical] : [Axis.vertical, Axis.horizontal];
_updateSystemOrientation(); _updateSystemOrientation();
} }
bool shouldUseNavRail(Size size) => size.width > size.height && size.height > 250; Display get display => PlatformDispatcher.instance.displays.first;
bool shouldUseNavRail() => true; //display.size.width > display.size.height && display.size.height > 250;
/// Enable landscape, portrait or both. Views can call this method to override the default settings. /// Enable landscape, portrait or both. Views can call this method to override the default settings.
/// For example, the [FullscreenVideoViewer] always wants to enable both landscape and portrait. /// For example, the [FullscreenVideoViewer] always wants to enable both landscape and portrait.

View File

@ -10,9 +10,12 @@ class WondersAppScaffold extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// Listen to the device size, and update AppStyle when it changes // Listen to the device size, and update AppStyle when it changes
_style = AppStyle(screenSize: context.sizePx); MediaQuery.of(context);
appLogic.handleAppSizeChanged();
// Set default timing for animations in the app
Animate.defaultDuration = _style.times.fast; Animate.defaultDuration = _style.times.fast;
appLogic.handleAppSizeChanged(context.mq.size); // Create a style object that will be passed down the widget tree
_style = AppStyle(screenSize: context.sizePx);
return KeyedSubtree( return KeyedSubtree(
key: ValueKey($styles.scale), key: ValueKey($styles.scale),
child: Theme( child: Theme(

View File

@ -72,7 +72,7 @@ class _WonderEditorialScreenState extends State<WonderEditorialScreen> {
/// Attempt to maintain a similar aspect ratio for the image within the app-bar /// Attempt to maintain a similar aspect ratio for the image within the app-bar
double maxAppBarHeight = min(context.widthPx, $styles.sizes.maxContentWidth1) * 1.2; double maxAppBarHeight = min(context.widthPx, $styles.sizes.maxContentWidth1) * 1.2;
bool showBackBtn = appLogic.shouldUseNavRail(context.mq.size) == false; bool showBackBtn = appLogic.shouldUseNavRail() == false;
return PopRouterOnOverScroll( return PopRouterOnOverScroll(
controller: _scroller, controller: _scroller,
child: ColoredBox( child: ColoredBox(

View File

@ -47,7 +47,7 @@ class _WonderDetailsScreenState extends State<WonderDetailsScreen>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
_useNavRail = appLogic.shouldUseNavRail(context.mq.size); _useNavRail = appLogic.shouldUseNavRail();
final wonder = wondersLogic.getData(widget.type); final wonder = wondersLogic.getData(widget.type);
int tabIndex = _tabController.index; int tabIndex = _tabController.index;