gpt4 book ai didi

Flutter:在布局期间对象被赋予无限大小

转载 作者:行者123 更新时间:2023-12-03 02:40:58 26 4
gpt4 key购买 nike

我正在解决一个问题,我收到错误“ 布局期间对象被赋予无限大小

“这可能意味着它是一个试图尽可能大的渲染对象,但它被放置在另一个渲染对象中,允许其子项选择自己的大小。”。
我明白这意味着什么,但不知道我如何仍然可以保留我当前的小部件树 响应式 (它在运行时呈现,所以对于前端用户来说似乎没有问题)同时仍然解决这个问题。目前我没有设置某些东西的大小来保持响应,和 如果可能,希望避免小部件的硬编码大小 .
非常感谢任何正确方向的帮助或指示:)
这是我当前的代码:

    class EditArtikel extends StatefulWidget {
final String path;

EditArtikel({this.path});

@override
_EditArtikelState createState() => _EditArtikelState(path: path);
}

class _EditArtikelState extends State<EditArtikel> {
final String path;

_EditArtikelState({this.path});

final titleController = TextEditingController();
final subtitleController = TextEditingController();
final authorController = TextEditingController();
final textController = TextEditingController();

File imageFile;

List<DropdownMenuItem<dynamic>> dropdownMenuItemFromList() {
List<DropdownMenuItem<dynamic>> itemsList = [];
for (var i = 1; i < currentTags.length; i++) {
itemsList.add(new DropdownMenuItem(
child: Text(currentTags[i]),
value: currentTags[i],
));
}
return itemsList;
}

var _selectedValue = "";

@override
Widget build(BuildContext context) {
final isAdmin = Provider.of<bool>(context);
var pathElements = path.split('/');
final String artikelID = pathElements[3];
List<DropdownMenuItem<dynamic>> items = dropdownMenuItemFromList();

if (isAdmin == true) {
return LayoutBuilder(
builder: (context, constraint) {
return GlobalScaffold(
body: Container(
height: constraint.maxHeight,
child: SingleChildScrollView(
child: StreamBuilder<ArtikelData>(
stream:
DatabaseService(pathID: artikelID).artikelByArtikelID,
builder: (context, snapshot) {
if (snapshot.hasData) {
titleController.text == ""
? titleController.text = snapshot.data.title
: titleController.text;

subtitleController.text == ""
? subtitleController.text = snapshot.data.subtitle
: subtitleController.text;

authorController.text == ""
? authorController.text = snapshot.data.author
: authorController.text;

textController.text == ""
? textController.text = snapshot.data.text
: textController.text;

_selectedValue == ""
? _selectedValue =
currentTags.contains(snapshot.data.tags)
? snapshot.data.tags
: currentTags[1]
: _selectedValue = _selectedValue;

FirebaseStorageImage fbImage = new FirebaseStorageImage(
fileName: artikelID,
storageLocation: fbRefArtiklarImages,
);

return Container(
color: primaryColor,
padding: EdgeInsets.symmetric(
horizontal: 20, vertical: 15),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GradientHeading(
large: true,
text: "Redigera artikel",
),
SizedBox(height: 15),
CustomTextFormField(
labelText: "Rubrik",
controller: titleController,
),
CustomTextFormField(
labelText: "Underrubrik",
controller: subtitleController,
),
Padding(
padding: EdgeInsets.only(bottom: 7),
child: DropdownButtonFormField(
decoration: customInputDecoration("Tags"),
value: _selectedValue,
isDense: true,
onChanged: (value) {
setState(() {
_selectedValue = value;
});
},
items: items,
),
),
CustomTextFormField(
labelText: "Skriven av",
controller: authorController,
),
CustomTextFormField(
labelText: "Text",
multiline: true,
controller: textController,
),
NormalButton(
text: "Ladda upp ny bild",
outlined: true,
outlinedBgColor: primaryColor,
onPressed: () async {
FocusScope.of(context).unfocus();
imageFile = await ChooseImage()
.chooseImageFromGallery();
setState(() {});
},
),
ConditionalBuilder(
condition: imageFile == null,
ifTrue: NormalButton(
text: "Ta bort originalbild",
shouldOverideColor: true,
overriddenColor: redWarningColor,
onPressed: () async {
FocusScope.of(context).unfocus();
showDialog(
context: context,
barrierDismissible: true,
builder: (_) => AlertDialog(
content: Text(
"Vill du radera originalbilden?"),
actions: <Widget>[
FlatButton(
child: Text("Avbryt"),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text("Radera"),
onPressed: () async {
await StorageService()
.deleteArtikelImageToStorage(
artikelID);
setState(() {});
Navigator.of(context).pop();
},
),
],
),
);
},
),
),
ConditionalBuilder(
condition: imageFile != null,
ifTrue: NormalButton(
text: "Ta bort bild",
shouldOverideColor: true,
overriddenColor: redWarningColor,
onPressed: () {
FocusScope.of(context).unfocus();
imageFile = null;
setState(() {});
},
),
),
ConditionalBuilder(
condition: imageFile != null,
ifTrue: imageFile != null
? Image(
image: FileImage(imageFile),
)
: Container(),
ifFalse: fbImage,
),
SizedBox(height: 40),
],
),
NormalButton(
text: "Publisera ändringar",
onPressed: () async {
if (titleController.text != "" &&
subtitleController.text != "" &&
authorController.text != "" &&
textController.text != "") {
DatabaseService().editArtikel(
artikelID,
titleController.text,
subtitleController.text,
_selectedValue,
authorController.text,
textController.text,
imageFile,
);
Navigator.pop(context);
} else {
showDialog(
context: context,
barrierDismissible: true,
builder: (_) => AlertDialog(
content:
Text("Du måste fylla i alla fält"),
actions: <Widget>[
FlatButton(
child: Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
),
);
}
},
),
],
),
);
} else {
return LoadingWidget();
}
}),
),
),
);
},
);
} else {
return GlobalScaffold(
body: Center(
child: Text("Du har inte tillgång till den här sidan"),
),
);
}
}
}
这是错误日志:
enter image description here
enter image description here

最佳答案

从我可以看到的地方,您遇到了问题,因为您正在插入 StreamBuilderSingleChildScrollView它具有无限的高度可以增长,并且 StreamBuilder询问他 parent 的高度。
StreamBuilder需要一个具有固定大小的父级,以便他可以了解他有多少空间来渲染他的 child 。

你需要做的是把SingleChildScrollView里面Container 具有给定大小 (您可以将所有主体放在 LayoutBuilder 中,并使用 constrains.maxHeight 作为容器的高度,因此 SingleChildScrollView 知道其大小)。

像这样:(因为我没有你的小部件,我无法运行这段代码......所以也许缺少一些括号)

我希望这有帮助!

return GlobalScaffold(
body: LayoutBuilder(
builder: (ctx, constrains){
return GlobalScaffold(
body: Container(
height: constrains.maxHeight,
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
GradientHeading(text: "Guider", large: true),
ConditionalBuilder(
condition: isAdmin,
ifTrue: Column(
children: <Widget>[
NormalButton(
text: "Skapa ny guide",
onPressed: () {
Navigator.pushNamed(context, createNewArtikelRoute);
},
),
NormalButton(
text: "Lägg till ny kategori",
outlined: true,
onPressed: () {},
),
],
),
),
SizedBox(height: 10),
StreamBuilder<List<GuiderCategoriesData>>(
stream: DatabaseService().guiderCategoriesByPopularity,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
return GuiderCategories(
snapshot: snapshot,
numberOfCategories: snapshot.data.length,
);
} else if (!snapshot.hasData) {
return GuiderCategories(
hasNoCategories: true,
);
} else {
return LoadingWidget();
}
},
),
],
),
),
),
),
);
}
)
);

关于Flutter:在布局期间对象被赋予无限大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60058946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com