我有一个脚手架,当我在不同的手机上测试时,它总是给我溢出错误.我在代码中只用了mediaquery.of(context).size.height一次,我得到了40.392的溢出错误,这没有任何意义,因为我在声明高度时从来没有使用小数

    Scaffold(
      backgroundColor: Colors.black,
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(50),
        child: AppBar(
          elevation: 0,
          iconTheme: const IconThemeData(color: Colors.blue),
          backgroundColor: mode.background1,
          actions: actionList(),
        ),
      ),
      body: Container(
        height: MediaQuery.of(context).size.height - 104,
        child: Column(
          children: [
            Container(
                //overflow error exists right here
                height: MediaQuery.of(context).size.height - 107,
                child: getWid(_selectedindex)),
            Container(
              height: 3,
              child: Row(
                children: [
                  mydivider(0),
                  mydivider(1),
                  mydivider(2),
                  mydivider(3),
                ],
              ),
            )
          ],
        ),
      ),
      bottomNavigationBar: Theme(
        data: Theme.of(context).copyWith(
            brightness: Brightness.dark,
            canvasColor: mode.canvasColor,
            primaryColor: const Color.fromARGB(255, 45, 124, 243),
            textTheme: Theme.of(context)
                .textTheme
                .copyWith(bodySmall: const TextStyle(color: Colors.grey))),
        child: SizedBox(
          height: 54,
          child: BottomNavigationBar(
              onTap: (int index) {
                setState(() {
                  _selectedindex = index;
                });
              },
              currentIndex: _selectedindex,
              type: BottomNavigationBarType.fixed,
              selectedItemColor: Colors.blue,
              unselectedItemColor: Colors.grey,
              items: const [
                BottomNavigationBarItem(
                  icon: Icon(Icons.list),
                  label: 'List',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.local_grocery_store),
                  label: 'Checkout',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.history),
                  label: 'Transactions',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.settings_outlined),
                  label: 'Settings',
                )
              ]),
        ),
      ),
    )

很明显,我给了所有的小工具高度.我认为唯一合乎逻辑的解释是在这款手机上构建窗口小部件时出现了错误,但我不知道如何处理.另外,我以前在这款手机上测试过很多应用程序. 我感谢你的帮助

推荐答案

使用LayoutBuilder来构建用户界面,而不是使用MediaQuery.

return Scaffold(
  body: LayoutBuilder(
    builder: (context, constraints) {
      final width = constraints.maxWidth;

      return Text("");
    },
  ),
);

更多关于LayoutBuilderadaptive-responsive的信息.

Flutter相关问答推荐

类型int不是可迭代动态tmdbapi类型的子类型<>

如何刷新future 的构建器时,没有OBS变量的内部在Ffltter?

相同的Flutter 代码库,但Firebase登录适用于Web应用程序,但无法登录iOS应用程序

Flutter 蜂窝单元测试

Flutter 翼future 建造者不断开火和重建.它在每次重建时都会生成一个新的随机数--我如何防止这种情况?

FOR回路中的Flutter 模型

在 flutter 吧蜂巢中拯救主题

在使用FutureProvider时,我想显示用于加载的指示器,但无法自定义大小

使用现有Map方法对子类克隆方法的逻辑进行Dart标准化

使用 forLoops 在 Dart 中进行单元测试会返回 stackoverflow

Flutter Riverpod 对提供者状态的变化做出react

在我使用 flutter_riverpod stateprovider 重新保存代码之前,UI 主题状态不会更新

Flutter 3.10 - 'window' 已弃用,不应使用

Flutter ImagePicker - 在 onPressed 中异步获取图像显示 lint 警告

PreferredSizeWidget类不能用作混合,因为它既不是混合类也不是混合

如何更改 DropDownMenu 的显示方向?

Flutter/Dart - 从外部类更新状态

如何在向下滚动时隐藏顶部卡片并在向上滚动时出现?

Flutter - 异步文件读取异常处理

如何验证位于 PageView 内不同页面的 TextFormFields