Line data Source code
1 : import 'dart:async'; 2 : 3 : import 'package:flutter/material.dart'; 4 : 5 : import 'ambiguate.dart'; 6 : 7 : /// BuildContext extension with some ui utils methods and getters 8 : extension ContextExtensionss on BuildContext { 9 : /// The same of [MediaQuery.of(context).size] 10 3 : Size get mediaQuerySize => MediaQuery.of(this).size; 11 : 12 : /// The same of [MediaQuery.of(context).size.height] 13 : /// Note: updates when you rezise your screen (like on a browser or 14 : /// desktop window) 15 3 : double get height => mediaQuerySize.height; 16 : 17 : /// The same of [MediaQuery.of(context).size.width] 18 : /// Note: updates when you rezise your screen (like on a browser or 19 : /// desktop window) 20 3 : double get width => mediaQuerySize.width; 21 : 22 : /// return true if the current app mode is dark 23 4 : bool get isDarkMode => Theme.of(this).brightness == Brightness.dark; 24 : 25 : /// similar to [MediaQuery.of(context).padding] 26 2 : ThemeData get theme => Theme.of(this); 27 : 28 : /// similar to [MediaQuery.of(context).padding] 29 3 : TextTheme get textTheme => Theme.of(this).textTheme; 30 : 31 : /// similar to [MediaQuery.of(context).padding] 32 3 : EdgeInsets get mediaQueryPadding => MediaQuery.of(this).padding; 33 : 34 : /// similar to [MediaQuery.of(context).padding] 35 2 : MediaQueryData get mediaQuery => MediaQuery.of(this); 36 : 37 : /// similar to [MediaQuery.of(context).viewPadding] 38 3 : EdgeInsets get mediaQueryViewPadding => MediaQuery.of(this).viewPadding; 39 : 40 : /// similar to [MediaQuery.of(context).viewInsets] 41 3 : EdgeInsets get mediaQueryViewInsets => MediaQuery.of(this).viewInsets; 42 : 43 : /// similar to [MediaQuery.of(context).orientation] 44 3 : Orientation get orientation => MediaQuery.of(this).orientation; 45 : 46 : /// check if device is on landscape mode 47 3 : bool get isLandscape => orientation == Orientation.landscape; 48 : 49 : /// check if device is on portrait mode 50 3 : bool get isPortrait => orientation == Orientation.portrait; 51 : 52 : /// similar to [MediaQuery.of(this).devicePixelRatio] 53 3 : double get devicePixelRatio => MediaQuery.of(this).devicePixelRatio; 54 : 55 : /// similar to [MediaQuery.of(this).textScaleFactor] 56 3 : double get textScaleFactor => MediaQuery.of(this).textScaleFactor; 57 : 58 : /// get the shortestSide from screen 59 3 : double get mediaQueryShortestSide => mediaQuerySize.shortestSide; 60 : 61 : /// True if the shortestSide is smaller than 480p 62 3 : bool get isSmallPhone => mediaQueryShortestSide < 480; 63 : 64 : /// True if the shortestSide is smaller than 600p 65 3 : bool get isPhone => mediaQueryShortestSide < 600; 66 : 67 : /// True if the shortestSide is largest than 600p 68 3 : bool get isSmallTablet => mediaQueryShortestSide >= 600; 69 : 70 : /// True if the shortestSide is largest than 720p 71 3 : bool get isLargeTablet => mediaQueryShortestSide >= 720; 72 : 73 : /// True if the current device is Tablet 74 2 : bool get isTablet => isSmallTablet || isLargeTablet; 75 : } 76 : 77 : /// Add with AfterLayoutMixin<MyWidget> mixin to your State<MyWidget> class and then implement 78 : /// the void afterFirstLayout(BuildContext context) abstract method. Code in this method will be 79 : /// called the first time this widget is laid out on the screen. 80 : mixin AfterFirstLayoutMixin<T extends StatefulWidget> on State<T> { 81 0 : @override 82 : void initState() { 83 0 : super.initState(); 84 0 : ambiguate(WidgetsBinding.instance)?.endOfFrame.then( 85 0 : (_) { 86 0 : if (mounted) { 87 0 : afterFirstLayout(context); 88 : } 89 : }, 90 : ); 91 : } 92 : 93 : /// Brings functionality to execute code after the first layout of a widget has been performed, 94 : /// i.e. after the first frame has been displayed. 95 : /// 96 : /// If you want to display a widget that depends on the layout, such as a Dialog or BottomSheet, 97 : /// you can not use that widget in initState instead of that you can use this method 98 : FutureOr<void> afterFirstLayout(BuildContext context); 99 : }