responsive.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // ignore_for_file: no_leading_underscores_for_local_identifiers
  2. import 'package:flutter/material.dart';
  3. class ResponsiveWidget extends StatelessWidget {
  4. const ResponsiveWidget({
  5. Key? key,
  6. required this.mobileBody,
  7. this.tabletBody,
  8. this.desktopBody,
  9. }) : super(key: key);
  10. final Widget mobileBody;
  11. final Widget? tabletBody;
  12. final Widget? desktopBody;
  13. // This isMobile, isTablet, isDesktop helep us later
  14. static bool isMobile(BuildContext context) =>
  15. MediaQuery.of(context).size.width < 650;
  16. static bool isTablet(BuildContext context) =>
  17. MediaQuery.of(context).size.width < 1200 &&
  18. MediaQuery.of(context).size.width >= 650;
  19. static bool isDesktop(BuildContext context) =>
  20. MediaQuery.of(context).size.width >= 1200;
  21. static double fullWidth(BuildContext context) =>
  22. MediaQuery.of(context).size.width;
  23. static double fullHeight(BuildContext context) =>
  24. MediaQuery.of(context).size.height;
  25. static const kTabletBreakpoint =
  26. 768.0; //breakpoint for a tablet (a tablet's width is 768 px)
  27. static const kDesktopBreakPoint =
  28. 1024.0; //breakpoint for desktop (a desktop screen's width is 1440 px)
  29. static const kSideMenuWidth = 300.0; // for sidemenu
  30. static const kNavigationRailWidth = 72.0; // for navigation rail
  31. static const kMaxWidth = 1180.0; // maximum width
  32. @override
  33. Widget build(BuildContext context) {
  34. return LayoutBuilder(
  35. builder: (context, dimens) {
  36. // check if the device is a phone
  37. if (dimens.maxWidth >= kDesktopBreakPoint) {
  38. // returns mobileBody if desktopBody is null
  39. return desktopBody ?? mobileBody;
  40. } else if (dimens.maxWidth >= kTabletBreakpoint &&
  41. dimens.maxWidth < kDesktopBreakPoint) {
  42. // returns mobileBody if tabletBody is null
  43. return tabletBody ?? mobileBody;
  44. } else {
  45. return mobileBody;
  46. }
  47. },
  48. );
  49. }
  50. }
  51. class ScreenTypeData extends StatelessWidget {
  52. final Widget mobile;
  53. final Widget? tablet;
  54. final Widget desktop;
  55. const ScreenTypeData({
  56. required this.mobile,
  57. required this.desktop,
  58. Key? key,
  59. this.tablet,
  60. }) : super(key: key);
  61. // This size work fine on my design, maybe you need some customization depends on your design
  62. // This isMobile, isTablet, isDesktop helep us later
  63. static bool isMobile(BuildContext context) =>
  64. MediaQuery.of(context).size.width < 650;
  65. static bool isTablet(BuildContext context) =>
  66. MediaQuery.of(context).size.width < 1100 &&
  67. MediaQuery.of(context).size.width >= 650;
  68. static bool isDesktop(BuildContext context) =>
  69. MediaQuery.of(context).size.width >= 1100;
  70. static double fullWidth(BuildContext context) =>
  71. MediaQuery.of(context).size.width;
  72. static double fullHeight(BuildContext context) =>
  73. MediaQuery.of(context).size.height;
  74. @override
  75. Widget build(BuildContext context) {
  76. final _size = MediaQuery.of(context).size;
  77. // If our width is more than 1100 then we consider it a desktop
  78. if (_size.width >= 1100) {
  79. return desktop;
  80. }
  81. // If width it less then 1100 and more then 850 we consider it as tablet
  82. else if (_size.width >= 650 && tablet != null) {
  83. return tablet!;
  84. }
  85. // Or less then that we called it mobile
  86. else {
  87. return mobile;
  88. }
  89. }
  90. }