19

I am aware that I can use IconButton in the actions of the AppBar in Flutter. But instead of the icon, I'd like the user to see the words 'Save' or 'Back' or 'Cancel' and click on them in the AppBar. How can I achieve this? Here is part of my code that shows the AppBar. Instead of the save Icon, I'd like to use 'Save'

return Scaffold(
    appBar: AppBar(
      leading: IconButton(icon: Icon(Icons.arrow_back),
      tooltip: "Cancel and Return to List",
      onPressed: () {
        Navigator.pop(context, true);
      },
    ),
      automaticallyImplyLeading: false,
      title: Text(title),
      actions: <Widget>[

        IconButton(
          icon: Icon(Icons.save),
          tooltip: "Save Todo and Retrun to List",
          onPressed: () {
            save();
          },
        )
      ],
    ),//AppBar
Roya
  • 357
  • 1
  • 2
  • 13
  • You can Use FlatButton.icon(onPressed: (){save();}, icon: Icon(Icons.save), label: 'Save'); Instead of Iconbutton. – anmol.majhail Oct 09 '18 at 06:21
  • Thank you so much! I needed a flat button actually. – Roya Oct 09 '18 at 06:59
  • But it didn't work for the arrow_back – Roya Oct 09 '18 at 07:06
  • to be exact: there is not enough space for word 'cancel' in the leading section instead of the arrow_back – Roya Oct 09 '18 at 07:14
  • For those who want an icon instead of text, see [How to add icon to AppBar in Flutter](https://stackoverflow.com/questions/57941227/how-to-add-icon-to-appbar-in-flutter) – Suragch Oct 13 '19 at 04:33

7 Answers7

40

You can use FlatButton within an AppBar's actions list:

appBar: AppBar(
  title: Text("Test Screen"),
  actions: <Widget>[
    FlatButton(
      textColor: Colors.white,
      onPressed: () {},
      child: Text("Save"),
      shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
    ),
  ],
),

onPressed must be defined or the button will appear disabled. Also note that by default the shape of the button will create a filled rectangle for the InkWell effect. By setting the shape property to CircleBorder, we get a nicer effect for the pressed state.

E.g.

Not pressed:

Pressed:

Sandy Chapman
  • 10,385
  • 3
  • 55
  • 63
6

Use TextButton:

appBar: AppBar(
  actions: <Widget>[
    TextButton(
      onPressed: () {},
      child: Text('Save'),
    ),
  ],
)
CopsOnRoad
  • 109,635
  • 30
  • 367
  • 257
3

If you have short string then you can pass Text widget in place of Icon into the IconButton.icon argument:

IconButton(
  icon: Text(
    "Save",
    style: Theme.of(context).textTheme.button.apply(
      color: Theme.of(context).appBarTheme.actionsIconTheme.color,
    ),
  ),
  onPressed: _save,
),

Unfortunately, it will not work for a longer text like Cancel.

sosite
  • 2,309
  • 1
  • 26
  • 35
1

It's the second time I come to this thread to find a solution. I actually had upvoted a few answers that I found interesting.

@sosite solution's is almost perfect and changing the iconSize allows to display longer texts. But, it's not perfect because the button's splash radius will be too large.

The best approach is using the constraints: BoxConstraints(width: ...). It will mimic the default IconButton's splash radius.

We can use bigger texts, multi-world texts and align the text to the center to be perfectly aligned.

IconButton(
            constraints: BoxConstraints.expand(width: 80),
            icon: Text('CREATE GAME', textAlign: TextAlign.center),
            onPressed: () {},
          ),

If the text is cut, increase the width value :)

enter image description here

Pedro Romão
  • 1,925
  • 23
  • 20
0

New version flutter2

TextButton( style: TextButton.styleFrom( primary: Colors.red, // foreground ), onPressed: () { }, child: Text('TextButton with custom foreground'), )

0

You can also achieve this by using a GestureDetector widget inside Center widget

   Center(
      child: GestureDetector(
        onTap: (){},
        child: Text(
          "Ssave",
          textScaleFactor: 1.5,
          style: TextStyle(
            fontSize: 12.0,
            color: Colors.white,
          ),
        ),
      ),
    )

enter image description here

Paresh Mangukiya
  • 14,668
  • 7
  • 90
  • 90
-1

Use new version Flutter 2.0

appBar: AppBar(
  title: Text("Test Screen"),
  actions: <Widget>[
    TextButton(
      textColor: Colors.white,
      onPressed: () {},
      child: Text("Save"),
      shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
    ),
  ],
),
Dharmesh Mansata
  • 2,406
  • 19
  • 30