responsive.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import 'package:flutter/material.dart';
  2. /// Description: 响应式布局
  3. /// Time : 08/03/2022 Wednesday
  4. /// Author : liuyuqi.gov@msn.cn
  5. class Responsive extends StatelessWidget {
  6. final Widget mobile;
  7. final Widget tablet;
  8. final Widget desktop;
  9. const Responsive({
  10. Key key,
  11. @required this.mobile,
  12. @required this.tablet,
  13. @required this.desktop,
  14. }) : super(key: key);
  15. // This size work fine on my design, maybe you need some customization depends on your design
  16. static bool isMobile(BuildContext context) =>
  17. MediaQuery.of(context).size.width < 650;
  18. static bool isTablet(BuildContext context) =>
  19. MediaQuery.of(context).size.width < 900 &&
  20. MediaQuery.of(context).size.width >= 650;
  21. static bool isDesktop(BuildContext context) =>
  22. MediaQuery.of(context).size.width >= 900;
  23. @override
  24. Widget build(BuildContext context) {
  25. return LayoutBuilder(
  26. // If our width is more than 900 then we consider it a desktop
  27. builder: (context, constraints) {
  28. if (constraints.maxWidth >= 900) {
  29. return desktop;
  30. }
  31. // If width it less then 900 and more then 650 we consider it as tablet
  32. else if (constraints.maxWidth >= 650) {
  33. return tablet;
  34. }
  35. // Or less then that we called it mobile
  36. else {
  37. return mobile;
  38. }
  39. },
  40. );
  41. }
  42. }