responsive_widget.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import 'package:flutter/material.dart';
  2. import 'package:router/componentRouter/app_service.dart';
  3. import 'package:router/componentRouter/component_service_manager.dart';
  4. import 'package:shared/utils/log_utils.dart';
  5. /// 响应式的布局,根据不同的尺寸展示不同的布局
  6. /// 桌面 - 平板 - 手机的布局判断,手机内部分为全面屏与非全面屏
  7. class ResponsiveWidget extends StatelessWidget {
  8. final Widget? largeScreen; // 大屏幕布局
  9. final Widget? mediumScreen; // 中等屏幕布局
  10. final Widget smallScreen; // 默认手机屏
  11. final Widget? smallFullScreen; // 手机的全面屏手机布局
  12. const ResponsiveWidget({
  13. super.key,
  14. this.largeScreen,
  15. this.mediumScreen,
  16. this.smallFullScreen,
  17. required this.smallScreen,
  18. });
  19. @override
  20. Widget build(BuildContext context) {
  21. return LayoutBuilder(
  22. builder: (context, constraints) {
  23. // 大屏幕
  24. if (constraints.maxWidth > 1200) {
  25. return largeScreen ?? smallScreen;
  26. }
  27. // 中等屏幕
  28. else if (constraints.maxWidth <= 1200 && constraints.maxWidth >= 800) {
  29. return mediumScreen ?? largeScreen ?? smallScreen;
  30. }
  31. // 小屏幕
  32. else {
  33. // 获取屏幕的高度和宽度
  34. final isFullScreen = ComponentServiceManager().appService.isDeviceFullScreen();
  35. Log.d("isFullScreen:$isFullScreen");
  36. // 判断是否为全面屏
  37. if (isFullScreen) {
  38. return smallFullScreen ?? smallScreen; // 如果是全面屏,使用全面屏布局
  39. } else {
  40. return smallScreen; // 否则使用默认手机布局
  41. }
  42. }
  43. },
  44. );
  45. }
  46. }