Почему передача BuildContext говорит, что в этом контексте нет скаффолда? - PullRequest
0 голосов
/ 27 июня 2018
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!'),
                  ));
            },
          )
        ],
      ));
}

В приведенном выше коде передача контекста фрагменту, отделенному от метода сборки, выдает ошибку, когда 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,),
            ],
          )),
    );
  }
}

Ответы [ 2 ]

0 голосов
/ 23 апреля 2019

Я обернул свой виджет, который вызывает функцию Scaffold.of(context), внутри StreamBuilder: enter image description here

0 голосов
/ 27 июня 2018

Переданный контекст не тот, который нужен Scaffold.of(context). Вам нужно обернуть его в Builder, так как Flutter BuildContext указывает:

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