Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to left align the OutlineButton icon in Flutter

How to left align the OutlineButton icon in Flutter? Icon can be added as follows, but both icon and text are centered aligned in the button. Is there a way to align the icon to the left and text to the center?

return new OutlineButton.icon(
  onPressed: onPressed,
  label: new Text(title),
  icon: icon,
  highlightedBorderColor: Colors.orange,
  color: Colors.green,
  borderSide: new BorderSide(color: Colors.green),
  shape: new RoundedRectangleBorder(
      borderRadius: new BorderRadius.circular(5.0)));
like image 763
Chatura Dilan Avatar asked Oct 04 '18 00:10

Chatura Dilan


People also ask

How do you align widgets to the left in Flutter?

The Align widget positions the FlutterLogo such that the two points are on top of each other. In this example, the top left of the FlutterLogo will be placed at (24.0, 72.0) - (12.0, 36.0) = (12.0, 36.0) from the top left of the Align widget.

How do you align FlatButton in Flutter?

FlatButton( color: Colors. black, child: Align( alignment: Alignment. centerRight, child: Text( "M", style: TextStyle( color: mColor, fontSize: 10.0), ), ), That code produces this. But changing to Alignment.

How do I make text go left in Flutter?

textDirection: textDirection is used to specify the direction of the text inside a Text Widget say ltr (left-to-right) or rtl (right to left). ltr is the default text direction.


2 Answers

There are many ways you can do it, but it's not possible using the factory constructor that you mentioned OutlineButton.icon, you can go deeper and check the source code to see how it build the widget.

You can create your own Widget to put an icon to the left and the text centered.

Also you can use the OutlineButton widget and pass a Stack/Row as a child, check this sample

OutlineButton(
    onPressed: () => null,
    child: Stack(
        children: <Widget>[
            Align(
                alignment: Alignment.centerLeft,
                child: Icon(Icons.access_alarm)
            ),
            Align(
                alignment: Alignment.center,
                child: Text(
                    "Testing",
                    textAlign: TextAlign.center,
                )
            )
        ],
    ),
    highlightedBorderColor: Colors.orange,
    color: Colors.green,
    borderSide: new BorderSide(color: Colors.green),
    shape: new RoundedRectangleBorder(
        borderRadius: new BorderRadius.circular(5.0)
    )
)
like image 148
diegoveloper Avatar answered Oct 10 '22 19:10

diegoveloper


There are common approaches you can try, I have implemented this :

   OutlineButton(
        onPressed: () => null,
        child: Stack(
          alignment: Alignment.centerLeft,
          children: <Widget>[
            Icon(Icons.save_alt_rounded),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('Title'),
              ],
            ),
          ],
        ),
        highlightedBorderColor: Colors.orange,
        color: Colors.green,
        borderSide: new BorderSide(color: Colors.green),
        shape: new RoundedRectangleBorder(
            borderRadius: new BorderRadius.circular(5.0)
        )
    );

enter image description here

like image 21
Paresh Mangukiya Avatar answered Oct 10 '22 20:10

Paresh Mangukiya