在我的一个 Flutter 文件中,我有这段代码,理论上来说运行起来根本不应该是一个问题。它在调试版本中渲染得很好,但是当我构建发布的 iOS 和 Android 应用程序文件时,它变得部分不可见。有一个名为
_heyName()
的小部件,它只是一个文本小部件。 _whatWouldYouLike()
小部件几乎是 _heyName()
小部件的 1:1 副本,但这个小部件不会渲染,之后的任何小部件也不会渲染。
控制台确实会抛出此警告:
======== Exception caught by widgets library =======================================================
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
The ParentDataWidget Flexible(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.
Usually, this means that the Flexible widget has the wrong ancestor RenderObjectWidget. Typically, Flexible widgets are placed directly inside Flex widgets.
The offending Flexible is currently placed inside a RepaintBoundary widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
ConstrainedBox ← Container ← Flexible ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← SliverList ← ⋯
When the exception was thrown, this was the stack:
#0 RenderObjectElement._updateParentData.<anonymous closure> (package:flutter/src/widgets/framework.dart:5723:11)
#1 RenderObjectElement._updateParentData (package:flutter/src/widgets/framework.dart:5739:6)
#2 RenderObjectElement.attachRenderObject (package:flutter/src/widgets/framework.dart:5761:7)
#3 RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5440:5)
#4 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6082:11)
...
====================================================================================================
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
pinned: false,
expandedHeight: appBarHeight,
automaticallyImplyLeading: false,
floating: true,
flexibleSpace: LayoutBuilder(
builder: (context, constraints) {
return Container(
alignment: Alignment.bottomCenter,
child: Container(
height: SizeConfig.screenWidth * 0.13,
child: GestureDetector(
onTap: () {
},
child: _searchCategories(context))),
);
},
),
),
];
},
body: ListView(
shrinkWrap: true,
children: [
_heyName(),
SizedBox(
height: 10,
),
_whatWouldYouLike(),
SizedBox(
height: 10,
),
_requests(),
SizedBox(
height: 10,
),
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_categoriesText(),
_seeAll(),
],
),
SizedBox(
height: SizeConfig.screenWidth * 0.03,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_category("Food and Beverages", 1),
_category("Design", 2),
],
),
SizedBox(
height: SizeConfig.screenWidth * 0.1,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_category("Music", 3),
_category("Sports and Fitness", 4),
],
),
SizedBox(
height: SizeConfig.screenWidth * 0.1,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_category("Personal Grooming", 5),
_category("Information Technology", 6),
],
),
SizedBox(
height: SizeConfig.screenWidth * 0.1,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_category("Home Services", 7),
_category("Lifestyle", 8),
],
),
SizedBox(
height: SizeConfig.screenWidth * 0.1,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
_category("Pet Services", 9),
_category("Consulting", 10),
],
),
SizedBox(
height: SizeConfig.screenWidth * 0.25,
),
],
),
],
),
),
);
}
}
除了 ListView 之外,我还可以使用其他小部件吗?我觉得这就是问题的根源,因为当我使用 Column 而不是 ListView 时,它渲染得很好,只是因为它比屏幕大,所以它会抛出旧的 RenderFlex 错误。我尝试将列置于扩展状态,但没有成功。我觉得我在这里错过了一些东西。
这可能是由您的自定义小部件之一引起的,例如
_heyName()
或 _whatWouldYouLike()
。
分享这些小部件的代码,或者更好地查看您编写的代码,并确保它们不以 Flexible
小部件开头。
该错误是不言自明的,除了
Flexible
或 Row
之外,您还使用了 Column
小部件。因此,在您的 listView 中,子级以灵活开始,因此您会收到此错误。 Expanded 也是如此,例如,您不能将 Expanded 放入容器或列表视图中。
我遇到了这个错误,我将字体大小添加到标题中 ex 25.sp 并导入 flutter screen util 这就是工作