可滚动和可缩放的WebView在 Flutter

kknvjkwl  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(173)

我需要在webView中启用缩放,但每当我启用缩放时,滚动功能被禁用,尝试使用单个子滚动视图,不起作用,现在我的代码滚动,但不能放大或缩小!我试图通过交互式查看器 Package 它,但交互式查看器禁用滚动!我如何解决这个问题?

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../widgets/headers/custom_app_bar.dart';

class InfoScreen extends StatefulWidget {
  final String url;
  final String title;

  InfoScreen({Key key, @required this.title, @required this.url})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _InfoScreenState();
}

class _InfoScreenState extends State<InfoScreen> {
  bool isLoading = true;
  final _key = UniqueKey();

  @override
  Widget build(BuildContext context) {
    WebViewController controller;
    return Scaffold(
      appBar: CustomAppBar(title: widget.title),
      body: Stack(
        children: <Widget>[
          WebView(
            key: _key,
            initialUrl: widget.url,
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              controller = webViewController;
            },
            onPageFinished: (finish) {
              setState(() {
                isLoading = false;
              });
            },
            gestureRecognizers: Set()
              ..add(Factory<ScaleGestureRecognizer>(
                      () => ScaleGestureRecognizer())),
          ),
          isLoading
              ? Center(
            child: CircularProgressIndicator(),
          )
              : Stack(),
        ],
      ),
    );
  }
}
am46iovg

am46iovg1#

尝试为垂直和水平方向添加gestureRecognizers。以下是修改后的代码:

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../widgets/headers/custom_app_bar.dart';

class InfoScreen extends StatefulWidget {
  final String url;
  final String title;

  InfoScreen({Key key, @required this.title, @required this.url})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _InfoScreenState();
}

class _InfoScreenState extends State<InfoScreen> {
  bool isLoading = true;
  final _key = UniqueKey();

  @override
  Widget build(BuildContext context) {
    WebViewController controller;
    return Scaffold(
      appBar: CustomAppBar(title: widget.title),
      body: Stack(
        children: <Widget>[
          WebView(
            key: _key,
            initialUrl: widget.url,
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              controller = webViewController;
            },
            onPageFinished: (finish) {
              setState(() {
                isLoading = false;
              });
            },
            gestureRecognizers: Set()
              ..add(
                Factory<VerticalDragGestureRecognizer>(
                  () => VerticalDragGestureRecognizer(),
                ),
              )
              ..add(
                Factory<HorizontalDragGestureRecognizer>(
                  () => HorizontalDragGestureRecognizer(),
                ),
              )
              ..add(
                Factory<ScaleGestureRecognizer>(
                  () => ScaleGestureRecognizer(),
                ),
              ),
          ),
          isLoading
              ? Center(
                  child: CircularProgressIndicator(),
                )
              : Stack(),
        ],
      ),
    );
  }
}

相关问题