没有任何问题,但我仍然收到错误消息

时间:2021-04-04 15:39:00

标签: flutter dart flutter-layout

我完全按照我想要的方式制作了一个页面。每次我转到该页面时,编译器都会以某种方式向我显示一条错误消息。似乎没有什么不对。一切正常。为什么我每次都收到这个错误?

我的代码是这样的;

Stack(
  children: [
    Image.asset(
      "assets/someImage.jpg",
      fit: BoxFit.cover,
    ),
    Scaffold(
      backgroundColor: Colors.transparent,
      body: ListView(
        children: [
          SlimyCard(), // It's a package from pub.dev
          Expanded(
            child: ListView.builder(
              physics: NeverScrollableScrollPhysics(),
              itemCount: 20,
              itemBuilder: (context, index) {
                return ConstrainedBox(
                  constraints: BoxConstraints(
                    minWidth: MediaQuery.of(context).size.width,
                    minHeight: 50,
                    maxHeight: 500,
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        child: Row(
                          children: [
                            Text("Some text"),
                            Text("Some text."),
                            Text("Some text.."),
                          ],
                        ),
                      ),
                      Container(
                        child: Text("Some text..."),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        ],
      ),
    ),
  ],
);

我尝试删除 ListView 上的 Expanded 并添加其他地方,但一切都在崩溃。

编译器说;

Exception has occurred.
FlutterError (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:
  _ScrollSemantics-[GlobalKey#25b39] ← Scrollable ← ListView ← Expanded ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← ⋯)

2 个答案:

答案 0 :(得分:1)

我试图理解为什么需要 Expanded,因为 SlimeyCard 将只需要它需要的量,而不是更多。您最初为什么使用 Expanded 小部件?

下面的代码对我有用,试试看,如果你得到相同的结果

 return Stack(
  children: [
    Container(height: 1000,width: double.infinity, color: Colors.white,),// instead of the background image
    // Image.asset(
    //   "assets/someImage.jpg",
    //   fit: BoxFit.cover,
    // ),
    Scaffold(
      backgroundColor: Colors.transparent,

      body: ListView(
        children: [
          SlimyCard(bottomCardHeight: 500,),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: 20,
            itemBuilder: (context, index) {
              return ConstrainedBox(
                constraints: BoxConstraints(
                  minWidth: MediaQuery.of(context).size.width,
                  minHeight: 50,
                  maxHeight: 500,
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      child: Row(
                        children: [
                          Text("Some text"),
                          Text("Some text."),
                          Text("Some text.."),
                        ],
                      ),
                    ),
                    Container(
                      child: Text("Some text..."),
                    ),
                  ],
                ),
              );
            },
          ),

        ],
      ),
    ),
  ],
);

答案 1 :(得分:0)

我不知道您想要的 UI 是什么,但我对您的代码进行了一些更改,并且它可以正常工作且没有错误。如果这不是您想要的,请分享您想要的 UI 的屏幕截图。

Stack(
  children: [
    Image.asset('assets/someImage.jpg', fit: BoxFit.cover),
    Scaffold(
      backgroundColor: Colors.transparent,
      body: Column(
        children: [
          SizedBox(height: 130, child: Placeholder()), // Replace this with your card.
          Expanded(
            child: ListView.builder(
              physics: NeverScrollableScrollPhysics(),
              itemCount: 20,
              itemBuilder: (context, index) {
                return ConstrainedBox(
                  constraints: BoxConstraints(
                    minWidth: MediaQuery.of(context).size.width,
                    minHeight: 50,
                    maxHeight: 500,
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        child: Row(
                          children: [
                            Text('Some text'),
                            Text('Some text.'),
                            Text('Some text..'),
                          ],
                        ),
                      ),
                      Text('Some text...'),
                    ],
                  ),
                );
              },
            ),
          ),
        ],
      ),
    ),
  ],
)