webapp.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import 'dart:math' as math show max;
  2. import 'package:fis_ui/utils/sizer/sizer.dart';
  3. import 'package:flutter/material.dart';
  4. /// Web应用容器
  5. class WebAppContainer extends StatefulWidget {
  6. const WebAppContainer(
  7. this.builder, {
  8. Key? key,
  9. }) : super(key: key);
  10. final Widget Function(bool isFirstBuild) builder;
  11. @override
  12. State<StatefulWidget> createState() => _WebAppContainer();
  13. }
  14. class _WebAppContainer extends State<WebAppContainer> {
  15. late bool isMobile;
  16. bool isFirstBuild = true;
  17. final sizer = Sizer.ins;
  18. @override
  19. Widget build(BuildContext context) {
  20. final isMobile = sizer.isMobile;
  21. final size = sizer.size;
  22. final judgment = sizer.judgment;
  23. final limitWidth =
  24. isMobile ? judgment.minMobileWidth : judgment.minDesktopWidth;
  25. final limitHeight =
  26. isMobile ? judgment.minMobileHeight : judgment.minDesktopHeight;
  27. // 限制渲染最小尺寸
  28. return Container(
  29. alignment: Alignment.topLeft,
  30. constraints: BoxConstraints(
  31. minWidth: math.max(size.width, limitWidth),
  32. minHeight: math.max(size.height, limitHeight),
  33. ),
  34. child: widget.builder.call(isFirstBuild),
  35. );
  36. // return FittedBox(
  37. // fit: BoxFit.cover,
  38. // alignment: Alignment.topLeft,
  39. // child: body,
  40. // );
  41. }
  42. @override
  43. void initState() {
  44. isMobile = sizer.isMobile;
  45. sizer.addListener(handleResize);
  46. super.initState();
  47. }
  48. @override
  49. void dispose() {
  50. sizer.removeListener(handleResize);
  51. super.dispose();
  52. }
  53. void handleResize(Size size) {
  54. print("app resize: $size");
  55. if (sizer.isMobile != isMobile) {
  56. // Mobile/Desktop 尺寸切换时,重建APP
  57. print("app rebuild after resize.");
  58. setState(() {
  59. isFirstBuild = false;
  60. isMobile = sizer.isMobile;
  61. });
  62. }
  63. }
  64. }