15

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)));
Chatura Dilan
  • 1,502
  • 1
  • 16
  • 29

3 Answers3

44

There are many ways you can do it, but it's not possible using the factory constructor that you mentioned OutlinedButton.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 OutlinedButton widget and pass a Stack/Row as a child, check this sample

    OutlinedButton(
        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)
        )
    )

diegoveloper
  • 93,875
  • 20
  • 236
  • 194
1

You can use OutlineButton.icon, and simply wrap your Text with in Center like this:

return new OutlineButton.icon(
  onPressed: onPressed,
  label: Center(child: new Text(title)),
  icon: icon,
)

If you want the text left-aligned, this also works:

return new OutlineButton.icon(
  onPressed: onPressed,
  label: Align(alignment: Alignment.centerLeft, child: new Text(title)),
  icon: icon,
)
Jim Flood
  • 8,144
  • 3
  • 36
  • 48
  • Clearly you did not read the original question. OP wants to align the icon, *not* the label text. – KhoPhi Feb 02 '23 at 10:50
  • @KhoPhi Clearly, I did read the original question: "How to left align the OutlineButton icon in Flutter?" OutlineButton.icon aligns the *icon*. You can align the text *separately*. – Jim Flood Mar 15 '23 at 17:30
0

I Wrap Text Widget with Expanded widget.

         OutlinedButton.icon(

                      icon: Icon(
                        Icons.lock,
                        color: MyAppTheme.accentColor,
                        size: 20,
                      ),
                      label: Expanded(
                        child: Text(
                          '      Login',
                          style: TextStyle(
                              color: MyAppTheme.primaryColor,
                              fontSize: 16),
                        ),
                      ),
                      style: OutlinedButton.styleFrom(
                          shape: RoundedRectangleBorder(
                              borderRadius: new BorderRadius.circular(5.0)),
                          side: BorderSide(color: MyAppTheme.primaryColor)
                      ),
                     
                    ),
Shan Mk
  • 1,219
  • 1
  • 9
  • 7
  • This throws the exception: `Expanded(flex: 1) (typically placed directly inside a Flex widget)` – KhoPhi Feb 02 '23 at 11:08