Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to center a circle into a card in flutter ?

I tried to create un geofencing layer in an image ( google map url)

I have a card with a child image , and I add a circle stack over the map image and I use a slider to change the size of the zone. the problem is that I don't succeded to center the map in my container. The only solution currently found is to use margin to go down the circle but when I change the margin limit the top of the circle and not the center, so my center is shifting...

here is my code example:

               new Card (

                child :new Stack(
                  children: <Widget>[

                new Container(

                  width: 200.0,
                  height: 200.0,
                ),

                    new Container(
                    alignment: new FractionalOffset(0.0, 0.0),   
                      decoration: new BoxDecoration(
                        border: new Border.all(
                          color: Colors.blue.withOpacity(0.5),
                          width: val,  // it's my slider variable, to change the size of the circle
                        ),
                        shape: BoxShape.circle,
                      ),
                    ),

                  ],
                ),
               ),
like image 682
Nitneuq Avatar asked Jul 01 '18 19:07

Nitneuq


People also ask

How do you make a circle card in flutter?

Border radius is used to make Circle, you can add border radius more than 50% of width or height in Card to make it a circle.

How do I center align an image in flutter?

To center align the widgets in the Row widget, you can use the mainAxisAlignment property and set it to MainAxisAlignment. center.


2 Answers

Either use alignment property of Stack like this

new Card(
        child: new Stack(
          alignment: AlignmentDirectional.center,
          children: <Widget>[
            new Container(
              width: 200.0,
              height: 200.0,
            ),
            new Container(
              alignment: new FractionalOffset(0.0, 0.0),
              decoration: new BoxDecoration(
                border: new Border.all(
                  color: Colors.blue.withOpacity(0.5),
                  width: 50.0,
                ),
                shape: BoxShape.circle,
              ),
            ),
          ],
        ),
      ),

or simply wrap your container widget with FractionalOffsetSet

 new Card(
            child: new Stack(
              alignment: AlignmentDirectional.center,
              children: <Widget>[
                new Container(
                  width: 200.0,
                  height: 200.0,
                ),
                FractionalTranslation(  
                translation: Offset(0.0, 0.5),
                child: new Container(
                     alignment: new FractionalOffset(0.0, 0.0),
                     decoration: new BoxDecoration(
                     border: new Border.all(
                     color: Colors.blue.withOpacity(0.5),
                     width: 50.0, 
                   ),
                shape: BoxShape.circle,
              ),
            ),
          ],
        ),
      ),
like image 98
goops17 Avatar answered Oct 18 '22 20:10

goops17


Wrap your Widget in FractionalTranslation and set the the offset Y value to 0.5

        FractionalTranslation(
          translation: Offset(0.0, 0.5),
          child: new Container(
            alignment: new FractionalOffset(0.0, 0.0),
            decoration: new BoxDecoration(
              border: new Border.all(
                color: Colors.blue.withOpacity(0.5),
                width: 50.0, // it's my slider variable, to change the size of the circle
              ),
              shape: BoxShape.circle,
            ),
          ),
        ),

enter image description here

like image 26
Raouf Rahiche Avatar answered Oct 18 '22 18:10

Raouf Rahiche