简体   繁体   English

如何解决 flutter 中的一叠卡片布局问题

[英]how to fix a stack of cards layout issue in flutter

I'm trying to make all the cards look like they are stack together but I'm not sure why my code it's not working.我试图让所有的卡片看起来像是堆叠在一起,但我不确定为什么我的代码不起作用。 Right now all the cards are behind each other but I want to look like the design below.现在所有的卡片都在彼此后面,但我想要看起来像下面的设计。 I try to adjust behind card by increase the height but some reasons it's still not working.我尝试通过增加高度来调整卡后面,但由于某些原因它仍然无法正常工作。 Any suggestion will be really appreciated.任何建议将不胜感激。

    Widget _buildStackedCards(App app) {
    return Stack(
      key: Key(app.name + "Stack"),
      children: <Widget>[
      Container(
        height: 153,
        child: SingleChildScrollView(
          child: CardWidget(title: "Title 1", tileItems: brandListMock)
        ),
      ),
      Container(
        height: 150,
        child: SingleChildScrollView(
          child: CardWidget(title: "Title 2", tileItems: fleetDeliveriesListMock)
        ),
      ),
      Container(
        height: 180,
        child: SingleChildScrollView(
          child: CardWidget(title: "Title 3", tileItems: regionListMock)
        ),
      ),
      ],
    );
  }

I want my cards to stack together like this我希望我的卡片像这样堆叠在一起

在此处输入图像描述

You must use Positioned widget to position the widgets:)您必须使用Positioned小部件来 position 小部件:)

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var app = MaterialApp(
      debugShowCheckedModeBanner: true,
      home: Scaffold(
        backgroundColor: Colors.white60,
        body: SafeArea(
          child: Stack(
            children: <Widget>[
              getCard(4),
              getCard(3),
              getCard(2),
              getCard(1),
            ],
          ),
        ),
      ),
    );

    return app;
  }

  Widget getCard(int index) {
    return Positioned(
        top: 20.0 * index,
        left: 15,
        right: 15,
        child: Container(
          height: 153,
          child: SingleChildScrollView(
              child: Container(
            height: 100,
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius:
                    BorderRadius.circular(8),
                boxShadow: [
                  BoxShadow(
                    color: Color.fromRGBO(
                        0, 64, 101, 0.15),
                    spreadRadius: 1,
                    blurRadius: 8,
                    offset: Offset(0,
                        2), // changes position of shadow
                  ),
                ]),
            child: Center(child: Text("Cards")),
          )),
        ));
  }
}

结果

You should have the widget with the largest height first in the list of the Stack 's children.您应该首先在Stack的子项列表中拥有最大高度的小部件。

This is because这是因为

the stack paints its children in order with the first child being at the bottom.堆栈按顺序绘制其子项,第一个子项位于底部。 Source资源

So your code should be changed to:所以你的代码应该改为:

 children: <Widget>[
      Container(
        height: 180,
        child: SingleChildScrollView(
          child: CardWidget(title: "Title 3", tileItems: regionListMock)
        ),
      ),

      Container(
        height: 153,
        child: SingleChildScrollView(
          child: CardWidget(title: "Title 1", tileItems: brandListMock)
        ),
      ),
      Container(
        height: 150,
        child: SingleChildScrollView(
          child: CardWidget(title: "Title 2", tileItems: fleetDeliveriesListMock)
        ),
      ),
     ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM