Flutter - Overlay card widget on a container

前端 未结 3 1175
刺人心
刺人心 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 17:49

    To do this,You Implement Positioned of the card using Stack widget in Flutter.

    Stack class is useful if you want to overlap several children in a simple way,

    Positioned widget that controls where a child of a Stack is positioned.

    Note: Stack paints its children in order with the first child being at the bottom.

    0 讨论(0)
  • 2020-12-28 17:55

    Here is running example with overlay:

    class _MyHomePageState extends State<MyHomePage> {
         double _width = 0.0;
         double _height = 0.0;
    
         @override
         Widget build(BuildContext context) {
          _width = MediaQuery.of(context).size.width;
          _height = MediaQuery.of(context).size.height;
          return Scaffold(
           backgroundColor: Colors.white,
           body: Stack(
            children: <Widget>[
              // The containers in the background and scrollable
             getScrollableBody(),
    
             // This container will work as Overlay
             getOverlayWidget()
            ],
          ),
       );
     }
    
     Widget getOverlayWidget() {
       return new Container(
         alignment: Alignment.bottomCenter,
         child: new Container(
          height: 100.0,
          width: _width,
          color: Colors.cyan.withOpacity(0.4),
         ),
       );
     }
     Widget getScrollableBody() {
      return SingleChildScrollView(
        child: new Column(
          children: <Widget>[
            new Container(
             height: _height * .65,
             color: Colors.yellow,
           ),
           new Container(
             height: _height * .65,
             color: Colors.brown,
           ),
           new Container(
            margin: EdgeInsets.only(bottom: 100.0),
            height: _height * .65,
            color: Colors.orange,
           ),
         ],
       ),
      );
     }
    }
    

    Here is Result of code: Scrollable Body under customised Bottom Bar

    0 讨论(0)
  • 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: <Widget>[
            // The containers in the background
            new Column(
              children: <Widget>[
                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!

    0 讨论(0)
提交回复
热议问题