1

I want to reuse my SnackBar widget but I get this error:

The argument type 'ShowSnackBar' can't be assigned to the parameter 'SnackBar'

This is my SnackBarShow code

import 'package:flutter/material.dart';
class ShowSnackBar extends StatelessWidget{
  final dynamic state;

  const ShowSnackBar({this.state}) ;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

      return SnackBar(content: Text(state.message??'خطا رخ داد.',style: TextStyle(color: Colors.white),
      ),
        elevation: 4,
        backgroundColor: Colors.deepPurple,
        action: SnackBarAction(label: 'متوجه شدم', onPressed: (){
          print("ok");

        }),);
    }
  }

This is some part of my code which use flutter_bloc library and this ShowSnacBar class.I got this error on line 4

 BlocListener<AuthenticationBloc,AuthenticationState>  (listener: (context,state){
              if(state is AuthenticationError){
                  _scafoldKey.currentState.showSnackBar(
                    ShowSnackBar(state: state)
                  );
              }
            },

Thank you .

dev
  • 35
  • 6

4 Answers4

0

make sure the class ShowSnackBar implements SnackBar like this:

class BetterSnackBar extends StatelessWidget implements SnackBar {
  Widget build(BuildContext context) {
     // Tweak the build method for maximum customization
  }

  // rest of the class, you can just copy-paste it
  // from SnackBar, if you want to make sure you support
  // everything the original snack bar supports
}

more about that here

also, i will advise you to use some packages that give you more customizations and don't need a scaffold, like this package.

hewa jalal
  • 952
  • 11
  • 24
0

You can do something like this.

class CustomSnackAlert {
  static SnackBar showErrorSnackBar(String message) {
    return SnackBar(
      content: Text(message),
      backgroundColor: Colors.red[700],
      duration: Duration(seconds: 7),
    );
  }
  // ... another snackbar
}

And

ScaffoldMessenger.of(context)
 ..hideCurrentSnackBar()..showSnackBar(
 CustomSnackAlert.showErrorSnackBar(
 "Your error",
  ),
);
user8784065
  • 39
  • 2
  • 6
0

I did it waaaay more simple. Since is just A widget, you don't need all that stuff:

SnackBar CustomSnackBar(message) => SnackBar(
      backgroundColor: colorBeige,
      content: Text(
        message,
        style: TextStyle(color: colorBlack),
      ),
    );

void showSnackBar(BuildContext context, String message) {
  ScaffoldMessenger.of(context).showSnackBar(
    CustomSnackBar(message),
  );
}

Then import the file and use it as simples as:

showSnackBar(context, "hello");
Daniel Vilela
  • 587
  • 1
  • 4
  • 19
0

Was struggling with this as well and followed @hewa sudggestion. Instead of implements I used extends

class MyNotification extends SnackBar {
  final Widget content;
  const MyNotification({Key? key, required this.content}) : super(key: key, content: content);

  Widget build(BuildContext context) {
    return SnackBar(
      content: content,
      backgroundColor: Theme.of(context).backgroundColor,
      duration: const Duration(milliseconds: 800),
    );
  }

}

// ScaffoldMessenger.of(context).showSnackBar(MyNotification(content: Text('Hello World')));
fredtma
  • 1,007
  • 2
  • 17
  • 27