admin管理员组

文章数量:1129794

My Code Look alike this

As we know that we can open .exe in flutter, but what i want is when user open any .exe launch it in same window instead of new or separated window.

//Here are all the dependencies
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
import 'package:flutter/material.dart';

import 'package:flutter_native_view/flutter_native_view.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterNativeView.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  final controller = ScrollController();
  final controllers = [
    NativeViewController(
      handle: FindWindow(
        nullptr,
        'CPRG'.toNativeUtf16(),
      ),
      hitTestBehavior: HitTestBehavior.translucent,
    ),
    NativeViewController(
      handle: FindWindow(
        nullptr,
        'This PC'.toNativeUtf16(),
      ),
      hitTestBehavior: HitTestBehavior.translucent,
    ),
  ];

  Future<bool> openFile(String filePath) async {
    try {
      bool fileExists = await File(filePath).exists();
      if (!fileExists) {
        print("File does not exist at: $filePath");
        return false;
      }

      if (Platform.isWindows) {
        print('Attempting to run: $filePath');
        try {
          Process process = await Process.start(
            filePath,
            [],
            mode: ProcessStartMode.detached,
            runInShell: true,
          );
          print("Process started: $process");
          return true;
        } catch (e) {
          print("Failed to start process: $e");
          return false;
        }
      } else {
        print("Opening files is not supported on this platform.");
        return false;
      }
    } catch (e) {
      print("Error opening file: $e");
      return false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: ElevatedButton(
            onPressed: () {
              openFile('D:/ThermoverseExes/Products/Combipac RG/CPRG.exe');
            },
            child: Text('opne')),
        appBar: AppBar(
          title: const Text('flutter_native_view'),
        ),
        body: ListView(
          controller: controller,
          padding: const EdgeInsets.symmetric(vertical: 16.0),
          children: [
            Padding(
              padding:
                  const EdgeInsets.symmetric(vertical: 16.0, horizontal: 32.0),
              child: NativeView(
                controller: controllers[0],
                width: 640.0,
                height: 480.0,
              ),
            ),
            Padding(
              padding:
                  const EdgeInsets.symmetric(vertical: 16.0, horizontal: 32.0),
              child: NativeView(
                controller: controllers[1],
                width: 640.0,
                height: 480.0,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

As we know that we can open .exe in flutter, but what i want is when user open any .exe launch it in same window instead of new or separated window.

本文标签: dartHow to open exe in same window in flutterStack Overflow