Skip to content

Windows:When using webview, the minimize method cannot hide the webview page #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gk-1213 opened this issue Feb 6, 2025 · 0 comments

Comments

@gk-1213
Copy link

gk-1213 commented Feb 6, 2025

import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:window_manager/window_manager.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await windowManager.ensureInitialized();

  WindowOptions windowOptions = const WindowOptions(
    size: Size(800, 600),
    center: true,
    backgroundColor: Colors.transparent,
    skipTaskbar: false,
    titleBarStyle: TitleBarStyle.normal,
    windowButtonVisibility: false,
  );
  windowManager.waitUntilReadyToShow(windowOptions, () async {
    await windowManager.show();
    await windowManager.focus();
  });

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ThemeMode _themeMode = ThemeMode.light;

  InAppWebViewController? webViewController;
  String url = "";
  double progress = 0;
  final urlController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final virtualWindowFrameBuilder = VirtualWindowFrameInit();

    return MaterialApp(
        debugShowCheckedModeBanner: false,
        themeMode: _themeMode,
        builder: (context, child) {
          child = virtualWindowFrameBuilder(context, child);
          return child;
        },
        home: Scaffold(
          appBar: PreferredSize(
              preferredSize: const Size.fromHeight(40),
              child: SizedBox(
                width: 1060,
                child: Row(
                  children: [
                    const Expanded(
                      child: DragWindow(),
                    ),

                    /// 右上角按钮
                    Row(children: [
                      IconButton(
                          onPressed: () async{
                            await windowManager.minimize();
                          }, icon: const Icon(Icons.remove)),
                      IconButton(
                          onPressed: () async{
                            await windowManager.close();
                          }, icon: const Icon(Icons.close)),
                    ])
                  ],
                ),
              )),
          body: SafeArea(
              child: Column(children: <Widget>[
            TextField(
              decoration: const InputDecoration(prefixIcon: Icon(Icons.search)),
              controller: urlController,
              keyboardType: TextInputType.url,
              onSubmitted: (value) {
                var url = WebUri(value);
                if (url.scheme.isEmpty) {
                  url = WebUri("https://www.google.com/search?q=$value");
                }
                webViewController?.loadUrl(urlRequest: URLRequest(url: url));
              },
            ),
            Expanded(
              child: Stack(
                children: [
                  InAppWebView(
                    initialUrlRequest:
                        URLRequest(url: WebUri("https://inappwebview.dev/")),
                    onWebViewCreated: (controller) {
                      webViewController = controller;
                    },
                    onLoadStart: (controller, url) {
                      setState(() {
                        this.url = url.toString();
                        urlController.text = this.url;
                      });
                    },
                  ),
                  progress < 1.0
                      ? LinearProgressIndicator(value: progress)
                      : Container(),
                ],
              ),
            ),
            ButtonBar(
              alignment: MainAxisAlignment.center,
              children: <Widget>[
                ElevatedButton(
                  child: const Icon(Icons.arrow_back),
                  onPressed: () {
                    webViewController?.goBack();
                  },
                ),
                ElevatedButton(
                  child: const Icon(Icons.arrow_forward),
                  onPressed: () {
                    webViewController?.goForward();
                  },
                ),
                ElevatedButton(
                  child: const Icon(Icons.refresh),
                  onPressed: () {
                    webViewController?.reload();
                  },
                ),
              ],
            ),
          ])),
        ));
  }
}

//拖动窗口
class DragWindow extends StatelessWidget {
  const DragWindow({super.key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      child: Listener(
        onPointerDown: (event) async {
          await windowManager.startDragging();
        },
        child: Container(
          color: Colors.transparent,
        ),
      ),
    );
  }
}

After minimizing the window, the mouse cannot interact with the desktop, and the minimize method does not hide the webview page

Flutter version:3.24.3
Version of plugin used:0.4.3
Other plugins used:flutter_inappwebview: ^6.2.0-beta.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant