You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Theme.of(context).platform显示系统名称而非Web?

解决方案
  • 检测Web环境直接用Flutter提供的kIsWeb常量,它在flutter/foundation.dart包中,能准确判断当前是否运行在Web平台,不受宿主系统影响。
  • 代码示例:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class WebAdjustedPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 判断是否为Web环境
    final isWebPlatform = kIsWeb;
    // 根据环境设置地址栏调整高度
    final barHeightReduction = isWebPlatform ? 10.0 : 0.0;

    return Scaffold(
      body: Padding(
        // 结合系统顶部内边距,减去需要调整的高度
        padding: EdgeInsets.only(
          top: MediaQuery.of(context).padding.top - barHeightReduction
        ),
        child: Center(child: Text('适配Web地址栏后的页面')),
      ),
    );
  }
}
  • 关于Theme.of(context).platform返回macOS的原因:Web端的Flutter会默认继承运行所在的宿主系统平台信息,所以在Mac系统的Chrome中运行时,会返回macOS平台标识,这属于正常行为,因此不能依靠它来检测Web环境。

内容的提问来源于stack exchange,提问作者s4nk37

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 12:42:39