加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 14:49:08 所属栏目:百科 来源:网络整理
导读:我想打电话 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.
我想打电话

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读