Files
PiliPlus/lib/pages/webview/view.dart
bggRGjQaUbCoE 751db80b4a opt: webview
2024-09-03 08:43:37 +08:00

116 lines
4.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import 'controller.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebviewPage extends StatefulWidget {
const WebviewPage({super.key});
@override
State<WebviewPage> createState() => _WebviewPageState();
}
class _WebviewPageState extends State<WebviewPage> {
final WebviewController _webviewController = Get.put(WebviewController());
@override
void dispose() {
Get.delete<WebviewController>();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: false,
titleSpacing: 0,
title: Obx(
() => Text(
_webviewController.pageTitle.value,
style: Theme.of(context).textTheme.titleMedium,
),
),
actions: [
const SizedBox(width: 4),
IconButton(
tooltip: '刷新网页',
onPressed: () {
_webviewController.controller.reload();
},
icon: Icon(Icons.refresh_outlined,
color: Theme.of(context).colorScheme.primary),
),
IconButton(
tooltip: '用外部浏览器打开',
onPressed: () {
launchUrl(Uri.parse(_webviewController.url));
},
icon: Icon(Icons.open_in_browser_outlined,
color: Theme.of(context).colorScheme.primary),
),
const SizedBox(width: 12)
],
),
body: Column(
children: [
Obx(
() => AnimatedContainer(
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 350),
height: _webviewController.loadShow.value ? 4 : 0,
child: LinearProgressIndicator(
key: ValueKey(_webviewController.loadProgress),
value: _webviewController.loadProgress / 100,
),
),
),
if (_webviewController.type.value == 'login') ...<Widget>[
Container(
width: double.infinity,
color: Theme.of(context).colorScheme.onInverseSurface,
padding: const EdgeInsets.only(
left: 12, right: 12, top: 6, bottom: 6),
child: const Text('登录成功未自动跳转? 请点击右上角「刷新登录态」'),
),
const SizedBox(height: 4),
Container(
width: double.infinity,
color: Theme.of(context).colorScheme.onInverseSurface,
padding: const EdgeInsets.only(
left: 12, right: 12, top: 6, bottom: 6),
child: const Text(
'如需二维码登录请点击「电脑版」放大左侧二维码截图后官方app或另一设备扫码授权后点击「刷新登录态」'),
),
],
Expanded(
child: SafeArea(
child: WebViewWidget(
controller: _webviewController.controller,
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>{
Factory<VerticalDragGestureRecognizer>(
() => VerticalDragGestureRecognizer(),
),
Factory<PanGestureRecognizer>(
() => PanGestureRecognizer(),
),
Factory<ForcePressGestureRecognizer>(
() => ForcePressGestureRecognizer(),
),
Factory<EagerGestureRecognizer>(
() => EagerGestureRecognizer(),
),
Factory<HorizontalDragGestureRecognizer>(
() => HorizontalDragGestureRecognizer(),
),
}),
),
),
],
));
}
}