I am trying to call
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(\"Snack text\"),
));
inside onPressed
of fl
UPDATE: The second solution is better than this solution.
You should put the floatingActionButton widget in a Builder Widget. The following code should work:
@override
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: new Builder(builder: (BuildContext context) {
return new FloatingActionButton(onPressed: () {
Scaffold
.of(context)
.showSnackBar(new SnackBar(content: new Text('Hello!')));
});
}),
body: new Container(
padding: new EdgeInsets.all(32.0),
child: new Column(
children: <Widget>[
new MySwitch(
value: _switchValue,
onChanged: (bool value) {
if (value != _switchValue) {
setState(() {
_switchValue = value;
});
}
},
)
],
),
),
);
Add a Globalkey of the Scaffold state and use that to display snack bar as below,
GlobalKey<ScaffoldState> scaffoldState;
Scaffold {
key: scaffoldState,
....
scaffoldState.currentState.showSnackBar(new SnackBar(content: new Text('Hello!')));
This is even simpler. Tried it. Create the FloatingActionButton as a separate Stateless Widget. Call this Stateless Widget from the Scaffold.
class Abc extends StatelessWidget
{
Widget build(BuildContext context)
{
return Scaffold(
appBar:AppBar(title:Text("Long List View")),
body:SomeOtherWidget(),
floatingActionButton:MyFAB()
);
}
}
class MyFAB extends StatelessWidget
{
Widget build(BuildContext context)
{
return FloatingActionButton(
onPressed:(){
showSnackBarHandler(context);
},
child:Icon(Icons.add),
tooltip:"Press to Add More"
);
}
}
void showSnackBarHandler(BuildContext context){
var snackBar = SnackBar(
content:Text("Hello")
);
Scaffold.of(context).showSnackBar(snackBar);
}