Flutter 布局報錯The following assertion was thrown during performLayout():

編寫 flutter 代碼碰到一個錯誤

======== Exception caught by rendering library =====================================================
The following assertion was thrown during performLayout():
BoxConstraints forces an infinite width.

These invalid constraints were provided to _RenderColoredBox's layout() function by the following function, which probably computed the invalid constraints in question:
  RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:268:14)
The offending constraints were: BoxConstraints(w=Infinity, h=44.0)
The relevant error-causing widget was: 
  Container file:///Users/project_github/flutter/my_flutter_demo/lib/error_page.dart:20:20
When the exception was thrown, this was the stack: 
#0      BoxConstraints.debugAssertIsValid.<anonymous closure>.throwError (package:flutter/src/rendering/box.dart:517:9)
#1      BoxConstraints.debugAssertIsValid.<anonymous closure> (package:flutter/src/rendering/box.dart:559:21)
#2      BoxConstraints.debugAssertIsValid (package:flutter/src/rendering/box.dart:565:6)
#3      RenderObject.layout (package:flutter/src/rendering/object.dart:1677:24)
#4      RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:268:14)
...
The following RenderObject was being processed when the exception was fired: RenderConstrainedBox#e8eb9 relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...  parentData: bottom=0.0; offset=Offset(0.0, 515.0) (can use size)
...  constraints: BoxConstraints(unconstrained)
...  size: Size(360.0, 44.0)
...  additionalConstraints: BoxConstraints(w=Infinity, h=44.0)
RenderObject: RenderConstrainedBox#e8eb9 relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  parentData: bottom=0.0; offset=Offset(0.0, 515.0) (can use size)
  constraints: BoxConstraints(unconstrained)
  size: Size(360.0, 44.0)
  additionalConstraints: BoxConstraints(w=Infinity, h=44.0)
...  child: _RenderColoredBox#4f7c5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...    parentData: <none> (can use size)
...    constraints: BoxConstraints(w=360.0, h=44.0)
...    size: Size(360.0, 44.0)
...    behavior: opaque
...    child: RenderPositionedBox#c8a6c NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...      parentData: <none> (can use size)
...      constraints: BoxConstraints(w=360.0, h=44.0)
...      size: Size(360.0, 44.0)
...      alignment: center
...      textDirection: ltr
...      widthFactor: expand
...      heightFactor: expand
...      child: RenderParagraph#2f2e8 relayoutBoundary=up1 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
...        parentData: offset=Offset(152.0, 11.5) (can use size)
...        constraints: BoxConstraints(0.0<=w<=360.0, 0.0<=h<=44.0)
...        size: Size(56.0, 21.0)
...        textAlign: start
...        textDirection: ltr
...        softWrap: wrapping at box width
...        overflow: clip
...        locale: en_US
...        maxLines: unlimited
...        text: TextSpan
...          debugLabel: ((englishLike body1 2014).merge(blackMountainView bodyText2)).merge(unknown)
...          inherit: false
...          color: Color(0xff000000)
...          family: Roboto
...          size: 14.0
...          weight: 500
...          baseline: alphabetic
...          decoration: TextDecoration.none
...          "確認"
====================================================================================================

我的代碼


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ErrorPage extends StatefulWidget {
  @override
  _MyErrorPageState createState() => _MyErrorPageState();

}

class _MyErrorPageState extends State<ErrorPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("測試頁面"),),
      body: Stack(
        children: [
          Positioned(
            bottom: 0,
            child: Container(
            color: Colors.green,
            width: double.infinity,
            height: 44,
            alignment: Alignment.center,
            child: Text(
              "確認",
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  fontWeight: FontWeight.w500
              ),
            ),
          )
          )
        ],
      )
    );
  }

}

解決辦法

給Positioned 設置一個寬度即可

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ErrorPage extends StatefulWidget {
  @override
  _MyErrorPageState createState() => _MyErrorPageState();
}

class _MyErrorPageState extends State<ErrorPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("測試頁面"),
        ),
        body: Stack(
          children: [
            Positioned(
                bottom: 0,
                width: MediaQuery.of(context).size.width,//給 Positioned 設置一個寬度即可
                child: Container(
                  color: Colors.green,
                  width: double.infinity,
                  height: 44,
                  alignment: Alignment.center,
                  child: Text(
                    "確認",
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 14,
                        fontWeight: FontWeight.w500),
                  ),
                ))
          ],
        ));
  }
}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末叫乌,一起剝皮案震驚了整個濱河市柴罐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌憨奸,老刑警劉巖革屠,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡似芝,警方通過查閱死者的電腦和手機那婉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來党瓮,“玉大人详炬,你說我怎么就攤上這事∧椋” “怎么了呛谜?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蝇闭。 經(jīng)常有香客問我呻率,道長硬毕,這世上最難降的妖魔是什么呻引? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮吐咳,結(jié)果婚禮上逻悠,老公的妹妹穿的比我還像新娘。我一直安慰自己韭脊,他們只是感情好童谒,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著沪羔,像睡著了一般饥伊。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蔫饰,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天琅豆,我揣著相機與錄音,去河邊找鬼篓吁。 笑死茫因,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的杖剪。 我是一名探鬼主播冻押,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼盛嘿!你這毒婦竟也來了洛巢?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤次兆,失蹤者是張志新(化名)和其女友劉穎狼渊,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡狈邑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年城须,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片米苹。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡计福,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出扣孟,到底是詐尸還是另有隱情计雌,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布训唱,位于F島的核電站褥蚯,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏况增。R本人自食惡果不足惜赞庶,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望澳骤。 院中可真熱鬧歧强,春花似錦、人聲如沸为肮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽颊艳。三九已至茅特,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間棋枕,已是汗流浹背白修。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留戒悠,地道東北人熬荆。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像绸狐,于是被迫代替她去往敵國和親卤恳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容