Flutter - Overlay card widget on a container

前端 未结 3 1172
刺人心
刺人心 2020-12-28 17:16

In flutter, is it possible to place a part of a card on another container? In CSS, we would set margin-top to a negative value or use translate property. In flutter as we ca

3条回答
  •  有刺的猬
    2020-12-28 18:04

    Yes, you can acheive it with a Stack widget. You can stack a card over the background and provide a top or bottom padding.

    A simple example would look like:

    class StackDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Stack(
          children: [
            // The containers in the background
            new Column(
              children: [
                new Container(
                  height: MediaQuery.of(context).size.height * .65,
                  color: Colors.blue,
                ),
                new Container(
                  height: MediaQuery.of(context).size.height * .35,
                  color: Colors.white,
                )
              ],
            ),
            // The card widget with top padding, 
            // incase if you wanted bottom padding to work, 
            // set the `alignment` of container to Alignment.bottomCenter
            new Container(
              alignment: Alignment.topCenter,
              padding: new EdgeInsets.only(
                  top: MediaQuery.of(context).size.height * .58,
                  right: 20.0,
                  left: 20.0),
              child: new Container(
                height: 200.0,
                width: MediaQuery.of(context).size.width,
                child: new Card(
                  color: Colors.white,
                  elevation: 4.0,
                ),
              ),
            )
          ],
        );
      }
    }
    

    The output of the above code would look something like:

    Hope this helps!

提交回复
热议问题