Showing
1 changed file
with
150 additions
and
0 deletions
| 1 | +import 'package:flutter/widgets.dart'; | ||
| 2 | + | ||
| 3 | +import '../../../get.dart'; | ||
| 4 | +import 'get_view.dart'; | ||
| 5 | + | ||
| 6 | +abstract class GetResponsive<T> extends GetView<T> { | ||
| 7 | + final ResponsiveScreen screen; | ||
| 8 | + GetResponsive(ResponsiveScreenSettings settings, {Key key}) | ||
| 9 | + : screen = ResponsiveScreen(settings), | ||
| 10 | + super(key: key); | ||
| 11 | + | ||
| 12 | + Widget builder(); | ||
| 13 | + Widget mobile(); | ||
| 14 | + Widget tablet(); | ||
| 15 | + Widget desktop(); | ||
| 16 | + Widget watch(); | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +class GetResponsiveView<T> extends GetResponsive<T> { | ||
| 20 | + final bool alwaysUseBuilder = true; | ||
| 21 | + GetResponsiveView( | ||
| 22 | + {alwaysUseBuilder, | ||
| 23 | + ResponsiveScreenSettings settings = const ResponsiveScreenSettings(), | ||
| 24 | + Key key}) | ||
| 25 | + : super(settings, key: key); | ||
| 26 | + @override | ||
| 27 | + Widget build(BuildContext context) { | ||
| 28 | + screen.context = context; | ||
| 29 | + if (alwaysUseBuilder) { | ||
| 30 | + return builder(); | ||
| 31 | + } | ||
| 32 | + Widget widget; | ||
| 33 | + if (screen.isDesktop) { | ||
| 34 | + widget = desktop(); | ||
| 35 | + if (widget != null) return widget; | ||
| 36 | + } | ||
| 37 | + if (screen.isTablet) { | ||
| 38 | + widget = tablet(); | ||
| 39 | + if (widget != null) return widget; | ||
| 40 | + } | ||
| 41 | + if (screen.isMobile) { | ||
| 42 | + widget = mobile(); | ||
| 43 | + if (widget != null) return widget; | ||
| 44 | + } | ||
| 45 | + return watch(); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @override | ||
| 49 | + Widget builder() => null; | ||
| 50 | + | ||
| 51 | + @override | ||
| 52 | + Widget desktop() => null; | ||
| 53 | + | ||
| 54 | + @override | ||
| 55 | + Widget mobile() => null; | ||
| 56 | + | ||
| 57 | + @override | ||
| 58 | + Widget tablet() => null; | ||
| 59 | + | ||
| 60 | + @override | ||
| 61 | + Widget watch() => null; | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +class ResponsiveScreenSettings { | ||
| 65 | + /// When the width is greater als this value | ||
| 66 | + /// the display will be set as [ScreenType.Desktop] | ||
| 67 | + final double desktopChangePoint; | ||
| 68 | + | ||
| 69 | + /// When the width is greater als this value | ||
| 70 | + /// the display will be set as [ScreenType.Tablet] | ||
| 71 | + /// or when width greater als [watchChangePoint] and smaller als this value | ||
| 72 | + /// the display will be [ScreenType.Mobile] | ||
| 73 | + final double tabletChangePoint; | ||
| 74 | + | ||
| 75 | + /// When the width is smaller als this value | ||
| 76 | + /// the display will be set as [ScreenType.Watch] | ||
| 77 | + /// or when width greater als this value and smaller als [tabletChangePoint] | ||
| 78 | + /// the display will be [ScreenType.Mobile] | ||
| 79 | + final double watchChangePoint; | ||
| 80 | + | ||
| 81 | + const ResponsiveScreenSettings( | ||
| 82 | + {this.desktopChangePoint = 1200, | ||
| 83 | + this.tabletChangePoint = 600, | ||
| 84 | + this.watchChangePoint = 300}); | ||
| 85 | +} | ||
| 86 | + | ||
| 87 | +class ResponsiveScreen { | ||
| 88 | + BuildContext context; | ||
| 89 | + final ResponsiveScreenSettings settings; | ||
| 90 | + | ||
| 91 | + bool _isPaltformDesktop; | ||
| 92 | + ResponsiveScreen(this.settings) { | ||
| 93 | + _isPaltformDesktop = GetPlatform.isDesktop; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + double get height => context.height; | ||
| 97 | + double get width => context.width; | ||
| 98 | + | ||
| 99 | + /// Is [screenType] [ScreenType.Desktop] | ||
| 100 | + bool get isDesktop => (screenType == ScreenType.Desktop); | ||
| 101 | + | ||
| 102 | + /// Is [screenType] [ScreenType.Tablet] | ||
| 103 | + bool get isTablet => (screenType == ScreenType.Tablet); | ||
| 104 | + | ||
| 105 | + /// Is [screenType] [ScreenType.Mobile] | ||
| 106 | + bool get isMobile => (screenType == ScreenType.Mobile); | ||
| 107 | + | ||
| 108 | + /// Is [screenType] [ScreenType.Watch] | ||
| 109 | + bool get isWatch => (screenType == ScreenType.Watch); | ||
| 110 | + | ||
| 111 | + double get _getdeviceWidth { | ||
| 112 | + if (_isPaltformDesktop) { | ||
| 113 | + return width; | ||
| 114 | + } | ||
| 115 | + return context.mediaQueryShortestSide; | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + ScreenType get screenType { | ||
| 119 | + final deviceWidth = _getdeviceWidth; | ||
| 120 | + if (deviceWidth >= settings.desktopChangePoint) return ScreenType.Desktop; | ||
| 121 | + if (deviceWidth >= settings.tabletChangePoint) return ScreenType.Tablet; | ||
| 122 | + if (deviceWidth < settings.watchChangePoint) return ScreenType.Watch; | ||
| 123 | + return ScreenType.Mobile; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + /// Return widget according to screen type | ||
| 127 | + /// if the [screenType] is [ScreenType.Desktop] and | ||
| 128 | + /// `desktop` object is null the `tablet` object will be returned | ||
| 129 | + /// and if `tablet` object is null the `mobile` object will be returned | ||
| 130 | + /// and if `mobile` object is null the `watch` object will be returned | ||
| 131 | + /// also when it is null. | ||
| 132 | + T responsiveValue<T>({ | ||
| 133 | + T mobile, | ||
| 134 | + T tablet, | ||
| 135 | + T desktop, | ||
| 136 | + T watch, | ||
| 137 | + }) { | ||
| 138 | + if (isDesktop && desktop != null) return desktop; | ||
| 139 | + if (isTablet && tablet != null) return tablet; | ||
| 140 | + if (isMobile && mobile != null) return mobile; | ||
| 141 | + return watch; | ||
| 142 | + } | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +enum ScreenType { | ||
| 146 | + Watch, | ||
| 147 | + Mobile, | ||
| 148 | + Tablet, | ||
| 149 | + Desktop, | ||
| 150 | +} |
-
Please register or login to post a comment