简体   繁体   English

如何更改 Flutter 中的默认后退按钮图标?

[英]How to change default back button icon in Flutter?

How I will change the back button icon in flutter using the theme.我将如何使用主题更改 flutter 中的后退按钮图标。 So it can be reflected throughout the application.所以它可以体现在整个应用程序中。 I saw multiple options in the theme to change the size and color of the icon.我在主题中看到了多个选项来更改图标的大小和颜色。 But didn't see the change icon option.但是没有看到更改图标选项。

I'm not sure if this is the right way to do this.我不确定这是否是正确的方法。 I'm still learning and I don't know if this will work in all cases.我还在学习,我不知道这是否适用于所有情况。 I've done this using VS Code while my app is in development.我在开发应用程序时使用 VS Code 完成了此操作。

I looked up the backbutton references(Shift + Alt + F12) and found the actual button in flutter sdk. I changed it there in the back_button.dart file which can be found in your flutter installation folder: c:\flutter\packages\flutter\lib\src\material\back_button.dart我查找了后退按钮引用(Shift + Alt + F12)并在 flutter sdk 中找到了实际按钮。我在back_button.dart文件中更改了它,该文件可以在您的 flutter 安装文件夹中找到: c:\flutter\packages\flutter\lib\src\material\back_button.dart

There, edit the static IconData method of the button and choose any Icon you wanna use for any platform.在那里,编辑按钮的 static IconData 方法并选择您想要用于任何平台的任何图标。

  static IconData _getIconData(TargetPlatform platform) {
    switch (platform) {
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
      case TargetPlatform.linux:
      case TargetPlatform.windows:
      case TargetPlatform.iOS:
      case TargetPlatform.macOS:
        return Icons.arrow_back_ios;
    }
  }

In the above case Icons.arrow_back_ios will be returned in all platforms.在上述情况下,Icons.arrow_back_ios 将在所有平台中返回。 You can specify the return Icon for each platform.您可以为每个平台指定返回图标。

However, I haven't tested this in like a real world "production use" situation so I hope someone else here can clarify.但是,我还没有在现实世界的“生产使用”情况下对此进行测试,所以我希望这里的其他人可以澄清一下。

Example:例子:

Scaffold(
  appBar(
    leading: BackButton(), // icon depends on TargetPlattrom
  ),
),

Or you can create your own constant widget basis on IconButton with your own icon settings, than use it as value for leading property.或者,您可以使用自己的图标设置在IconButton上创建自己的常量小部件,而不是将其用作leading属性的值。 In addition, there is an AppBarTheme class, which can be used when you create instance of ThemeData .此外,还有一个AppBarTheme class,可以在创建ThemeData实例时使用。

https://api.flutter.dev/flutter/material/AppBarTheme-class.html https://api.flutter.dev/flutter/material/AppBarTheme-class.html

PS If you look at AppBar source code you see that regular widget CloseButton or BackButton are used. PS 如果您查看 AppBar 源代码,您会看到使用了常规小部件CloseButtonBackButton So there is no specific style for it.所以它没有特定的风格。 And you have 2 ways:你有两种方法:

  • Create your own global widget and use it in each Scaffold.创建您自己的全局小部件并在每个 Scaffold 中使用它。
  • Subclass Scaffold with your own leading property and use it.使用您自己的领先属性子类 Scaffold 并使用它。

This will do the job for you:这将为您完成工作:

IconButton(
  onPressed: (){
    Navigator.pop(context);
  },
  icon:Icon(Icons.arrow_back_ios), 
  //replace with our own icon data.
  )

For the future seekers like me.对于像我这样的未来寻求者。

appBar: AppBar(
              automaticallyImplyLeading: false,
              leading: Builder(
                builder: (BuildContext context) {
                  return IconButton(
                    icon: const Icon(Icons.alarm_on), // Put icon of your preference.
                    onPressed: () {
                      // your method (function), 
                    },
                  );
                },
              ),
              title: Text(widget.title),
              centerTitle: true,

The above code will change the default back icon to alarm on icon.上面的代码会将默认的后退图标更改为图标上的警报。 You can change it to any thing.你可以把它改成任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 Flutter 中的默认前缀图标主题? - How to change default preffix icon theme in Flutter? 如何更改 flutter 应用程序默认图标的背景? - How to change the background of the flutter app default icon? 如何将 Flutter 中的按钮图标更改为动画文本? - How to Change Button Icon to Animated Text in Flutter? 如何更改 flutter 中我的图标按钮的颜色? - How to change the color of my icon button in flutter? 如何在不修改 Flutter 中默认提供的行为的情况下更改 appbar 的抽屉/后退按钮图标的大小? - How can I change the size of drawer/back button icons of appbar without modifying the behavior provided by default in Flutter? 如何在Flutter中更改CupertinoNavigationBar中后退按钮的颜色 - How to change the color of the back button in a CupertinoNavigationBar in Flutter 如何更改 flutter 中 RateMyAppNoButton 上的后退按钮 - how to change back button on RateMyAppNoButton in flutter 如何正确更改后颤应用程序启动器图标颜色 - how to change the back flutter app launcher icon color properly 切换按钮 flutter 的更改图标 - Change Icon of toggle button flutter 如何更改“BottomNavigationBarItem”中的默认图标 flutter - How can I change this default Icon in "BottomNavigationBarItem" flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM