forked from eatvio/eatvio_app
80 lines
2.2 KiB
Dart
80 lines
2.2 KiB
Dart
import 'package:eatvio_app/core/app_config.dart';
|
|
import 'package:eatvio_app/core/locator_service.dart';
|
|
import 'package:eatvio_app/core/widgets/loader.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
|
|
const _passKeyLoginPath = 'login_with_pass_key';
|
|
const _contentReadyProgress = 40;
|
|
|
|
@visibleForTesting
|
|
bool shouldHideGlossaryLoader({required String url, required int progress}) {
|
|
if (url.isEmpty || url.contains(_passKeyLoginPath)) {
|
|
return false;
|
|
}
|
|
return progress >= _contentReadyProgress;
|
|
}
|
|
|
|
class GlossaryScreen extends StatefulWidget {
|
|
const GlossaryScreen({super.key, required this.passKey});
|
|
final String passKey;
|
|
@override
|
|
State<GlossaryScreen> createState() => _GlossaryScreenState();
|
|
}
|
|
|
|
class _GlossaryScreenState extends State<GlossaryScreen> {
|
|
late final WebViewController _controller;
|
|
bool _isLoading = true;
|
|
String _currentUrl = '';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
_controller = WebViewController()
|
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
|
..setNavigationDelegate(
|
|
NavigationDelegate(
|
|
onPageStarted: (url) {
|
|
_currentUrl = url;
|
|
},
|
|
onProgress: (progress) {
|
|
if (shouldHideGlossaryLoader(url: _currentUrl, progress: progress)) {
|
|
_hideLoader();
|
|
}
|
|
},
|
|
onPageFinished: (url) {
|
|
if (shouldHideGlossaryLoader(url: url, progress: 100)) {
|
|
_hideLoader();
|
|
}
|
|
},
|
|
),
|
|
)
|
|
..loadRequest(Uri.parse('${sl<AppConfig>().baseUrl}/login_with_pass_key?id=${widget.passKey}'));
|
|
}
|
|
|
|
void _hideLoader() {
|
|
if (!_isLoading || !mounted) {
|
|
return;
|
|
}
|
|
setState(() => _isLoading = false);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Глоссарий')),
|
|
body: Stack(
|
|
children: [
|
|
WebViewWidget(controller: _controller),
|
|
if (_isLoading)
|
|
ColoredBox(
|
|
color: Theme.of(context).scaffoldBackgroundColor,
|
|
child: const SizedBox.expand(child: Loader()),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|