forked from eatvio/eatvio_app
feat: webview
This commit is contained in:
@@ -17,6 +17,7 @@ import 'package:go_router/go_router.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import 'package:validators/validators.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
import 'bloc/auth_bloc.dart';
|
||||
import 'models/auth_model.dart';
|
||||
@@ -43,11 +44,71 @@ class _AuthScreenState extends State<AuthScreen> {
|
||||
bool checkboxState = false;
|
||||
bool hidePassword = true;
|
||||
final baseUrlController = TextEditingController();
|
||||
late final WebViewController _controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..addJavaScriptChannel(
|
||||
'FlutterChannel',
|
||||
onMessageReceived: (JavaScriptMessage message) {
|
||||
// Handle messages from JavaScript
|
||||
final data = jsonDecode(message.message);
|
||||
final email = data['email'];
|
||||
final password = data['password'];
|
||||
print('Email: $email, Password: $password');
|
||||
},
|
||||
)
|
||||
..setNavigationDelegate(
|
||||
NavigationDelegate(
|
||||
onPageFinished: (String url) async {
|
||||
await _controller.runJavaScript('''
|
||||
function initializeListeners() {
|
||||
const emailInput = document.getElementById("inputEmail_main");
|
||||
const passwordInput = document.getElementById("inputPassword_main");
|
||||
|
||||
if (emailInput && passwordInput) {
|
||||
function sendToFlutter() {
|
||||
const email = emailInput.value;
|
||||
const password = passwordInput.value;
|
||||
const data = JSON.stringify({ email: email, password: password });
|
||||
if (window.FlutterChannel) {
|
||||
window.FlutterChannel.postMessage(data);
|
||||
}
|
||||
}
|
||||
|
||||
emailInput.addEventListener("input", sendToFlutter);
|
||||
passwordInput.addEventListener("input", sendToFlutter);
|
||||
} else {
|
||||
console.error("Email or Password input not found.");
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure DOM is loaded before attaching listeners
|
||||
document.addEventListener("DOMContentLoaded", initializeListeners);
|
||||
initializeListeners();
|
||||
''');
|
||||
},
|
||||
),
|
||||
)
|
||||
..loadRequest(Uri.parse(baseUrl));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
FlutterNativeSplash.remove();
|
||||
final s = S.of(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
toolbarHeight: 0,
|
||||
),
|
||||
body: WebViewWidget(controller: _controller),
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
|
||||
Reference in New Issue
Block a user