android - 如何在应用栏中添加 Assets 图像作为 Flutter 应用程序中的操作图标?

标签 android flutter flutter-layout flutter-dependencies flutter-animation

问题 我正在尝试在 flutter 屏幕的应用栏中添加注销图像图标。我创建了一个 Assets 文件夹并创建了目录 images/icons/并在其中放置了图标。 我在 pubspec.yaml 文件中提到了它们。 我尝试在应用栏中实现 Assets 图像,但它不起作用。

代码

pubspe.yaml


    name: mtrack_notifications
    description: Flutter application for MTrack Notifications 

    dependencies:
      flutter:
        sdk: flutter
      # The following adds the Cupertino Icons font to your application.
      # Use with the CupertinoIcons class for iOS style icons.
      cupertino_icons: ^0.1.2
      http: ^0.11.3+16
      shared_preferences: "^0.4.2"


    dev_dependencies:
      flutter_test:
        sdk: flutter


    # For information on the generic Dart part of this file, see the
    # following page: https://www.dartlang.org/tools/pub/pubspec

    # The following section is specific to Flutter.
    flutter:

      # The following line ensures that the Material Icons font is
      # included with your application, so that you can use the icons in
      # the material Icons class.
      uses-material-design: true

      # To add assets to your application, add an assets section, like this:
      # assets:
      #  - images/a_dot_burr.jpeg
      #  - images/a_dot_ham.jpeg
      assets:
      - assets/images/icons/like.png
      - assets/images/icons/logout.png
      # An image asset can refer to one or more resolution-specific "variants", see
      # https://flutter.io/assets-and-images/#resolution-aware.

      # For details regarding adding assets from package dependencies, see
      # https://flutter.io/assets-and-images/#from-packages

      # To add custom fonts to your application, add a fonts section here,
      # in this "flutter" section. Each entry in this list should have a
      # "family" key with the font family name, and a "fonts" key with a
      # list giving the asset and other descriptors for the font. For
      # example:
      # fonts:
      #   - family: Schyler
      #     fonts:
      #       - asset: fonts/Schyler-Regular.ttf
      #       - asset: fonts/Schyler-Italic.ttf
      #         style: italic
      #   - family: Trajan Pro
      #     fonts:
      #       - asset: fonts/TrajanPro.ttf
      #       - asset: fonts/TrajanPro_Bold.ttf
      #         weight: 700
      #
      # For details regarding fonts from package dependencies,
      # see https://flutter.io/custom-fonts/#from-packages

Click to see the IDE screenshot here

应用条码




    @override
      Widget build(BuildContext context) {
        //build a form widget using the form key we created above
        return new Scaffold(
          appBar: new AppBar(
            title: new Text(StringRef.appName),
            actions: [

          new Center(
          child:new Text(
            userName,
            textScaleFactor: 1.5,
            style: new TextStyle(
              fontSize: 12.0,
              color: Colors.white,
            ),
          )),
          new IconButton(
            icon: new Icon(Icons.close),
            tooltip: 'Closes application',
            onPressed: () => exit(0),
          ),

          new IconButton(
            icon: new Image.asset('images/icons/logout.png'),
            tooltip: 'Closes application',
            onPressed: () => exit(0),
          ),

            ],
          ),

最佳答案

问题出在您提供给 IconButton 的路径中。

应该是这样的

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

关于android - 如何在应用栏中添加 Assets 图像作为 Flutter 应用程序中的操作图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51187662/

相关文章:

flutter - 为什么此测试中的 Future 从未完成?

flutter - 如何为Flutter material app菜单按钮添加徽章?

android - Flutter - TextFormField 的异步验证器

flutter - 为什么在initState()之前调用有状态的小部件build()函数?

java - 如何使用带参数的 Android Apache HttpDelete 类

java - 安卓和Java : Reduce memory usage on a service loop

Flutter 自定义底部表单

flutter - 如何使用 GlobalMaterialLocalizations 中的本地化字符串?

android - 如何通过 Android 的 Gmail 客户端自动发送电子邮件

Android - 在 Android 版本 4.0.3 以上的 WebView 中突出显示文本