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

在上面的代碼中,將上下文傳遞給與build方法分離的片段,當Scaffold.of的調用方式為:

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

但是,當我們使用Builder窗口小部件並將_takeURL作為生成器傳遞給Builder時,此問題得到解決。 在那之后,小吃店工作得非常好。

可能是什么原因? 我需要閱讀什么才能理解為什么?

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)所需要的。 您需要按照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!'),
                    ));
            },
            )
        ],
        ));
});
}

我在StreamBuilderStreamBuilder Scaffold.of(context)函數的小部件: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM