在Flutter Web中展示Snackbar 我有以下我想用来显示Snackbar的代码,但它行不通,我不知道为什么: home-screen.dart是一切开始的地方,我定义了一个脚手架,我传递给另一个

问题描述 投票:0回答:1
anther-widget.dart

final helpers = Helpers(); start() async { try { if (energy == 0) { if (!mounted) return; helpers.showSnackWithKey(widget.scaffoldKey, "Not enough energy"); showRemaining = true; return; } return Card( shadowColor: Colors.blue, child: Column( children: [ ... ElevatedButton( onPressed: () { start(); }, ...

-helpers.dart
  showSnackWithKey(scaffoldKey, message) {
    print(message);
      ScaffoldMessenger.of(scaffoldKey.currentContext).showSnackBar(
        SnackBar(
          backgroundColor: Colors.blueGrey,
          behavior: SnackBarBehavior.floating,
          elevation: 3,
          content: Text(message),
        ),
      );
  }

suse null断言。而不是在助手中,使用
ScaffoldMessenger.of(scaffoldKey.currentContext)

ScaffoldMessenger.of(scaffoldKey.currentContext!)
flutter dart
1个回答
0
投票
GlobalKey.currentContext

ScaffoldMessenger.of
类型,因此没有IDE警告。
Edit:我遇到了一次无效的错误,但无法复制。我尝试运行提供代码,它可以显示为预期的消息

scaffoldKey


您可以在Flutter中使用另一_Flushbar包。该软件包有用,有创意且可自定义。我最近使用了它,我推荐给您。您可以使用此软件包而不是Snackbar。
dynamic
You can find the bellow link :
https://pub.dev/packages/another_flushbar

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.