xxxxxxxxxx
This exception happens because you are using the context of the widget that
instantiated Scaffold.
Not the context of a child of Scaffold.
You can solve this by just using a different context :
Scaffold(
appBar: AppBar(
title: Text('SnackBar Playground'),
),
body: Builder(
builder: (context) =>
Center(
child: RaisedButton(
color: Colors.pink,
textColor: Colors.white,
onPressed: () => _displaySnackBar(context),
child: Text('Display SnackBar'),
),
),
),
);
xxxxxxxxxx
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: Builder(
builder: (context) {
return RaisedButton(
child: Text('Show Snackbar'),
onPressed: () {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Hello!')),
);
},
);
},
),
),
);
}
}
void main() {
runApp(MyApp());
}