Flutter snackbar alternative or easier method than wrapping everything in Scaffold? Flutter snackbar alternative or easier method than wrapping everything in Scaffold? flutter flutter

Flutter snackbar alternative or easier method than wrapping everything in Scaffold?


You can use GlobalKey to make it work the way you want it.

enter image description here

Since I don't have access to your database stuff, this is how I gave you an idea to do it. Copy and paste this code in your class and make changes accordingly. I also believe there is something wrong in your RowRule class, can you just copy the full code I have given you and run?

void main() => runApp(MaterialApp(home: HomePage()));class HomePage extends StatelessWidget {  final GlobalKey<ScaffoldState> _key = GlobalKey();  @override  Widget build(BuildContext context) {    return Scaffold(      backgroundColor: Color(0xFFFFFFFF).withOpacity(0.9),      key: _key,      body: Column(        children: <Widget>[          Container(            color: Color.fromRGBO(52, 56, 245, 1),            height: 150,            alignment: Alignment.center,            child: Container(width: 56, padding: EdgeInsets.only(top: 12), decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.yellow)),          ),          Expanded(            child: ListView.builder(              padding: EdgeInsets.zero,              itemCount: 120,              itemBuilder: (context, index) {                return Container(                  color: Colors.white,                  margin: const EdgeInsets.all(4),                  child: ListTile(                    title: Text("Row #$index"),                    onLongPress: () => _key.currentState                      ..removeCurrentSnackBar()                      ..showSnackBar(SnackBar(content: Text("Copied \"Row #$index\""))),                  ),                );              },            ),          ),        ],      ),    );  }}


These is a simple plugin replacement for the Snackbar named "Flushbar". You can get the plugin here - https://pub.dartlang.org/packages/flushbar You don't have to take care of any wrapping of widgets into scaffold also you get a lot of modifications for you like background gradient, adding forms and so on into Snackbar's and all. Inside your onLongPressed in GestureDetectore you can do this.

onLongPressed:(){Clipboard.setData(new ClipboardData(text: ruleGroup['label'] + " "  + ruleGroup['details']));Flushbar(             message:  "Copied !!",             duration:  Duration(seconds: 3),                        )..show(context);}

This will display the snackbar in you app where you would want to see it also you can get a lot of modification available to you so the you can make it look as per your app.


There are couple of things you need to do, like use onPressed property of the FlatButton it is mandatory to allow clicks, wrap your GestureDetector in a Scaffold. I have further modified the code so that it uses GlobalKey to make things easy for you.

Here is the final code (Your way)

class RowRule extends StatelessWidget {  final GlobalKey<ScaffoldState> globalKey = GlobalKey();  final DocumentSnapshot ruleGroup;  RowRule(this.ruleGroup);  _buildChildren() {    var builder = <Widget>[];    if (!ruleGroup['label'].isEmpty) {      builder.add(new Text(ruleGroup['label'], style: MyTheme.TextStyles.articleContentLabelTextStyle));    }    if (!ruleGroup['details'].isEmpty) {      builder.add(new Text(ruleGroup['details'], style: MyTheme.TextStyles.articleContentTextStyle));    }    return builder;  }  @override  Widget build(BuildContext context) {    return Scaffold(      key: globalKey,      body: GestureDetector(        onLongPress: () {          Clipboard.setData(new ClipboardData(text: ruleGroup['label'] + " " + ruleGroup['details']));          globalKey.currentState            ..removeCurrentSnackBar()            ..showSnackBar(SnackBar(content: Text('text copied')));        },        child: Container(          margin: const EdgeInsets.symmetric(vertical: 3.0),          child: new FlatButton(            onPressed: () => print("Handle button press here"),            color: Colors.white,            padding: EdgeInsets.symmetric(horizontal: 0.0),            child: new Stack(              children: <Widget>[                new Container(                  margin: const EdgeInsets.symmetric(vertical: MyTheme.Dimens.ruleGroupListRowMarginVertical),                  child: new Container(                    child: Padding(                      padding: EdgeInsets.symmetric(horizontal: 32.0, vertical: 8.0),                      child: new Column(                        crossAxisAlignment: CrossAxisAlignment.stretch,                        children: _buildChildren(),                      ),                    ),                  ),                )              ],            ),          ),        ),      ),    );  }}