96 lines
2.8 KiB
Dart
96 lines
2.8 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
import 'package:webview_flutter_android/webview_flutter_android.dart';
|
|
|
|
import '../../core/theme/app_colors.dart';
|
|
import '../../shared/widgets/top_nav_bar.dart';
|
|
|
|
/// 应用内 WebView 页面(支付、隐私政策、用户协议等)
|
|
class PaymentWebViewScreen extends StatefulWidget {
|
|
const PaymentWebViewScreen({
|
|
super.key,
|
|
required this.paymentUrl,
|
|
this.title = 'Payment',
|
|
});
|
|
|
|
final String paymentUrl;
|
|
final String title;
|
|
|
|
@override
|
|
State<PaymentWebViewScreen> createState() => _PaymentWebViewScreenState();
|
|
}
|
|
|
|
class _PaymentWebViewScreenState extends State<PaymentWebViewScreen> {
|
|
late final WebViewController _controller;
|
|
int _loadingProgress = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller = WebViewController()
|
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
|
..setNavigationDelegate(
|
|
NavigationDelegate(
|
|
onProgress: (progress) {
|
|
if (mounted) setState(() => _loadingProgress = progress);
|
|
},
|
|
onPageStarted: (_) {
|
|
if (mounted) setState(() => _loadingProgress = 0);
|
|
},
|
|
onPageFinished: (_) {
|
|
if (mounted) setState(() => _loadingProgress = 100);
|
|
},
|
|
onWebResourceError: (e) {},
|
|
),
|
|
)
|
|
..loadRequest(Uri.parse(widget.paymentUrl));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: AppColors.surface,
|
|
appBar: PreferredSize(
|
|
preferredSize: const Size.fromHeight(59),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
TopNavBar(
|
|
title: widget.title,
|
|
showBackButton: true,
|
|
onBack: () => Navigator.of(context).pop(),
|
|
),
|
|
if (_loadingProgress < 100)
|
|
SizedBox(
|
|
height: 3,
|
|
child: LinearProgressIndicator(
|
|
value: _loadingProgress / 100,
|
|
backgroundColor: AppColors.surfaceAlt,
|
|
valueColor:
|
|
const AlwaysStoppedAnimation<Color>(AppColors.primary),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
body: SafeArea(
|
|
child: _buildWebView(),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildWebView() {
|
|
if (defaultTargetPlatform == TargetPlatform.android) {
|
|
return WebViewWidget.fromPlatformCreationParams(
|
|
params: AndroidWebViewWidgetCreationParams(
|
|
controller: _controller.platform as AndroidWebViewController,
|
|
displayWithHybridComposition: true,
|
|
),
|
|
);
|
|
}
|
|
return WebViewWidget(controller: _controller);
|
|
}
|
|
}
|