How to add asset image in app bar as an action icon in Flutter application? How to add asset image in app bar as an action icon in Flutter application? android android

How to add asset image in app bar as an action icon in Flutter application?


The problem is in your path you have supplied to your IconButton.

It should be like this.

Scaffold(  appBar: AppBar(    actions: [      IconButton(        icon: Image.asset('assets/images/icons/logout.png'),        onPressed: () => exit(0),      ),    ],  ),)


According to Flutter Documentation you need to include the full path like specified in pubspec.yaml of the asset in order to load it:

Image.asset('assets/images/icons/logout.png')


Here's an example code of AppBar in Flutter with an icon

Widget build(BuildContext context) {  return new Scaffold(    appBar: AppBar(      title: new Text('App Name'),      actions: [        // action button        IconButton(          icon: Image.asset('assets/images/icons/logout.png'),          onPressed: () { },        ),      ],    ),  );}

Don't forget to add asset for your mobile app logo in the pubspec.yaml like this, and add your image named logo.png inside the assets directory.

flutter:  assets:    - assets/images/icons/logout.png

Note: The assets subsection of the flutter section specifies files that should be included with the app. Each asset is identified by an explicit path (relative to the pubspec.yaml file) where the asset file is located. The order in which the assets are declared doesn’t matter. The actual directory name used (assets in first example or directory in the above example) doesn’t matter.

During a build, Flutter places assets into a special archive called the asset bundle that apps read from at runtime.