确保 WebView 在点击 BottomBarItem(颤动)后打开新 URL

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

所以我一直在努力让我的应用程序正常工作。我想使用 flutter 为我的一个网站创建一个应用程序,到目前为止一切顺利。唯一的问题是,当我单击标记为“状态”的按钮时,WebView 不会更改为我想要它转到的页面。

我的代码:

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

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  int _selectedIndex = 0;
  final List<String> _urls = [
    "https://minecraft.xandersalarie.com/",
    "https://minecraft.xandersalarie.com/status",
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: WebView(
            initialUrl: _urls[_selectedIndex],
            javascriptMode: JavascriptMode.unrestricted,
          ),
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: 'Home',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.stacked_bar_chart_outlined),
              label: 'Status',
            ),
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: Colors.amber[800],
          onTap: _onItemTapped,
        ),
      ),
    );
  }
}

我尝试了迄今为止在互联网上读过的内容。我已经读到,到目前为止你不能使用initialUrl两次,所以我确保使用

final List<String>_urls
,因为这是我找到的唯一选项。我期待发生的是,当它在“家”时。它从登录页面开始,当我点击“状态”时,它会转到列表中的另一个网址。这不会发生在我身上。我还想要的是,如果你点击主页,它会刷新 WebView,但它也不会这样做

android iphone flutter dart webview
1个回答
0
投票

您可以使用 webview 控制器和 webviewwidget 来控制页面导航。

您可以使用官方的flutter.dev webview包:https://pub.dev/packages/webview_flutter

类似这样的:

class MyApp extends StatefulWidget {
  const MyApp({super.key});

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

class _MyAppState extends State<MyApp> {
  int _selectedIndex = 0;
  final List<String> _urls = [
    "https://minecraft.xandersalarie.com/",
    "https://minecraft.xandersalarie.com/status",
  ];

  late final WebViewController controller;

  @override
  void initState() {
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(
        NavigationDelegate(
          onNavigationRequest: (request) async {
            if (_urls.contains(request.url)) {
              return NavigationDecision.navigate;
            }

            return NavigationDecision.prevent;
          },
        ),
      )
      ..loadRequest(Uri.parse(_urls[_selectedIndex])).onError((error, stackTrace) => log(
        'error',
        error: error,
        stackTrace: stackTrace,
      ));
    super.initState();
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
    // This will load new url to the webview
    controller.loadRequest(Uri.parse(_urls[_selectedIndex]));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: WebViewWidget(
            controller: controller,
          ),
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: 'Home',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.stacked_bar_chart_outlined),
              label: 'Status',
            ),
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: Colors.amber[800],
          onTap: _onItemTapped,
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.