【问题标题】:Flutter - Incorrect use of ParentDataWidgetFlutter - ParentDataWidget 的错误使用
【发布时间】:2021-03-16 23:24:13
【问题描述】:

由于我引入了 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 ...

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

这是页面视图:

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],
        ),
      ),
    );
  }
}

【问题讨论】:

  • 在 VpFormPageScaffold 中会发生什么?我假设传递给构造函数的小部件用于错误类型的父小部件
  • @dumazy 谢谢我刚刚将它添加到问题的底部

标签: flutter dart flutter-layout


【解决方案1】:

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

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

请查看 Expanded Widget 的 Flutter 文档:

Expanded

扩展 Row、Column 或 Flex 的子项的小部件 以便孩子填满可用空间。

使用 Expanded 小部件可创建 Row、Column 或 Flex 的子级 展开以填充沿主轴的可用空间(例如, 水平的行或垂直的列)。如果多个 孩子们被扩大,可用空间在他们之间分配 根据弹性系数。

扩展小部件必须是 Row、Column 或 Flex 的后代,并且 从 Expanded 小部件到其封闭的 Row、Column 或 Flex 必须只包含 StatelessWidgets 或 StatefulWidgets(不能包含其他 各种小部件,例如 RenderObjectWidgets)。

【讨论】:

  • 谢谢,我已经更新了问题底部的错误。
  • 得到它的工作谢谢。当我应用您的解决方案时,需要删除 PageView 中的实际页面内的另一个 Expanded 以消除我的错误。
猜你喜欢
  • 2020-11-19
  • 2021-10-04
  • 2019-11-12
  • 2021-04-22
  • 2023-02-22
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 2021-01-20
相关资源
最近更新 更多