[英]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 源代码,您会看到使用了常规小部件
CloseButton
或BackButton
。 So there is no specific style for it.所以它没有特定的风格。 And you have 2 ways:
你有两种方法:
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.