import 'dart:math' as math show max; import 'package:fis_ui/utils/sizer/sizer.dart'; import 'package:flutter/material.dart'; /// Web应用容器 class WebAppContainer extends StatefulWidget { const WebAppContainer( this.builder, { Key? key, }) : super(key: key); final Widget Function(bool isFirstBuild) builder; @override State createState() => _WebAppContainer(); } class _WebAppContainer extends State { late bool isMobile; bool isFirstBuild = true; final sizer = Sizer.ins; @override Widget build(BuildContext context) { final isMobile = sizer.isMobile; final size = sizer.size; final judgment = sizer.judgment; final limitWidth = isMobile ? judgment.minMobileWidth : judgment.minDesktopWidth; final limitHeight = isMobile ? judgment.minMobileHeight : judgment.minDesktopHeight; // 限制渲染最小尺寸 return Container( alignment: Alignment.topLeft, constraints: BoxConstraints( minWidth: math.max(size.width, limitWidth), minHeight: math.max(size.height, limitHeight), ), child: widget.builder.call(isFirstBuild), ); // return FittedBox( // fit: BoxFit.cover, // alignment: Alignment.topLeft, // child: body, // ); } @override void initState() { isMobile = sizer.isMobile; sizer.addListener(handleResize); super.initState(); } @override void dispose() { sizer.removeListener(handleResize); super.dispose(); } void handleResize(Size size) { print("app resize: $size"); if (sizer.isMobile != isMobile) { // Mobile/Desktop 尺寸切换时,重建APP print("app rebuild after resize."); setState(() { isFirstBuild = false; isMobile = sizer.isMobile; }); } } }