Flutter是否总是在调用new之后创建新的小部件?

问题描述 投票:0回答:1

我有一个使用WebView的简单的有状态应用程序,第一次调用新的WebView时,调用onWebViewCreated,但是第二次调用“新WebView”时,将不再调用onWebViewCreated,每次都调用onPageFinished。似乎Flutter不会在build方法中创建新的WebView,而只是重新使用现有的WebView,对吗?示例代码如下:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() => runApp(MaterialApp(home: Sample()));

class Sample extends StatefulWidget {
  @override
  _SampleState createState() => _SampleState();
}
class _SampleState extends State<Sample> {
  var url = 'https://en.wikipedia.org/wiki/Kraken';
  Completer<WebViewController> _controller = Completer<WebViewController>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: webPage(),
      bottomNavigationBar: BottomAppBar(
        child: new Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                if (_controller.isCompleted) {
                  setState(() {
                    url = 'https://en.wikipedia.org/wiki/Sea_monster';

                    _controller.future.then((ctrl) {
                      ctrl.loadUrl(url);
                    });
                  });
                }
              },
              child: Text('load new url'),
            ),
          ],
        ),
      ),
    );
  }

  Widget webPage() {
    print('building new Webview?');
    return new WebView(

      initialUrl: url,

      javascriptMode: JavascriptMode.unrestricted,
      // debuggingEnabled: true,
      onWebViewCreated: (WebViewController webViewController) {
        print('webview created');
        _controller.complete(webViewController);
      },
      onPageFinished: (finish) {

        print('on page finished');
        print(finish);

      },
    );
  }
}
flutter android-webview webview-flutter
1个回答
0
投票

是的,是的。这已被回答,

这里:using the new keyword in flutter

这里:Do you need to use the "new" keyword in Dart?

© www.soinside.com 2019 - 2024. All rights reserved.