Does Flutter support negative margin?

前端 未结 8 737
你的背包
你的背包 2020-12-15 02:34

Negative margin is generally not needed but there are situations where it’s really useful. For example: why use negative margins?

For now, when I set margin for a co

8条回答
  •  一个人的身影
    2020-12-15 03:08

    No, Flutter does not allow negative margins but just in case you still want your widgets to overlap each other, you can use a Stack with Positioned which will allow you to generate the layout which you can do with negative margins.

    Here is an example :

    import 'package:flutter/material.dart';
    
    class MyHomePage extends StatefulWidget {
      MyHomePageState createState() => new MyHomePageState();
    }
    
    class MyHomePageState extends State  {
    
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            body: new Center(
              child: new Container(
                padding: const EdgeInsets.all(8.0),
                height: 500.0,
                width: 500.0,
                child: new Stack(
                  overflow: Overflow.visible,
                  children: [
                    new Icon(Icons.pages, size: 36.0, color: Colors.red),
                    new Positioned(
                      left: 20.0,
                      child: new Icon(Icons.pages, size: 36.0, color: Colors.green),
                    ),
    
                  ],
                ),
              ),
        )
        );
      }
    }
    
    void main() {
      runApp(new MaterialApp(
        title: 'Flutter Demo',
        theme: new ThemeData(
          primarySwatch: Colors.deepPurple,
        ),
        home: new MyHomePage(),
      ));
    }
    

    This will result in :

    NOTE: You can also give negative values in Positioned Widget.

提交回复
热议问题