【问题标题】:How can I remove the child from a stack in Flutter?如何从 Flutter 的堆栈中删除孩子?
【发布时间】:2019-09-12 17:36:15
【问题描述】:

我一直在 Flutter 中创建一个 WebView。我想在页面加载时将 CircularProgressIndicator 显示为覆盖。为此,我使用了一个堆栈,其中顶部子项是一个容器,其中 CircularProgressIndicator 位于中心,而 WebView 作为底部子项加载。现在我在 WebView 中看到一个名为 onPageFinished 的回调,我想用它来删除堆栈顶部的容器。我似乎没有找到从堆栈中删除孩子的方法。我该如何实施呢?

Stack(
  children: <Widget>[
    WebView(
      initialUrl: 'https://www.google.com',
      javascriptMode: JavascriptMode.unrestricted,
      onPageFinished: hideLoader,
    ),
    Container(
      decoration: BoxDecoration(
        color: Color.fromRGBO(0, 0, 0, 0.5),
      ),
      child: Center(
        child: CircularProgressIndicator(),
      ),
    )
  ],
)),

void hideLoader(String url) {}

【问题讨论】:

  • @SametÖZTOPRAK - 请停止将问题标题从“我如何”编辑为“如何”。这是不正确的。例如,请参阅Why do my "How to" questions often get renamed to "How do I"? 了解更多信息。
  • @WaiHaLee 这是你的观点我不搜索“我怎么能”这个问题,这很有趣,需要更笼统的问题,我也纠正拼写错误。
  • 您提出的一些修改建议错误地将标题更改为“如何”,例如herehere。至于语法,这不仅仅是我的意见 - 参见,例如Can “How to” be a question?.

标签: flutter


【解决方案1】:

试试下面的代码

    Stack(
      children: <Widget>[
        WebView(
          initialUrl: 'https://www.google.com',
          javascriptMode: JavascriptMode.unrestricted,
          onPageFinished: hideLoader,
        ),
        this._hideLoader == true
        ? Container(
            decoration: BoxDecoration(
              color: Color.fromRGBO(0, 0, 0, 0.5),
            ),
            child: Center(
              child: CircularProgressIndicator(),
            ),
          )
        : Container()
      ],
    )),
  bool _hideLoader = false;
  void hideLoader(String url) {
    setState(() {
      this._hideLoader = false;
    });
  }

【讨论】:

  • 当然!奇迹般有效。只是一个小小的修正。该标志应以bool _hideLoader = true; 而不是bool _hideLoader = false; 启动
猜你喜欢
  • 2021-05-08
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 2020-03-03
  • 2016-09-02
  • 1970-01-01
相关资源
最近更新 更多