簡體   English   中英

展開的小部件拋出“RenderBox 未布局”異常

[英]Expanded widget throws "RenderBox was not laid out" exception

嗨,我是 Flutter 的新手,我遇到了異常“RenderBox 未布局”的麻煩。 我正在嘗試創建MyDialog ,它是內置對話框的擴展。 它可以從其他小部件中注入內容。 我想將Expanded widget wrapping Text widget 作為內容放置,以便可以看到所有帶有換行符的文本。

我打算這樣顯示內容: 在此處輸入圖像描述

這是我的MyDialog小部件。

class MyDialog extends StatelessWidget {
  String label;
  Widget contents;
  const MyDialog({required this.label, required this.contents, Key? key}) : super(key : key)

  @override
  Widget build(BuildContext context) {
    return MyButtonTip(    // This is an extension of outlined button.
      label     : label,
      onPressed : () {
        showDialog(
          context : context,
          builder : (BuildContext context) => AlertDialog(
            insetPadding    : const EdgeInsets.all(15),
            content         : SizedBox(
              width : MediaQuery.of(context).size.width,
              child : contents
            )
          )
        );
      }
    );
  }
}

這是使用MyDialog的小部件。

...
MyDialog(
  label : 'Profile',
  contents : SizedBox(
    child : Stack(
      children : [
        Column(
          children : [
            Row(
              children : [
                Column(
                  crossAxisAlignment : CrossAxisAlignment.end,
                  children           : const [
                    Text('name'),
                    Text('nickname')
                  ]
                ),
                const SizedBox(width : 10),
                Column(
                  crossAxisAlignment : CrossAxisAlignment.start,
                  children           : [
                    Text(name),
                    Expanded(
                      child : Text(nickname)
                    )
                  ]
                )
              ]
            )
          ]
        )
      ]
    ))
  )
);
...

(我正在使用Stack來瞄准不同的東西,所以請不要關心它。)

我想知道正確的解決方案,因為我之前多次遇到此異常,並且每次都以某種方式解決了。 請詳細告訴我。

謝謝,

附言。 安慰: 在此處輸入圖像描述

我認為您將小部件放錯了,請嘗試使用以下代碼:

    MyDialog(
  label : 'Profile',
  contents : Column(children: [
          Row(children: const [
            Text('name : '),
            Text("hello"),
          ]),
          const SizedBox(width: 10),
          Row(crossAxisAlignment: CrossAxisAlignment.start, children: const [
            Text('taro yamuda : '),
            Expanded(
              child: Text(
                "someting sometingsometingsometingsometingsometingsometingsometingsometingsometingsometingsometingsometing",
                style: TextStyle(),
              ),
            )
          ])
        ]);
      
      
      }

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM