chatapp3-flutter/lib/modules/wallet/recharge/mifa_pay_webview_page.dart
2026-04-20 18:52:03 +08:00

121 lines
3.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:yumi/shared/tools/sc_url_launcher_utils.dart';
import 'package:yumi/ui_kit/components/appbar/socialchat_appbar.dart';
import 'package:webview_flutter/webview_flutter.dart';
class MiFaPayWebViewPage extends StatefulWidget {
const MiFaPayWebViewPage({
super.key,
required this.requestUrl,
this.title = 'MiFaPay',
});
final String requestUrl;
final String title;
@override
State<MiFaPayWebViewPage> createState() => _MiFaPayWebViewPageState();
}
class _MiFaPayWebViewPageState extends State<MiFaPayWebViewPage> {
late final WebViewController _controller;
bool _isLoading = true;
double _progress = 0;
@override
void initState() {
super.initState();
_controller =
WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onNavigationRequest: (NavigationRequest request) async {
if (!SCUrlLauncherUtils.shouldOpenExternally(request.url)) {
return NavigationDecision.navigate;
}
final bool launched = await SCUrlLauncherUtils.launchExternal(
request.url,
);
if (launched) {
return NavigationDecision.prevent;
}
return SCUrlLauncherUtils.isHttpUrl(request.url)
? NavigationDecision.navigate
: NavigationDecision.prevent;
},
onProgress: (int progress) {
_updateState(() {
_progress = progress / 100;
});
},
onPageStarted: (_) {
_updateState(() {
_isLoading = true;
});
},
onPageFinished: (_) {
_updateState(() {
_isLoading = false;
_progress = 1;
});
},
onWebResourceError: (_) {
_updateState(() {
_isLoading = false;
});
},
),
)
..loadRequest(Uri.parse(widget.requestUrl));
}
@override
void dispose() {
super.dispose();
}
void _updateState(VoidCallback action) {
if (!mounted) {
return;
}
setState(action);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: SocialChatStandardAppBar(
title: widget.title,
actions: const <Widget>[],
backgroundColor: Colors.white,
backButtonColor: Colors.black,
gradient: const LinearGradient(
colors: <Color>[Colors.white, Colors.white],
),
),
body: Stack(
children: [
WebViewWidget(controller: _controller),
if (_isLoading || _progress < 1)
Positioned(
top: 0,
left: 0,
right: 0,
child: LinearProgressIndicator(
value: _progress > 0 && _progress < 1 ? _progress : null,
minHeight: 2,
color: const Color(0xff18F2B1),
backgroundColor: const Color(0xffEDEDED),
),
),
],
),
);
}
}