Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Position Widgets

Tags:

flutter

dart

In Android we can position elements in a linear layout using LayoutGravity , I would like to know how to achieve the same in Flutter. I have widgets in a column and I would like some widgets to be position on the left, some on the right and some on the left. How can I achieve this? Here is my code:

return new Container(
      margin: const EdgeInsets.only(top: 16.0),
      color: Colors.white,
      child: new Column(
        children: <Widget>[
          //the following textfields must be positioned on the right, but they are on the center
          new Text("Hair cut",),
          new Text("Shampoo",),
          //the button is in the center and that is correct
          new RaisedButton(
            onPressed: ()=>{},
            color: Colors.purple,
            child: new Text("Book",style: new TextStyle(color: Colors.white),),
          )
        ],
      ),
    );

Setting crossAxisAlignment to CrossAxisAlignment.end moves everything to the right. I tried adding the textfields in their own Column widget and setting crossAxisAlignment to end but nothing happens.

like image 718
spongyboss Avatar asked May 08 '18 14:05

spongyboss


People also ask

What is stack widget in Flutter?

The stack is a widget in Flutter. It contains a list of widgets and places them on top of each other. And it places their children on top of each other like a stack of books. In other words, stack developers would overlap multiple widgets on one screen. As you can add different images or colors using containers in it.

How do you make a positioned widget responsive in Flutter?

check CustomMultiChildLayout - the docs say: "CustomMultiChildLayout is appropriate when there are complex relationships between the size and positioning of multiple widgets. To control the layout of a single child, CustomSingleChildLayout is more appropriate.


1 Answers

You can set crossAxisAlignment for a generic alignment. And then override it for a specific item by wrapping it in Align widget.

new Column(
  children: <Widget>[
    //the following textfields must be positioned on the right, but they are not the center
    new Align(
      alignment: Alignment.centerRight,
      child: new Text("Hair cut"),
    ),
    new Align(
      alignment: Alignment.centerRight,
      child: new Text("Shampoo"),
    ),
    //the button is in the center and that is correct
    new RaisedButton(
      onPressed: () => {},
      color: Colors.purple,
      child: new Text(
        "Book",
        style: new TextStyle(color: Colors.white),
      ),
    )
  ],
),

You may also want to wrap your Column into a IntrinsicWidth if you don't want your column to fill the width.

like image 52
Rémi Rousselet Avatar answered Sep 24 '22 00:09

Rémi Rousselet