flutter – 在浮动动作按钮上的onPressed回调中显示scaffold中的snackbar

flutter – 在浮动动作按钮上的onPressed回调中显示scaffold中的snackbar,第1张

概述我想打电话 Scaffold.of(context).showSnackBar(SnackBar( content: Text("Snack text"),)); 在onPressed of floatingActionButton of scaffold. 我收到这个错误 I/flutter (18613): Scaffold.of() called with a context that 我想打电话

Scaffold.of(context).showSnackbar(Snackbar(  content: Text("Snack text"),));

在onpressed of floatingActionbutton of scaffold.

我收到这个错误

I/Flutter (18613): Scaffold.of() called with a context that does not contain a Scaffold.I/Flutter (18613): No Scaffold ancestor Could be found starting from the context that was passed to ....

当你在一个体内调用Scaffold.of(context)时,它指向一个解决方案.

https://docs.flutter.io/flutter/material/Scaffold/of.html

但是如果你在onpressed of floatingActionbutton中调用它,那么同样的解决方案也无法工作

解决方法 您应该将floatingActionbutton小部件放在Builder小部件中.
以下代码应该有效:

@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;                  });                }              },)          ],),);
总结

以上是内存溢出为你收集整理的flutter – 在浮动动作按钮上的onPressed回调显示scaffold中的snackbar全部内容,希望文章能够帮你解决flutter – 在浮动动作按钮上的onPressed回调中显示scaffold中的snackbar所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1000501.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存