gpt4 book ai didi

flutter - 在flutter中下拉刷新WebView页面

转载 作者:行者123 更新时间:2023-12-04 14:28:22 24 4
gpt4 key购买 nike

我用 WebView 创建了一个 flutter 应用程序

有什么办法可以扫一扫刷新网页

我正在使用“webview_flutter_plugin”插件

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Stack(
children: <Widget>[
Container(
child: Center(
child: Text(_title),
),
),
],
)),
body: SafeArea(
child: WebView(
key: _key,
javascriptMode: JavascriptMode.unrestricted,
initialUrl: _url)),
);
}
}

最佳答案

解决起来并不容易,但您可以尝试这种方式。

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:fluttertoast/fluttertoast.dart';

int scheck;
WebViewController controllerGlobal;
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIOverlays([]);
runApp(
new MaterialApp(
debugShowCheckedModeBanner: false,
home: new WebViewEx(),
),
);
}

class WebViewEx extends StatefulWidget {
@override
WebViewExampleState createState() => WebViewExampleState();
}

class WebViewExampleState extends State<WebViewEx> {
bool check, check1;
ScrollController _scrollController;
@override
void initState() {
_scrollController = ScrollController();
_scrollController.addListener(_scrollListener);
super.initState();
check = false;
check1 = false;
}

_scrollListener() {
if (_scrollController.offset <=
_scrollController.position.minScrollExtent &&
!_scrollController.position.outOfRange) {
controllerGlobal.reload();
}
}

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () => controllerGlobal.goBack(),
child: ListView(
physics: ClampingScrollPhysics(),
shrinkWrap: true,
children: [
Container(
padding: EdgeInsets.all(10),
height: MediaQuery.of(context).size.height,
child: WebView(
initialUrl: '...............',
gestureNavigationEnabled: true,
debuggingEnabled: true,
gestureRecognizers: [
Factory(() => PlatformViewVerticalGestureRecognizer()),
].toSet(),
javascriptMode: JavascriptMode.unrestricted,
onPageFinished: (String url) {
setState(() {
bool temp = check;
check = true;
if (!temp) controllerGlobal.scrollBy(0, 10);
});
},
onWebViewCreated: (WebViewController webViewController) {
controllerGlobal = webViewController;
},
),
),
],
),
);
}

@override
void dispose() {
super.dispose();
}
}

class PlatformViewVerticalGestureRecognizer
extends VerticalDragGestureRecognizer {
PlatformViewVerticalGestureRecognizer({PointerDeviceKind kind})
: super(kind: kind);

Offset _dragDistance = Offset.zero;

@override
void addPointer(PointerEvent event) {
startTrackingPointer(event.pointer);
}

@override
void handleEvent(PointerEvent event) {
_dragDistance = _dragDistance + event.delta;
controllerGlobal.getScrollY().then((value) {
print(value);
if (value < 5) {
Fluttertoast.showToast(
msg: "RELOADING",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.amber[800],
textColor: Colors.black,
fontSize: 16.0);
controllerGlobal.reload();
}
});
// if (event is PointerMoveEvent) {
// final double dy = _dragDistance.dy.abs();
// final double dx = _dragDistance.dx.abs();

// if (dy > dx && dy > kTouchSlop) {
// // vertical drag - accept
// resolve(GestureDisposition.accepted);
// _dragDistance = Offset.zero;
// } else if (dx > kTouchSlop && dx > dy) {
// // horizontal drag - stop tracking
// stopTrackingPointer(event.pointer);
// _dragDistance = Offset.zero;
// }
// }
}

@override
String get debugDescription => 'horizontal drag (platform view)';

@override
void didStopTrackingLastPointer(int pointer) {}
}

关于flutter - 在flutter中下拉刷新WebView页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57656045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com