当前位置: 首页 > article >正文

解决Flutter报错boxconstraints has non-normalized height/width constraints

出错场景

如果我们在使用约束时没有正确的传入宽高,比如以下代码

ConstrainedBox(
  /// 设置最小高度为150, 最大高度为100.
  constraints: BoxConstraints(minHeight: 150,maxHeight: 100),
  child: Container(
    color: Colors.red,
    child: Center(
      child: Text('呵呵'),
    ),
  ),
),

运行会报以下错误

======== Exception caught by widgets library =======================================================
The following assertion was thrown building AsyncTaskPage(dirty, dependencies: [MediaQuery, _ViewScope], state: _AsyncTaskPageState#e16b3):
BoxConstraints has non-normalized height constraints.

The offending constraints were: BoxConstraints(0.0<=w<=Infinity, 150.0<=h<=100.0; NOT NORMALIZED)
The relevant error-causing widget was: 
  AsyncTaskPage AsyncTaskPage:file:///Users/ado/work/flutter/flutter_app/lib/main.dart:115:30
When the exception was thrown, this was the stack: 
#0      BoxConstraints.debugAssertIsValid.<anonymous closure>.throwError (package:flutter/src/rendering/box.dart:520:9)
#1      BoxConstraints.debugAssertIsValid.<anonymous closure> (package:flutter/src/rendering/box.dart:563:9)
#2      BoxConstraints.debugAssertIsValid (package:flutter/src/rendering/box.dart:578:6)
#3      new ConstrainedBox (package:flutter/src/widgets/basic.dart:2511:27)
#4      _AsyncTaskPageState._centerScrollEffect (package:flutter_app/async_task_page.dart:74:11)
#5      _AsyncTaskPageState.build (package:flutter_app/async_task_page.dart:48:13)
#6      StatefulElement.build (package:flutter/src/widgets/framework.dart:5198:27)
#7      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5086:15)
#8      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5251:11)
#9      Element.rebuild (package:flutter/src/widgets/framework.dart:4805:7)
#10     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2780:19)
#11     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:903:21)
#12     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:358:5)
#13     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1284:15)
#14     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1214:9)
#15     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:939:7)
#19     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189:12)
(elided 3 frames from class _Timer and dart:async-patch)
====================================================================================================

源码分析

进入BoxConstraintsdebugAssertIsValid方法查看(package:flutter/src/rendering/box.dart:520:9)

      if (minWidth < 0.0 && minHeight < 0.0) {
        throwError(ErrorSummary('BoxConstraints has both a negative minimum width and a negative minimum height.'));
      }
      if (minWidth < 0.0) {
        throwError(ErrorSummary('BoxConstraints has a negative minimum width.'));
      }
      if (minHeight < 0.0) {
        throwError(ErrorSummary('BoxConstraints has a negative minimum height.'));
      }
      if (maxWidth < minWidth && maxHeight < minHeight) {
        throwError(ErrorSummary('BoxConstraints has both width and height constraints non-normalized.'));
      }
      if (maxWidth < minWidth) {
        throwError(ErrorSummary('BoxConstraints has non-normalized width constraints.'));
      }
      if (maxHeight < minHeight) {
        throwError(ErrorSummary('BoxConstraints has non-normalized height constraints.'));
      }
      if (isAppliedConstraint) {
        if (minWidth.isInfinite && minHeight.isInfinite) {
          throwError(ErrorSummary('BoxConstraints forces an infinite width and infinite height.'));
        }
        if (minWidth.isInfinite) {
          throwError(ErrorSummary('BoxConstraints forces an infinite width.'));
        }
        if (minHeight.isInfinite) {
          throwError(ErrorSummary('BoxConstraints forces an infinite height.'));
        }
      }

通过代码得知,只要出现以上最小宽高为负数、最小宽高大于最大宽高、最小宽高是无穷大就会报错,所以我们只需要在使用约束的时候注意设置正确的minWidth、maxWidth、minHeight、maxHeight即可。

正确代码

ConstrainedBox(
  /// 设置最小高度为100, 最大高度为150.
  constraints: BoxConstraints(minHeight: 100,maxHeight: 150),
  child: Container(
    color: Colors.red,
    child: Center(
      child: Text('呵呵'),
    ),
  ),
),

http://www.kler.cn/a/154033.html

相关文章:

  • Figma中文网:UI设计师的新资源宝库
  • 【计算机网络安全】湖北大学-mysql事务隔离性实验
  • 【git】git取消提交的内容,恢复到暂存区
  • 【AI+教育】一些记录@2024.11.11
  • 51单片机--- 矩阵按键仿真
  • django的model时间怎么转时间戳
  • 设计模式-结构型模式之组合、享元设计模式
  • 最长连续递增序列
  • [学习记录]Node event loop 总结流程图
  • STL--位图的介绍与使用
  • 以热爱的态度对待生活,就是最自己的温柔
  • 软著项目推荐 深度学习疲劳驾驶检测 opencv python
  • 线程的状态
  • 详解原生Spring框架下的方法切入点表达式
  • 【IEEE出版|往届均已成功EI检索】2024年第四届消费电子与计算机工程国际学术会议(ICCECE 2024)
  • 智慧工地一体化解决方案(里程碑管理)源码
  • 背包9讲系列2-完全背包问题
  • 《论文阅读》DualGATs:用于对话中情绪识别的双图注意力网络
  • 正确理解MySQL的MVCC及实现原理
  • 八、Lua数组和迭代器
  • 【微软技术栈】数据并行和任务并行中的潜在缺陷
  • (Linux2.6内核)进程调度队列与切换
  • Wireshark使用详解
  • TCP 重传、滑动窗口、流量控制、拥塞控制
  • vue el-button 封装及使用
  • Course2-Week1-神经网络