How to add icon to AppBar in Flutter
Asked Answered
L

2

56

If I have an AppBar like this:

How do I add a clickable icon to it like this?

Lipid answered 15/9, 2019 at 4:45 Comment(3)
Possible duplicate of How can I have clickable text in the AppBar in FlutterJunji
Doesn't matter. Same principle. You can wrap Icon in a flat button or InkWell and have the same effect. I thought there was a lack of trying to find resources and experimenting when there were similar resources regarding making actions clickable in Appbar.Junji
actions is list of widgets - doesn't matter what widget you put into - icon or textCastle
L
172

You can add an icon to the AppBar by adding an IconButton widget to the actions list of the AppBar.

AppBar(
  title: Text('My App'),
  actions: <Widget>[
    IconButton(
      icon: Icon(
        Icons.settings,
        color: Colors.white,
      ),
      onPressed: () {
        // do something
      },
    )
  ],
),

See also

Lipid answered 15/9, 2019 at 4:45 Comment(2)
what if you want the icon to be on the left side?Intoxicative
@AntoninGAVREL, You can use the leading parameter of AppBar to add a widget (including an icon) to the left side.Lipid
D
49

enter image description here

Use leading for left sided icon and actions for right sided.

AppBar(
  centerTitle: true,
  title: Text('AppBar'),
  leading: IconButton(
    onPressed: () {},
    icon: Icon(Icons.home),
  ),
  actions: [
    IconButton(
      onPressed: () {},
      icon: Icon(Icons.call),
    ),
    IconButton(
      onPressed: () {},
      icon: Icon(Icons.more_vert),
    ),
  ],
)
Divertimento answered 9/7, 2021 at 15:8 Comment(2)
Leading icon is not visible.Rockett
@BoraKeçeci Please share a minimal reproducible code via Dartpad.Divertimento

© 2022 - 2024 — McMap. All rights reserved.