首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Flutter按钮应加载新的webview url

Flutter按钮应加载新的webview url
EN

Stack Overflow用户
提问于 2018-10-01 23:05:15
回答 1查看 3.1K关注 0票数 1

这可能是一个相对简单的问题,但我对Flutter是个新手,我需要今天就实现它。

我有一个Flutter应用程序,看起来像这样...

有一个颤动的BottomNavigationBar和一个颤动的Webview_Plugin

所有我需要的帮助是一个新的网址加载时,按下按钮主页或语言。但是我很难完全弄清楚颤动的范例。

我当前的代码,如果有帮助的话:

代码语言:javascript
运行
复制
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:flutter_webview_plugin_example/home.dart';
import 'package:flutter_webview_plugin_example/settings.dart';

const kAndroidUserAgent =
    'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36';

String selectedUrl = 'http://google.com';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'MyApp',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        '/': (_) => const MyHomePage(title: 'MyApp'),
        '/widget': (_) => new WebviewScaffold(
              url: selectedUrl,
              appBar: new AppBar(
                title: const Text('Widget webview'),
              ),
              withZoom: true,
              withLocalStorage: true,
            )
      },
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  // Instance of WebView plugin
  final flutterWebviewPlugin = new FlutterWebviewPlugin();

  // On destroy stream
  StreamSubscription _onDestroy;

  // On urlChanged stream
  StreamSubscription<String> _onUrlChanged;

  // On urlChanged stream
  StreamSubscription<WebViewStateChanged> _onStateChanged;

  StreamSubscription<WebViewHttpError> _onHttpError;

  StreamSubscription<double> _onScrollYChanged;

  StreamSubscription<double> _onScrollXChanged;

  final _urlCtrl = new TextEditingController(text: selectedUrl);

  final _codeCtrl =
      new TextEditingController(text: 'window.navigator.userAgent');

  final _scaffoldKey = new GlobalKey<ScaffoldState>();

  final _history = [];

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

    flutterWebviewPlugin.close();

    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);

    _urlCtrl.addListener(() {
      selectedUrl = _urlCtrl.text;
    });

    // Add a listener to on destroy WebView, so you can make came actions.
    _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
      if (mounted) {
        // Actions like show a info toast.
        _scaffoldKey.currentState.showSnackBar(
            const SnackBar(content: const Text('Webview Destroyed')));
      }
    });

    // Add a listener to on url changed
    _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
      if (mounted) {
        setState(() {
          _history.add('onUrlChanged: $url');
        });
      }
    });

    _onScrollYChanged =
        flutterWebviewPlugin.onScrollYChanged.listen((double y) {
      if (mounted) {
        setState(() {
          _history.add("Scroll in Y Direction: $y");
        });
      }
    });

    _onScrollXChanged =
        flutterWebviewPlugin.onScrollXChanged.listen((double x) {
      if (mounted) {
        setState(() {
          _history.add("Scroll in X Direction: $x");
        });
      }
    });

    _onStateChanged =
        flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
      if (mounted) {
        setState(() {
          _history.add('onStateChanged: ${state.type} ${state.url}');
        });
      }
    });

    _onHttpError =
        flutterWebviewPlugin.onHttpError.listen((WebViewHttpError error) {
      if (mounted) {
        setState(() {
          _history.add('onHttpError: ${error.code} ${error.url}');
        });
      }
    });
  }

  @override
  void dispose() {
    // Every listener should be canceled, the same should be done with this stream.
    _onDestroy.cancel();
    _onUrlChanged.cancel();
    _onStateChanged.cancel();
    _onHttpError.cancel();
    _onScrollXChanged.cancel();
    _onScrollYChanged.cancel();

    flutterWebviewPlugin.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    BottomNavigationBar btm = new BottomNavigationBar(
      items: [
        new BottomNavigationBarItem(
            icon: new Icon(
              Icons.home,
              color: const Color(0xFFFFFFFF),
            ),
            title: new Text(
              "Home",
              style: new TextStyle(
                color: const Color(0xFFFFFFFF),
              ),
            )),
        new BottomNavigationBarItem(
            icon: new Icon(
              Icons.settings,
              color: const Color(0xFFFFFFFF),
            ),
            title: new Text(
              "Language",
              style: new TextStyle(
                color: const Color(0xFFFFFFFF),
              ),
            )),
      ],
//      onTap: changeURL()
    );

    flutterWebviewPlugin.launch(selectedUrl,
    rect: new Rect.fromLTWH(
    0.0, 0.0,
    MediaQuery.of(context).size.width, MediaQuery.of(context).size.height - 55),
    userAgent: kAndroidUserAgent);

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          widget.title,
          style: new TextStyle(color: const Color(0xFFFFFFFF)),
        ),
      ),
      bottomNavigationBar: new Theme(
        data: Theme.of(context).copyWith(
          // sets the background color of the `BottomNavigationBar`
          canvasColor: const Color(0xFF167F67),
        ), // sets the inactive color of the `BottomNavigationBar`
        child: btm
      ),
    );
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-02 00:53:21

所以看起来它所需要的就是类似下面的onTap…

代码语言:javascript
运行
复制
@override
Widget build(BuildContext context) {
  BottomNavigationBar btm = new BottomNavigationBar(
    items: [
      new BottomNavigationBarItem(
          icon: new Icon(
            Icons.home,
            color: const Color(0xFFFFFFFF),
          ),
          title: new Text(
            "Home",
            style: new TextStyle(
              color: const Color(0xFFFFFFFF),
            ),
          )),
      new BottomNavigationBarItem(
          icon: new Icon(
            Icons.settings,
            color: const Color(0xFFFFFFFF),
          ),
          title: new Text(
            "Language",
            style: new TextStyle(
              color: const Color(0xFFFFFFFF),
            ),
          )),
    ],
    onTap: (int index) {
      flutterWebviewPlugin.close();
      flutterWebviewPlugin.launch('http://google.com',
          rect: new Rect.fromLTWH(
              0.0, 0.0,
              MediaQuery.of(context).size.width, MediaQuery.of(context).size.height - 55),
          userAgent: kAndroidUserAgent);
    },
  );

回想起来很简单,但很难做到恰到好处

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52593961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档