簡體   English   中英

Flutter WebView 中的進度指示器之后如何進行下一個活動?

[英]How to next activity after progress indicator in Flutter WebView?

我正在嘗試構建一個 webview 項目,我想在加載指示器后轉到下一頁,但它不起作用,有什么我錯過或搞砸的嗎? 我想在加載指示器后我加載的 webviewsite 會自動顯示。 我正在嘗試構建一個 webview 項目,我想在加載指示器后轉到下一頁,但它不起作用,有什么我錯過或搞砸的嗎? 我想在加載指示器后我加載的網站會自動顯示

這是我的代碼-

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:percent_indicator/circular_percent_indicator.dart';
import 'package:web2app/widgets/drawer.dart';
import 'package:webview_flutter/webview_flutter.dart';

const String flutterUrl = 'https://flutter.dev/';
const String wikiUrl = 'https://google.com/';

class X extends StatefulWidget {
  XState createState() => XState();
}

class XState extends State<X> {
  WebViewController _controller;

  bool isLoading = false;

  @override
  void initState() {
    setState(() {
      isLoading = true;
    });
    super.initState();
  }

  _back() async {
    if (await _controller.canGoBack()) {
      await _controller.goBack();
    }
  }

  _forward() async {
    if (await _controller.canGoForward()) {
      await _controller.goForward();
    }
  }

  _loadPage() async {
    var url = await _controller.currentUrl();
    _controller.loadUrl(
      url == flutterUrl ? wikiUrl : flutterUrl,
    );
  }

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.deepOrange,
          title: Text('Web2App'),
          actions: <Widget>[
            IconButton(icon: Icon(Icons.arrow_back_ios), onPressed: _back),
            IconButton(
                icon: Icon(Icons.arrow_forward_ios), onPressed: _forward),
            SizedBox(
              width: 10,
            ),
          ],
        ),
        drawer: Drawer(
          child: DrawerPage(),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _loadPage,
          child: Icon(Icons.refresh),
        ),
        body: Center(
          child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                isLoading
                    ? Container(
                        child: CircularPercentIndicator(
                          radius: 120.0,
                          lineWidth: 13.0,
                          animation: true,
                          animationDuration: 4500,
                          percent: 1,
                          footer: new Text(
                            "Loading...",
                            style: new TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 17.0),
                          ),
                          circularStrokeCap: CircularStrokeCap.round,
                          progressColor: Colors.purple,
                        ),
                      )
                    : WebView(
                        key: Key('webview'),
                        initialUrl: flutterUrl,
                        javascriptMode: JavascriptMode.unrestricted,
                        onWebViewCreated:
                            (WebViewController webViewController) {
                          _controller = webViewController;
                        },
                      )
              ]),
        ));
  }
}

看起來您已將isLoading設置為 true 而從未更改過它。

`void initState() {
    setState(() {
      isLoading = true;
    });
    super.initState();
  }`

但是你還沒有再次將isLoading設置為 false 來加載下一個屏幕

`isLoading ? Container()
//all your code for loading screen
//you could set the state of isLoading to false here
    : WebView()
    //All your WebView code`

您是說如果isLoading == true則繼續加載,但在小部件內沒有將其設置為 false 。

試試這個它會和你一起工作的


class WebViewScreen extends StatefulWidget {
  final String title;
  final String selectedUrl;

  WebViewScreen({@required this.title, @required this.selectedUrl});

  @override
  _WebViewScreenState createState() => _WebViewScreenState();
}

class _WebViewScreenState extends State<WebViewScreen> {
  Completer<WebViewController> _controller = Completer<WebViewController>();
  bool isLoading = true;

  num position = 1;

  final key = UniqueKey();

  doneLoading(String A) {
    setState(() {
      position = 0;
    });
  }

  startLoading(String A) {
    setState(() {
      position = 1;
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: IndexedStack(index: position, children: <Widget>[
          WebView(
            initialUrl: widget.selectedUrl,
            javascriptMode: JavascriptMode.unrestricted,
            key: key,
            onPageFinished: doneLoading,
            onPageStarted: startLoading,
            onWebViewCreated: (WebViewController webViewController) {
              _controller.complete(webViewController);
            },
          ),
          Container(
            color: Colors.white,
            child: Center(child: CustomAppProgressBar),
          ),
        ]));
  }
}

暫無
暫無

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

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