如何在单击按钮时使用flutter_webview返回上一页?

时间:2019-08-15 20:41:20

标签: flutter dart webview

我想使用~构建一个不带webview_flutter的简单应用,并使用静态URL,并在屏幕底部使用一个简单的AppBar来导航到上一页,但我不知道该怎么做。

有人可以给我一些指导,以便我尝试使按钮执行应做的事情吗?

以下是带有按钮的应用图片:

我不知道该怎么做,但是我想学习,对那个= D感到遗憾

这是我的整个代码: =)

floatingActionButton

3 个答案:

答案 0 :(得分:4)

Future<bool> _willPopCallback() async {
    WebViewController webViewController = await _controller.future;
    bool canNavigate = await webViewController.canGoBack();
    if (canNavigate) {
      webViewController.goBack();
      return false;
    } else {
      return true;
    }
  }

答案 1 :(得分:2)

我用与先前答案不同的代码来完成此操作。我按照this教程进行操作,以使Web视图和浮动按钮正常工作。从那里开始,很容易为浮动按钮提供一个后退箭头图标,并在单击浮动按钮时使Webview返回上一页。

要使Web视图在单击浮动按钮时返回上一页(将其放入onpressed方法中):

 controller.data.goBack();

可以在以下代码行轻松更改浮动按钮的图标:

child: Icon(Icons.arrow_back)

所有不同的按钮都可以在这里找到:link

这是我所有的代码:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'App'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  final Completer<WebViewController> _controller = Completer<WebViewController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WebView(
        initialUrl: "https://google.com",
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
          _controller.complete(webViewController);
        },
      ),
      floatingActionButton: FutureBuilder<WebViewController>(
        future: _controller.future,
        builder: (BuildContext context, 
          AsyncSnapshot<WebViewController> controller) {
            if (controller.hasData) {
              return FloatingActionButton(
                child: Icon(Icons.arrow_back),
                onPressed: () {
                  controller.data.goBack();
                });
            }

            return Container();
          }
        ),
    );
  }
}

答案 2 :(得分:0)

我做到了! = D

对于很多人来说,这看起来很简单,但是对于以前从未接触过这种东西的人,我为自己的忍者Ctrl C + Ctrl V感到非常自豪。开个玩笑,我看着很多例子并尝试了这并奏效,如果有人有任何建议,我将不胜感激! =)

这就是我所做的:

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

void main () {
  runApp(MaterialApp(
    title: 'Something',
    home: AplicativoB2b(),
    debugShowCheckedModeBanner: false,
  ));
  SystemChrome.setEnabledSystemUIOverlays ([]);
}

class AplicativoB2b extends StatefulWidget {
  @override
  _AplicativoB2bState createState() => _AplicativoB2bState();
}

class _AplicativoB2bState extends State<AplicativoB2b> {

  Completer<WebViewController> _controller = Completer<WebViewController>();

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: WebView(
        initialUrl: 'https://google.com',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
           _controller.complete(webViewController);
        },
      ),
      floatingActionButton: NavigationControls(_controller.future), // <-- added this
      );
  }
}

还有我用于floatingActionButton的类。

class NavigationControls extends StatelessWidget {
  const NavigationControls(this._webViewControllerFuture)
      : assert(_webViewControllerFuture != null);

  final Future<WebViewController> _webViewControllerFuture;

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<WebViewController>(
      future: _webViewControllerFuture,
      builder:
          (BuildContext context, AsyncSnapshot<WebViewController> snapshot) {
        final bool webViewReady =
            snapshot.connectionState == ConnectionState.done;
        final WebViewController controller = snapshot.data;
        return FloatingActionButton.extended(
              onPressed: !webViewReady
                  ? null
                  : () => navigate(context, controller, goBack: true),
              icon: Icon(Icons.arrow_back),
              backgroundColor: Colors.black,
              label: Text("Voltar"),
        );
      },
    );
  }

  navigate(BuildContext context, WebViewController controller,
      {bool goBack: false}) async {
    bool canNavigate =
        goBack ? await controller.canGoBack() : await controller.canGoForward();
    if (canNavigate) {
      goBack ? controller.goBack() : controller.goForward();
    } else {
      Scaffold.of(context).showSnackBar(
        SnackBar(
            content: Text("Sem histórico anterior")),
      );
    }
  }
}

基本上就是全部代码。当您真正想要Flutter时,它很酷且易于倾斜。 谢谢大家!