flutter 你能告诉我如何在ios webview上回到我的应用程序中的前几页吗?

nbnkbykc  于 2023-05-01  发布在  Flutter
关注(0)|答案(2)|浏览(112)

你能告诉我如何在我的webview应用程序中回到以前的页面吗?
我正在使用以下代码,适用于Android,但不能在iPhone上滑动-

late WebViewController? webViewController;

   Future<bool> _onWillPop(BuildContext context) async {
     if (await webViewController!.canGoBack()) {
       webViewController!.goBack();
       return Future value(false);
     } else {
       return Future value(true);
     }
   }

这是我完整的小部件代码-

return WillPopScope(
        child: Scaffold(
          bottomNavigationBar: BottomNavigationBar(
              selectedItemColor: Colors.orange,
              backgroundColor: Colors.white,
              type: BottomNavigationBarType.fixed,
              currentIndex: selectedIndex,
              unselectedLabelStyle: TextStyle(color: Color.fromARGB(153, 162, 173, 1)),
              selectedLabelStyle: TextStyle(fontSize: 11.9),
              items: [
                BottomNavigationBarItem(
                  icon: Container(margin: EdgeInsets.only(bottom: 5), height: 32, width: 35, child: Image.asset('assets/images/Home.png', fit: BoxFit.cover,),),
                  activeIcon: Container(margin: EdgeInsets.only(bottom: 5),height: 32, width: 35, child: Image.asset('assets/images/Home_color.png', fit: BoxFit.cover,),),
                  label: "Главная"
                ),
                BottomNavigationBarItem(
                  icon: Container(margin: EdgeInsets.only(bottom: 5),height: 32, width: 32, child: Image.asset('assets/images/Catalog.png', fit: BoxFit.cover,)),
                  activeIcon: Container(margin: EdgeInsets.only(bottom: 5),height: 32, width: 32, child: Image.asset('assets/images/Catalog_color.png', fit: BoxFit.cover,),),
                  label: "Каталог",
                ),
              ],
              onTap: (i) async {
                webViewController?.loadUrl(linkNavbar[i]);
                setState(() => selectedIndex = i);
              },
            ),
          body: SafeArea(
            child: Stack(
                children: [
                  WebView(
                    key: _key,
                    javascriptMode: JavascriptMode.unrestricted,
                    initialUrl: "https://caravan-store.shop/",
                    onWebViewCreated: (controller) {
                      setState(() {
                        webViewController = controller;
                      });
                    },
                    onWebResourceError: (WebResourceError error) {
                      Navigator.pushReplacement(
                        context,
                        MaterialPageRoute(
                          builder: (context) => MyError(),
                        ),
                      );
                    },
                  ),
        
                ]
            ),
          )
        ),
          onWillPop: () => _onWillPop(context)
    );

另外,我把这段代码添加到了我的main中。镖

theme: ThemeData(
    pageTransitionsTheme: PageTransitionsTheme(
      builders: {
        TargetPlatform.android: CupertinoPageTransitionsBuilder(),
        TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
      }
    )

在互联网上,我找不到解决这个问题的办法。我正在做的是尝试向右滑动以转到上一页。不幸的是,它不能在iPhone上工作,但它可以在Android上工作。
告诉我该怎么做?

xn1cxnb4

xn1cxnb41#

它的工作android和ios两个现在

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

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

  @override
  State<WebExample> createState() => _WebExampleState();
}

class _WebExampleState extends State<WebExample> {
  late final WebViewController _controller;

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

    // #docregion platform_features
    late final PlatformWebViewControllerCreationParams params;
    if (WebViewPlatform.instance is WebKitWebViewPlatform) {
      params = WebKitWebViewControllerCreationParams(
        allowsInlineMediaPlayback: true,
        mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
      );
    } else {
      params = const PlatformWebViewControllerCreationParams();
    }

    final WebViewController controller =
        WebViewController.fromPlatformCreationParams(params);
    // #enddocregion platform_features

    controller
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onNavigationRequest: (NavigationRequest request) {
            if (request.url.startsWith('https://www.youtube.com/')) {
              debugPrint('blocking navigation to ${request.url}');
              return NavigationDecision.prevent;
            }
            debugPrint('allowing navigation to ${request.url}');
            return NavigationDecision.navigate;
          },
          onUrlChange: (UrlChange change) {
            debugPrint('url change to ${change.url}');
          },
        ),
      )
      ..addJavaScriptChannel(
        'Toaster',
        onMessageReceived: (JavaScriptMessage message) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(message.message)),
          );
        },
      )
      ..loadRequest(Uri.parse('https://flutter.dev'));

    // #docregion platform_features
    if (controller.platform is AndroidWebViewController) {
      AndroidWebViewController.enableDebugging(true);
      (controller.platform as AndroidWebViewController)
          .setMediaPlaybackRequiresUserGesture(false);
    }

    _controller = controller;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => _backPressed2(_controller, context),
      child: Scaffold(
        body: WebViewWidget(controller: _controller),
      ),
    );
  }
}

Future<bool> _backPressed2(
    WebViewController webViewController, BuildContext context) async {
  if (await webViewController.canGoBack()) {
    await webViewController.goBack();
  } else {
    if (context.mounted) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('No back history item')),
      );
    }
  }
  return Future<bool>.value(false);
}
t30tvxxf

t30tvxxf2#

老实说,我不知道为什么它能工作,但通过添加以下代码,一切都能在iPhone上工作。

WebView(
                  key: _key,
                  javascriptMode: JavascriptMode.unrestricted,
                  initialUrl: "link",
                  gestureNavigationEnabled: true, //this

相关问题