gpt4 book ai didi

Flutter - ParentDataWidget 的不正确使用

转载 作者:行者123 更新时间:2023-12-05 03:46:54 26 4
gpt4 key购买 nike

自从我引入了 PageView 小部件后,我得到了这个错误:

════════ Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown while applying parent data.: Incorrect use of ParentDataWidget.

The ParentDataWidget Expanded(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 Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets. The offending Expanded is currently placed inside a RepaintBoundary widget.

The ownership chain for the RenderObject that received the incompatible parent data was: Padding ← Container ← AnimatedContainer-[LabeledGlobalKey<ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget>>#758ef] ← KeyboardAvoider ← Expanded ← VpFormContainer ← LoggedOutNickNamePage ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← ⋯ When the exception was thrown, this was the stack
#0 RenderObjectElement._updateParentData.<anonymous closure> package:flutter/…/widgets/framework.dart:5770
#1 RenderObjectElement._updateParentData package:flutter/…/widgets/framework.dart:5786
#2 RenderObjectElement.attachRenderObject package:flutter/…/widgets/framework.dart:5808
#3 RenderObjectElement.mount package:flutter/…/widgets/framework.dart:5501
#4 SingleChildRenderObjectElement.mount package:flutter/…/widgets/framework.dart:6117 ...

════════ Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown while applying parent data.: Incorrect use of ParentDataWidget.

The ParentDataWidget Expanded(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 Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets. The offending Expanded is currently placed inside a RepaintBoundary widget.

The ownership chain for the RenderObject that received the incompatible parent data was: Padding ← Container ← AnimatedContainer-[LabeledGlobalKey<ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget>>#758ef] ← KeyboardAvoider ← Expanded ← VpFormContainer ← LoggedOutNickNamePage ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← ⋯ When the exception was thrown, this was the stack
#0 RenderObjectElement._updateParentData.<anonymous closure> package:flutter/…/widgets/framework.dart:5770
#1 RenderObjectElement._updateParentData package:flutter/…/widgets/framework.dart:5786
#2 RenderObjectElement.attachRenderObject package:flutter/…/widgets/framework.dart:5808
#3 RenderObjectElement.mount package:flutter/…/widgets/framework.dart:5501
#4 SingleChildRenderObjectElement.mount package:flutter/…/widgets/framework.dart:6117 ...

过去几个小时我一直在尝试修复,但没有任何效果。有谁确切知道在哪里应用修复程序以及修复程序是什么?

这是页面 View :

class LoggedOutPageView extends StatelessWidget {
final _controller = PageController(
initialPage: 0,
);

@override
Widget build(BuildContext context) {
print('building loggedOutPageView');

final pageView = PageView(
physics: const NeverScrollableScrollPhysics(),
controller: _controller,
clipBehavior: Clip.none,
children: [
LoggedOutNickNamePage(_controller),
LoggedOutEmailPage(_controller),
LoggedOutPasswordPage(),
],
);

final _pageContent = Expanded(
child: Container(child: pageView, color: Colors.transparent), flex: 1);

final _pageIndicator = Container(
height: 50,
child: SmoothPageIndicator(
controller: _controller,
count: 3,
effect: const JumpingDotEffect(),
),
color: Colors.transparent);

return VpFormPageScaffold([
VpLogoHeader(
onPressed: () {
print(_controller.page);
if (_controller.page > 0) {
_controller.previousPage(
duration: const Duration(milliseconds: 800),
curve: Curves.easeInOutCubic);
} else {
Navigator.pop(context);
}
},
pop: false),
_pageContent,
_pageIndicator
]);
}
}

VpFormPageScaffold:

class VpFormPageScaffold extends StatelessWidget {
VpFormPageScaffold(this.children);

List<Widget> children;
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: ConstrainedBox(
constraints: BoxConstraints.tightFor(
height: MediaQuery.of(context).size.height),
child: VpGradientContainer(
beginColor: initialGradientColor,
endColor: endGradientColor,
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: children)))));
}
}

VpGradientContainer:

class VpGradientContainer extends StatelessWidget {
const VpGradientContainer({this.child, this.beginColor, this.endColor});

final Widget child;
final Color beginColor;
final Color endColor;

@override
Widget build(BuildContext context) {
return Container(
child: child,
height: double.infinity,
width: double.infinity,
padding: const EdgeInsets.all(40),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [beginColor, endColor],
),
),
);
}
}

最佳答案

展开的小部件必须放在 Flex 小部件内。您正在使用无法使用的扩展小部件。您必须从此处删除扩展小部件:

final _pageContent = Expanded(
child: Container(child: pageView, color: Colors.transparent), flex: 1);

请查看 Flutter 文档以了解 Expanded小部件:

Expanded

A widget that expands a child of a Row, Column, or Flexso that the child fills the available space.

Using an Expanded widget makes a child of a Row, Column, or Flexexpand to fill the available space along the main axis (e.g.,horizontally for a Row or vertically for a Column). If multiplechildren are expanded, the available space is divided among themaccording to the flex factor.

An Expanded widget must be a descendant of a Row, Column, or Flex, andthe path from the Expanded widget to its enclosing Row, Column, orFlex must contain only StatelessWidgets or StatefulWidgets (not otherkinds of widgets, like RenderObjectWidgets).

关于Flutter - ParentDataWidget 的不正确使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65151499/

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