本文介绍了为什么传递BuildContext表示该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!'),
                  ));
            },
          )
        ],
      ));
}

在上面的代码中,将上下文传递给与build方法分离的片段,当调用Scaffold.of时出现错误:

In the code above, passing the context to a fragment separated from the build method, gives error when Scaffold.of is called as:

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

但是,当我们使用Builder窗口小部件并将_takeURL作为生成器传递给Builder时,此问题得到解决。小吃店在那之后工作得很好。

However, this gets solved when we use a Builder widget and pass the _takeURL as builder to the 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,),
            ],
          )),
    );
  }
}


推荐答案

传递的上下文不是 Scaffold.of(context)所需的上下文。您需要按照指定的方式将其包装在Builder中:

The context passed on is not the one needed by Scaffold.of(context). You need to wrap it in a Builder as Flutter BuildContext specifies:

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

这篇关于为什么传递BuildContext表示该Context中没有脚手架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 12:26