games_app/prediqo_mobile/lib/shared/widgets/turnstile_captcha.dart
2026-11-15 19:48:21 +02:00

145 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../core/config/app_config.dart';
import '../../core/di/injection.dart';
/// Cloudflare Turnstile widget for native login (WebView + explicit render).
class TurnstileCaptcha extends StatefulWidget {
const TurnstileCaptcha({
super.key,
required this.siteKey,
required this.onToken,
});
final String siteKey;
final ValueChanged<String?> onToken;
@override
State<TurnstileCaptcha> createState() => _TurnstileCaptchaState();
}
class _TurnstileCaptchaState extends State<TurnstileCaptcha> {
late final WebViewController _controller;
var _ready = false;
@override
void initState() {
super.initState();
_controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(Colors.transparent)
..addJavaScriptChannel(
'TurnstileBridge',
onMessageReceived: (message) {
final raw = message.message.trim();
if (raw.isEmpty || raw == 'expired' || raw == 'error') {
widget.onToken(null);
return;
}
widget.onToken(raw);
},
)
..setNavigationDelegate(
NavigationDelegate(
onPageFinished: (_) {
if (mounted) setState(() => _ready = true);
},
),
);
_load();
}
String get _baseUrl {
final api = Uri.tryParse(sl<AppConfig>().apiBaseUrl);
if (api != null &&
api.hasScheme &&
api.host.isNotEmpty &&
api.host != 'localhost' &&
api.host != '127.0.0.1' &&
!api.host.startsWith('10.0.2.2')) {
return '${api.scheme}://${api.host}/';
}
return '${AppConfig.defaultProductionApiBaseUrl}/';
}
String _html(String siteKey) {
final escaped = siteKey.replaceAll("'", r"\'");
return '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit" async defer></script>
<style>
html, body {
margin: 0;
padding: 0;
background: transparent;
overflow: hidden;
}
#cf-turnstile { display: flex; justify-content: center; }
</style>
</head>
<body>
<div id="cf-turnstile"></div>
<script>
function notify(msg) {
try { TurnstileBridge.postMessage(String(msg)); } catch (e) {}
}
function renderWhenReady() {
if (!window.turnstile) {
setTimeout(renderWhenReady, 50);
return;
}
window.turnstile.render('#cf-turnstile', {
sitekey: '$escaped',
theme: 'auto',
callback: function (token) { notify(token); },
'expired-callback': function () { notify('expired'); },
'error-callback': function () { notify('error'); }
});
}
renderWhenReady();
</script>
</body>
</html>
''';
}
Future<void> _load() async {
widget.onToken(null);
await _controller.loadHtmlString(_html(widget.siteKey), baseUrl: _baseUrl);
}
@override
void didUpdateWidget(covariant TurnstileCaptcha oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.siteKey != widget.siteKey) {
setState(() => _ready = false);
_load();
}
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: 72,
width: double.infinity,
child: Stack(
alignment: Alignment.center,
children: [
WebViewWidget(controller: _controller),
if (!_ready)
const SizedBox(
width: 22,
height: 22,
child: CircularProgressIndicator(strokeWidth: 2),
),
],
),
);
}
}