如何在屏幕更改上放置Flutter WebviewScaffold?

时间:2019-01-06 19:25:27

标签: webview flutter

我有一个主屏幕,该屏幕显示带有标题的标题图像,该按钮具有转到第二个屏幕的按钮,以及一个位于其下方容器中的webview(支架)。当我切换到第二个屏幕时,Webview仍然存在。 这个问题似乎与此(Flutter: to close a webview when you left the page)类似,但是我不知道如何实现dispose事件。有人可以帮忙吗? 我正在使用flutter_webview_plugin包。 这是我的代码:

import 'package:flutter/material.dart';

import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

import 'dart:async';

import './SecondScreen.dart';

String selectedUrl = 'https://flutter.io';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  final flutterWebViewPlugin = FlutterWebviewPlugin();

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Generated App',
      theme: new ThemeData(
        primarySwatch: Colors.purple,
        primaryColor: const Color(0xFF8e80a8),
        accentColor: const Color(0xFF8e80a8),
        canvasColor: const Color(0xFF8e80a8),
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {


  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Color(0xFF8e80a8)),
      child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(10, 30, 10, 5),
              child: new Image.asset(
                'lib/assets/newoldralogo1.png',
              ),
            ),
            Container(
              constraints: BoxConstraints.expand(
                height:
                    Theme.of(context).textTheme.display1.fontSize * 1.1 + 10.0,
              ),
              child: new Stack(fit: StackFit.expand,
                  children: <Widget>[
                Container(
                  decoration: BoxDecoration(color: Color(0xFF8e80a8)),
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
                    child: Image.asset(
                      "lib/assets/newoldralogo1.png",
                      alignment: Alignment.topCenter,
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                ),
                Container(
                  child: RaisedButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => SecondScreen()),
                      );
                    },
                    color: Color(0xFFFFFF),
                  ),
                ),
              ]),
            ),

            Container(
              constraints: BoxConstraints.expand(
                height:
                    Theme.of(context).textTheme.display1.fontSize * 1.1 + 300.0,
              ),
              padding: const EdgeInsets.fromLTRB(10, 15, 10, 5),
              color: Color(0xFF8e80a8),
              alignment: Alignment.center,
              child: Container(
                child: WebviewScaffold(
                  url:
                      ('https://flutter.io/'),
                ),
              ),
            )
          ]),
    );
  }
}

/*
@override
void dispose() {
  super.dispose();
  _MyHomePageState();

}
*/

2 个答案:

答案 0 :(得分:1)

一种解决方案是将“ Navigator.push”更改为“ Navigator.pushRemove”。这意味着第一个屏幕不会再出现,因此按下“后退”按钮(至少在Android设备上)将退出该应用程序。 这不是我正在尝试做的事情,但现在可以使用。

如果有人可以告诉我在哪里放置“处理”的东西,我仍然很想知道!在此期间,我将一直保持联系。感谢那些在网络上分享了很多有用信息的人!

顺便说一下,这是新代码,需要进行一些清理:

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import './SecondScreen.dart';

class WebView extends StatefulWidget {
  @override
  _WebViewState createState() => _WebViewState();
}

class _WebViewState extends State<WebView> {
  TextEditingController controller = TextEditingController();
  FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
  var urlString =
      "https://stackoverflow.com/questions/43692923/flutter-container-onpressed";

  launchUrl() {
    setState(() {
      urlString = controller.text;
      flutterWebviewPlugin.reloadUrl(urlString);
    });
  }

  @override
  void initState() {
    super.initState();
    flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged wvs) {
      print(wvs.type);
    });
  }

  //this part isn't working
  @override
  void dispose() {
    _WebViewState();
    // close the webview here
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Color(0xFF8e80a8)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          new GestureDetector(
            onTap: () {
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(builder: (context) => SecondScreen()),
              );
            },
            child: Padding(
              padding: const EdgeInsets.fromLTRB(10, 40, 10, 10),
              child: new Container(
                  constraints: BoxConstraints.expand(
                    height: Theme.of(context).textTheme.display1.fontSize * 1.1 +
                        40.0,
                  ),
                  child: new Image.asset(
                    'lib/assets/newoldralogo1menubutton3.png',
                    alignment: Alignment.topCenter,
                    fit: BoxFit.fitWidth,
                  )),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: Container(
              constraints: BoxConstraints.expand(
                height: Theme.of(context).textTheme.display1.fontSize * 1.1 + 250.0,
              ),
              child: WebviewScaffold(
                url: urlString,
                withZoom: false,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

答案 1 :(得分:0)

当用户导航到第二个屏幕时隐藏网络视图

flutterWebViewPlugin.hide();

,并在用户向后导航时再次显示

flutterWebViewPlugin.show();

应该解决问题。通过使用这种方法,用户可以在导航到第二个屏幕后导航回到第一个屏幕。

记住要等待Navigator.push()进入第二个屏幕。

import 'package:flutter/material.dart';

import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

import 'dart:async';

import './SecondScreen.dart';

String selectedUrl = 'https://flutter.io';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  final flutterWebViewPlugin = FlutterWebviewPlugin();

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Generated App',
      theme: new ThemeData(
        primarySwatch: Colors.purple,
        primaryColor: const Color(0xFF8e80a8),
        accentColor: const Color(0xFF8e80a8),
        canvasColor: const Color(0xFF8e80a8),
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {


  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Color(0xFF8e80a8)),
      child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(10, 30, 10, 5),
              child: new Image.asset(
                'lib/assets/newoldralogo1.png',
              ),
            ),
            Container(
              constraints: BoxConstraints.expand(
                height:
                    Theme.of(context).textTheme.display1.fontSize * 1.1 + 10.0,
              ),
              child: new Stack(fit: StackFit.expand,
                  children: <Widget>[
                Container(
                  decoration: BoxDecoration(color: Color(0xFF8e80a8)),
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(10, 10, 10, 10),
                    child: Image.asset(
                      "lib/assets/newoldralogo1.png",
                      alignment: Alignment.topCenter,
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                ),
                Container(
                  child: RaisedButton(
                    onPressed: () async {
                      flutterWebViewPlugin.hide();
                      await Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => SecondScreen()),
                      );
                      flutterWebViewPlugin.show();
                    },
                    color: Color(0xFFFFFF),
                  ),
                ),
              ]),
            ),

            Container(
              constraints: BoxConstraints.expand(
                height:
                    Theme.of(context).textTheme.display1.fontSize * 1.1 + 300.0,
              ),
              padding: const EdgeInsets.fromLTRB(10, 15, 10, 5),
              color: Color(0xFF8e80a8),
              alignment: Alignment.center,
              child: Container(
                child: WebviewScaffold(
                  url:
                      ('https://flutter.io/'),
                ),
              ),
            )
          ]),
    );
  }
}