简体   繁体   English

为什么传递BuildContext表示该Context中没有Scaffold?

[英]Why does passing BuildContext say there is no Scaffold in that Context?

import 'package:flutter/material.dart';

main() {
  runApp(MaterialApp(
    home: HomeScreen(),
  ));
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Rite.ly'),
      ),
      body: new Container(
          decoration: new BoxDecoration(
              image: new DecorationImage(
            image: new AssetImage('assets/login_background.png'),
            fit: BoxFit.fill,
          )),
          child: new ListView(
            children: [
              new Padding(
                padding:
                    const EdgeInsets.only(top: 16.0, bottom: 8.0, left: 16.0),
                child: new Text('Link : ',
                    style: new TextStyle(color: Colors.white)),
              ),
              _takeUrl(context),
            ],
          )),
    );
  }
}

Widget _takeUrl(BuildContext context) {
  return new Container(
      margin: new EdgeInsets.symmetric(horizontal: 20.0),
      decoration: new BoxDecoration(
          color: Colors.white, borderRadius: new BorderRadius.circular(12.0)),
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          new Expanded(
            child: new Column(
              children: [
                new TextField(
                  decoration: new InputDecoration(
                      contentPadding: const EdgeInsets.all(16.0),
                      hintText: 'Link to be Shortened'),
                ),
              ],
            ),
          ),
          new IconButton(
            icon: new Icon(
              Icons.content_paste,
              color: Color.fromARGB(255, 191, 53, 146),
            ),
            onPressed: () {
              Scaffold.of(context).showSnackBar(new SnackBar(
                    content: new Text('Hello!'),
                  ));
            },
          )
        ],
      ));
}

In the code above, passing the context to a fragment separated from the build method, gives error when Scaffold.of is called as: 在上面的代码中,将上下文传递给与build方法分离的片段,当Scaffold.of的调用方式为:

I/flutter (20313): Another exception was thrown: Scaffold.of() called with a context that does not contain a Scaffold.

However, this gets solved when we use a Builder widget and pass the _takeURL as builder to the Builder. 但是,当我们使用Builder窗口小部件并将_takeURL作为生成器传递给Builder时,此问题得到解决。 The Snack Bar works perfectly fine after that. 在那之后,小吃店工作得非常好。

What might be responsible? 可能是什么原因? What do I need to read to understand why? 我需要阅读什么才能理解为什么?

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Rite.ly'),
      ),
      body: new Container(
          decoration: new BoxDecoration(
              image: new DecorationImage(
            image: new AssetImage('assets/login_background.png'),
            fit: BoxFit.fill,
          )),
          child: new ListView(
            children: [
              new Padding(
                padding:
                    const EdgeInsets.only(top: 16.0, bottom: 8.0, left: 16.0),
                child: new Text('Link : ',
                    style: new TextStyle(color: Colors.white)),
              ),
              Builder(builder: _takeUrl,),
            ],
          )),
    );
  }
}

The context passed on is not the one needed by Scaffold.of(context) . 传递的上下文不是Scaffold.of(context)所需要的。 You need to wrap it in a Builder as Flutter BuildContext specifies: 您需要按照Flutter BuildContext指定的方式将其包装在Builder中:

Widget _takeUrl(BuildContext context) {
return Builder(builder: (BuildContext context) {
    return new Container(
        margin: new EdgeInsets.symmetric(horizontal: 20.0),
        decoration: new BoxDecoration(
            color: Colors.white, borderRadius: new BorderRadius.circular(12.0)),
        child: new Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
            new Expanded(
            child: new Column(
                children: [
                new TextField(
                    decoration: new InputDecoration(
                        contentPadding: const EdgeInsets.all(16.0),
                        hintText: 'Link to be Shortened'),
                ),
                ],
            ),
            ),
            new IconButton(
            icon: new Icon(
                Icons.content_paste,
                color: Color.fromARGB(255, 191, 53, 146),
            ),
            onPressed: () {
                Scaffold.of(context).showSnackBar(new SnackBar(
                    content: new Text('Hello!'),
                    ));
            },
            )
        ],
        ));
});
}

I wrapped my widget which calls the Scaffold.of(context) function, inside a StreamBuilder : 我在StreamBuilderStreamBuilder Scaffold.of(context)函数的小部件: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不包含脚手架的上下文调用的 Scaffold.of() - Scaffold.of() called with a context that does not contain a Scaffold "Flutter中“Widget build(BuildContext context)”的“context”是什么意思?" - What does "context" of "Widget build(BuildContext context)" mean in Flutter? 为什么 BuildContext 没有挂载属性? - Why does BuildContext have no mounted property? 未处理的异常:使用不包含 Scaffold 的上下文调用 Scaffold.of() - Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold 在静态方法中传递 BuildContext 会导致 Flutter 中的内存泄漏吗? - Does passing BuildContext inside a static method causes memory leak in Flutter? Flutter 中的单元测试通过 BuildContext - Unit testing in Flutter passing BuildContext 将 BuildContext 传递给非构建器方法 - Passing BuildContext to non builder methods 如何防止传递BuildContext? - How to prevent passing down BuildContext? 如果传递给 build 方法的 BuildContext 是指父窗口小部件,那么传递给根窗口小部件的 build() 的上下文是指什么? - If BuildContext passed to build method refers to parent widget , then what does context passed to build() of root widget refer to? Flutter :此错误出现在使用不包含 Scaffold 的上下文调用的 Scaffold.of() 。 当我尝试展示小吃店时 - Flutter : this error appear Scaffold.of() called with a context that does not contain a Scaffold. when i try to show a snackbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM