| 1 | import 'package:flutter/material.dart'; |
| 2 | import 'package:mobx/mobx.dart'; |
| 3 | |
| 4 | part 'responsive_layout_util.g.dart'; |
| 5 | |
| 6 | class _ResponsiveLayoutUtil = ResponsiveLayoutUtilBase with _$_ResponsiveLayoutUtil; |
| 7 | |
| 8 | abstract class ResponsiveLayoutUtilBase with Store, WidgetsBindingObserver { |
| 9 | static const double kMobileThreshold = 550; |
| 10 | static const double kDesktopMaxWidthConstraint = 400; |
| 11 | static const double kDesktopMaxDashBoardWidthConstraint = 900; |
| 12 | static const double kPopupWidth = 400; |
| 13 | static const double kPopupSpaceHeight = 100; |
| 14 | |
| 15 | ResponsiveLayoutUtilBase() { |
| 16 | WidgetsBinding.instance.addObserver(this); |
| 17 | final initialMediaQuery = MediaQueryData.fromView(WidgetsBinding.instance!.window); |
| 18 | updateDeviceInfo(initialMediaQuery); |
| 19 | } |
| 20 | |
| 21 | @override |
| 22 | void didChangeMetrics() { |
| 23 | final mediaQuery = MediaQueryData.fromView(WidgetsBinding.instance!.window); |
| 24 | updateDeviceInfo(mediaQuery); |
| 25 | } |
| 26 | |
| 27 | @observable |
| 28 | double screenWidth = 0.0; |
| 29 | |
| 30 | @observable |
| 31 | double screenHeight = 0.0; |
| 32 | |
| 33 | @observable |
| 34 | Orientation orientation = Orientation.portrait; |
| 35 | |
| 36 | @action |
| 37 | void updateDeviceInfo(MediaQueryData mediaQuery) { |
| 38 | orientation = mediaQuery.orientation; |
| 39 | screenWidth = mediaQuery.size.width; |
| 40 | screenHeight = mediaQuery.size.height; |
| 41 | } |
| 42 | |
| 43 | @computed |
| 44 | bool get shouldRenderMobileUI { |
| 45 | return (screenWidth <= kMobileThreshold) || |
| 46 | (orientation == Orientation.portrait && screenWidth < screenHeight) || |
| 47 | (orientation == Orientation.landscape && screenWidth < screenHeight); |
| 48 | } |
| 49 | |
| 50 | bool get shouldRenderTabletUI { |
| 51 | return screenWidth > kMobileThreshold && screenWidth < kDesktopMaxDashBoardWidthConstraint; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | _ResponsiveLayoutUtil _singletonResponsiveLayoutUtil = _ResponsiveLayoutUtil(); |
| 56 | |
| 57 | _ResponsiveLayoutUtil get responsiveLayoutUtil => _singletonResponsiveLayoutUtil; |