Flutter ListView Widget 中使用 Stack Widget 可行吗?

4
我在我的应用程序中使用了Stack小部件,它运行良好,但现在我在将此Stack小部件包装在ListView小部件内时遇到问题。
我收到错误提示。
RenderBox was not laid out: RenderPointerListener#20d10 relayoutBoundary=up7 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1979 pos 12: 'hasSize'

我的Stack Widget是

Widget? stackW() {
    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        Positioned(
          top: 70,
          width: MediaQuery.of(context).size.width * .9,
          height: 150,
          child: Center(
            child: Container(
              width: MediaQuery.of(context).size.width * .9,
              decoration: BoxDecoration(
                  color: Colors.grey[200],
                  borderRadius: BorderRadius.circular(15)),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text(
                    "Product Designer",
                    style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                 
                ],
              ),
            ),
          ),
        ),
        Positioned(
          top: 30,
          left: MediaQuery.of(context).size.width / 2.5,
          width: 80,
          height: 80,
          child: CircleAvatar(
            backgroundColor: Colors.white,
            child: CircleAvatar(
              backgroundColor: Colors.green[100],
              radius: 35,
              child: const FaIcon(
                FontAwesomeIcons.apple,
                size: 30,
                color: Colors.black,
              ),
            ),
          ),
        ),
      ],
    );
  }

当我直接将stackWidget传递到主体中时,它能够正常工作,但是在包装在ListView中后,就会出现问题。所以,请指导我如何使用Stack Widget来实现我的ListView数据。
 body: stackW()!,  //working

body: ListView(
        scrollDirection: Axis.vertical,
        children: [
          stackW()!,
        ],
      ),
``` //not working

尝试了所有方法,但都没有解决。 - Pramod Yadav
1个回答

2

两个小部件都具有无限高度,您可以使用SizedBox小部件来包装stackW()

body: LayoutBuilder(
  builder: (context, constraints) => ListView(
    scrollDirection: Axis.vertical,
    children: [
      SizedBox(
        height: constraints.maxHeight,
        width: constraints.maxWidth,
        child: stackW()!,
      )
    ],
  ),
),

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接