代码之家  ›  专栏  ›  技术社区  ›  Pramod Yadav

ListView小部件中的颤振堆栈小部件?

  •  0
  • Pramod Yadav  · 技术社区  · 2 年前

    我正在我的应用程序中使用堆栈小部件,它工作得很好,但现在我在将这个堆栈小部件包装到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'
    

    我的堆栈小部件是

    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中之后,它就产生了问题。

    所以请指导我使用堆栈小部件实现listview数据。

     body: stackW()!,  //working
    
    body: ListView(
            scrollDirection: Axis.vertical,
            children: [
              stackW()!,
            ],
          ),
    ``` //not working
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Md. Yeasin Sheikh    2 年前

    两个小部件的高度都是无限的,您可以包装 stackW() 使用SizedBox小部件。

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