简体   繁体   English

将默认图标主题设置为cupertino flutter appbar

[英]Set default icon theme to cupertino flutter appbar

The app that im building requires me to have an AppBar with a leading back button.我正在构建的应用程序要求我有一个带有前导后退按钮的 AppBar。 However I prefer the cupertino back button(iOS-style) for the leading icon instead of the default back button for android.但是,我更喜欢使用cupertino 后退按钮(iOS 风格)作为引导图标,而不是android 的默认后退按钮。 I am aware that I can manually change the leading button of each AppBar by using an iconButton but i was wondering if there is any easy way to do this like a theme.我知道我可以使用 iconButton 手动更改每个 AppBar 的前导按钮,但我想知道是否有任何简单的方法可以像主题一样做到这一点。 Any help appreciated.任何帮助表示赞赏。

Instead of using MaterialApp as your root widget you can use CupertinoApp to do the same, assuming that the above changing of the AppBar is needed for each screen in your app.假设您的应用程序中的每个屏幕都需要对 AppBar 进行上述更改,而不是使用MaterialApp作为您的根小部件,您可以使用CupertinoApp来执行相同的操作。 This will automatically set the icon as you require这将根据您的需要自动设置图标

Here is a simple example to help you这是一个简单的例子来帮助你

Root Widget or starting point of the app根小部件或应用程序的起点

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const CupertinoApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

Then using a CupertinoPageScaffold where you want the CupertinoNavigationBar (I mean your appbar with ios icons) with the chevron icon like ios然后使用CupertinoPageScaffold在你想要CupertinoNavigationBar (我的意思是你的 appbar 带有 ios 图标)和像 ios 一样的 V 形图标

Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        // Try removing opacity to observe the lack of a blur effect and of sliding content.
        automaticallyImplyLeading: true // This will decide if the leading icon comes in by default
        backgroundColor: CupertinoColors.systemGrey.withOpacity(0.5),
        middle: const Text('Sample Code'),
      ),
      child: Column(
        children: <Widget>[
          Container(height: 50, color: CupertinoColors.systemRed),
          Container(height: 50, color: CupertinoColors.systemGreen),
          Container(height: 50, color: CupertinoColors.systemBlue),
          Container(height: 50, color: CupertinoColors.systemYellow),
        ],
      ),
    );

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM