简体   繁体   English

如何在没有脚手架的情况下显示snackBar

[英]How to show snackBar without Scaffold

I am trying to show snackbar in my app to notify User but without scaffold it shows me error.我试图在我的应用程序中显示小吃店以通知用户,但没有脚手架它会显示我的错误。 My current code is:我目前的代码是:

scaffoldKey.currentState?.showSnackBar(
    new SnackBar(
      backgroundColor: color ?? Colors.red,
      duration: Duration(milliseconds: milliseconds),
      content: Container(
        height: 50.0,
        child: Center(
          child: new Text(
            title,
            style: AppTheme.textStyle.lightText.copyWith(color: Colors.white),
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
    ),
  )

It can get the current context and show snackbar like this:它可以获取当前上下文并像这样显示snackbar:

void _showToast(BuildContext context) {
  final scaffold = Scaffold.of(context);
  scaffold.showSnackBar(
    SnackBar(
      content: const Text('Updating..'),
    ),
  );
}

this._showToast(context);

No, It is not possible.不,这是不可能的。 Snackbar is part of Scaffold. Snackbar 是 Scaffold 的一部分。 It must have a Scaffold parent.它必须有一个 Scaffold 父级。 Snackbar小吃店

Inside Scaffold parent, you can do like below在 Scaffold 父级中,您可以执行以下操作

BuildContext con=context;
final snackBar = SnackBar(content: Text(message));
Scaffold.of(con).showSnackBar(snackBar);

Use https://pub.dev/packages/get使用https://pub.dev/packages/get

then you can simply call the Get.snackbar() to show the snackbar where you want it to be displayed.然后你可以简单地调用 Get.snackbar() 来显示你想要显示的小吃店。

Get.snackbar('Hi', 'i am a modern snackbar');

NOTE笔记

You can use this package when you don't have the context too.当您没有上下文时,您也可以使用此包。

Scaffold.of(context).showSnackBar() is deprecated . Scaffold.of(context).showSnackBar()弃用 You should use ScaffoldMessenger now.您现在应该使用 ScaffoldMessenger。

Example: ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Clicked!")));示例: ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Clicked!")));

Here are listed the benefits of the new approach. 这里列出了新方法的好处。

If you have access to context , from laszlo's answer , just call如果您可以访问上下文,从laszlo 的回答中,只需致电

ScaffoldMessenger.of(context).showSnackBar(snackBar);

If you dont have context (eg: showing network request error), use this snippet based on answer from Miguel Ruvio如果您没有上下文(例如:显示网络请求错误),请根据Miguel Ruvio 的回答使用此代码段

We are going to use the property scaffoldMessengerKey which allows us to use the new ScaffoldMessenger to show snacbacks without needing context or GlobalKeys of scaffolds.我们将使用属性 scaffoldMessengerKey,它允许我们使用新的 ScaffoldMessenger 来显示 snacback,而无需上下文或脚手架的 GlobalKeys。 Also, no need to add 3rd party packages like Get.此外,无需添加像 Get 这样的 3rd 方包。


First, create a globals.dart to store a GlobalKey首先,创建一个 globals.dart 来存储一个 GlobalKey

//globals.dart

import 'package:flutter/material.dart';

final GlobalKey<ScaffoldMessengerState> snackbarKey =
    GlobalKey<ScaffoldMessengerState>();

Second, add scaffoldMessengerKey property to MaterialApp二、为MaterialApp添加scaffoldMessengerKey属性

//main.dart

import 'globals.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      scaffoldMessengerKey: snackbarKey, // <= this
      ...

Finally, refer to this Key anywhere to show a Snackbar最后,在任何地方引用这个 Key 来显示一个 Snackbar

// anywhere

import 'globals.dart';

final SnackBar snackBar = SnackBar(content: Text("your snackbar message"));
snackbarKey.currentState?.showSnackBar(snackBar); 

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

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