颤音 - 如何将Snackbar定位在系统导航栏上方

发布于 2025-01-22 22:36:27 字数 313 浏览 5 评论 0原文

我的应用程序中有一个透明的系统导航栏。

该应用程序使用GetX小吃栏来投掷错误和成功消息。我坚持的是,每当应用程序投掷小吃吧时,小吃吧都会出现并通过透明导航栏消失(我可以看到小吃栏在导航栏中移动)。有什么方法可以在导航栏上方出现小吃吧?如何将小吃栏放置在系统导航栏上方?

I have a transparent system navigation bar in my application.

enter image description here

Getx snack bar is used by the app to throw errors and success messages. What I'm stuck with is that anytime the app throws a snack bar, the snack bar appears and disappears through the transparent navigation bar (i can see the snack bar move through the navigation bar). Is there any way to have the snack bar appear above the navigation bar? How can I position the snack bar above the system navigation bar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半透明的墙 2025-01-29 22:36:27

您可能需要查看 snackbarbehavior 让一个人可以更改位置。如果您使用的是第三方库,则可能需要尝试将行为放入扩展方法中。在此所以问题确切的重复。

Snackbarbehavior枚举指出:

定义脚手架中应在哪里出现Snackbar,以及当脚手架还包括FloatingActionButton或BottomNavigationBar时,应如何调整其位置。

其他想法包括使用带有堆栈小部件的位置小部件来获得精确的控制。添加MediaQuery或其他分数小部件以允许多个屏幕尺寸。

You may want to look into SnackBarBehavior enum which allows one to change the position. If you are using a third party library then you may want to try putting the behavior into an extension method. Alternative ideas exist on this SO Question that may apply in your use case although not an exact duplicate.

The SnackbarBehavior enum states:

Defines where a SnackBar should appear within a Scaffold and how its location should be adjusted when the scaffold also includes a FloatingActionButton or a BottomNavigationBar.

Other ideas include using a position widget with a Stack widget to gain precise control. Add a MediaQuery or other fractional widgets to allow for multiple screen sizes.

紫瑟鸿黎 2025-01-29 22:36:27

我建议您使用Flutter团队开发的Snackbar小部件,而不是GetX Snackbar。它基本上出现在此处的导航栏上方。

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: ElevatedButton(
      child: const Text('Show Snackbar'),
      onPressed: () {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: const Text('Awesome Snackbar!'),
              action: SnackBarAction(
                label: 'Action',
                onPressed: () {
                  // Code to execute.
                },
              ),
            ),
          );
        },
      ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'Business',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            label: 'School',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }

I suggest you to use SnackBar Widget that developed by flutter team instead of Getx snackbar. It basically appears above of navigation bar here an basic example;

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: ElevatedButton(
      child: const Text('Show Snackbar'),
      onPressed: () {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: const Text('Awesome Snackbar!'),
              action: SnackBarAction(
                label: 'Action',
                onPressed: () {
                  // Code to execute.
                },
              ),
            ),
          );
        },
      ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'Business',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            label: 'School',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
小女人ら 2025-01-29 22:36:27
  static GetSnackBar SuccessSnackBar({String title = 'Success', String message}) {
    Get.log("[$title] $message");
    return GetSnackBar(
      titleText: Text(title.tr, style: Get.textTheme.headline6.merge(TextStyle(color: Get.theme.primaryColor))),
      messageText: Text(message, style: Get.textTheme.caption.merge(TextStyle(color: Get.theme.primaryColor))),
      snackPosition: SnackPosition.BOTTOM,
      margin: EdgeInsets.all(20),
      backgroundColor: Colors.green,
      icon: Icon(Icons.check_circle_outline, size: 32, color: Get.theme.primaryColor),
      padding: EdgeInsets.symmetric(horizontal: 20, vertical: 18),
      borderRadius: 8,
      dismissDirection: DismissDirection.horizontal,
      duration: Duration(seconds: 5),
    );
  }

在我的应用中使用这样的。工作正常。

  static GetSnackBar SuccessSnackBar({String title = 'Success', String message}) {
    Get.log("[$title] $message");
    return GetSnackBar(
      titleText: Text(title.tr, style: Get.textTheme.headline6.merge(TextStyle(color: Get.theme.primaryColor))),
      messageText: Text(message, style: Get.textTheme.caption.merge(TextStyle(color: Get.theme.primaryColor))),
      snackPosition: SnackPosition.BOTTOM,
      margin: EdgeInsets.all(20),
      backgroundColor: Colors.green,
      icon: Icon(Icons.check_circle_outline, size: 32, color: Get.theme.primaryColor),
      padding: EdgeInsets.symmetric(horizontal: 20, vertical: 18),
      borderRadius: 8,
      dismissDirection: DismissDirection.horizontal,
      duration: Duration(seconds: 5),
    );
  }

Used like this in my app. Worked fine.

说好的呢 2025-01-29 22:36:27
 Get.snackbar(
          "Bruh",
          "Your quantity have to at least one",
          colorText: Colors.black,
          backgroundColor: Palette.yellowColor,
        );

就我而言,这是我返回Show Shackbar以获取错误消息,Snackbar出现

在GetX Snackbar中Appbar下方的顶部,有一个小点心:属性

您可以使用它来显示您的

 Get.snackbar(
          "Bruh",
          "Your quantity have to at least one",
          colorText: Colors.black,
          backgroundColor: Palette.yellowColor,
        );

In my case, this is my return to show snackbar for error message, the snackbar appear on top below the appbar

In the getx snackbar there are a snackPosition: attribute

You can use that to display yours

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文