I have just started to learn desktop application development with flutter. I want to show some website on my flutter app but when I press ctrl + R the page gets reload but I do not want that. Is there any way to fix this.
I took this code from chatgpt and some from the github repo of webview_windows package. While running this code I am not getting any error I am only facing this reloading problem. If anybody knows about it please fix this.
Note: I don’t have any file other than main.dart file
import 'package:flutter/material.dart';
import 'package:webview_windows/webview_windows.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: StaticWebsiteView(),
);
}
}
class StaticWebsiteView extends StatefulWidget {
@override
_StaticWebsiteViewState createState() => _StaticWebsiteViewState();
}
class _StaticWebsiteViewState extends State<StaticWebsiteView> {
final _controller = WebviewController();
bool _isWebViewInitialized = false;
@override
void initState() {
super.initState();
_initializeWebView();
}
Future<void> _initializeWebView() async {
await _controller.initialize();
await _controller.loadUrl(
'https://humanshu-jaglan.vercel.app'); // Replace with your website URL
setState(() {
_isWebViewInitialized = true;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Static Website'),
),
body: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
// Handle other gestures or interactions as needed
},
child: RawKeyboardListener(
focusNode: FocusNode(),
onKey: (RawKeyEvent event) {
if (event.isControlPressed && event.logicalKey.keyId == 82) {
// Prevent Ctrl + R from triggering a reload
return;
}
},
child: _isWebViewInitialized
? Webview(
_controller) // Pass the controller as a positional argument
: const Center(child: CircularProgressIndicator()),
),
),
);
}
}
Humanshu Jaglan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.