fix drag&drop for web
This commit is contained in:
parent
bba42586f3
commit
2ab73b1dc4
|
|
@ -1,12 +1,33 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
|
||||
import 'package:window_manager/window_manager.dart';
|
||||
|
||||
import 'src/app.dart';
|
||||
import 'src/settings/settings_controller.dart';
|
||||
import 'src/settings/settings_service.dart';
|
||||
|
||||
void main(List<String> args) async {
|
||||
// Identify if Desktop or Mobile app.
|
||||
|
||||
if (!kIsWeb) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await windowManager.ensureInitialized();
|
||||
|
||||
WindowOptions windowOptions = const WindowOptions(
|
||||
title: 'Fast Log Viewer',
|
||||
center: true,
|
||||
);
|
||||
|
||||
windowManager.waitUntilReadyToShow(windowOptions, () async {
|
||||
await windowManager.show();
|
||||
await windowManager.focus();
|
||||
});
|
||||
}
|
||||
|
||||
// Set up the SettingsController, which will glue user settings to multiple
|
||||
// Flutter Widgets.
|
||||
final settingsController = SettingsController(SettingsService());
|
||||
|
|
|
|||
119
lib/src/app.dart
119
lib/src/app.dart
|
|
@ -72,11 +72,13 @@ class LogViewerScreen extends StatefulWidget {
|
|||
class LogViewerScreenState extends State<LogViewerScreen> {
|
||||
List<LogEntry> logs = [];
|
||||
List<LogEntry> visibleLogs = [];
|
||||
String search = '';
|
||||
LogLevel selectedLogLevel = LogLevel.info;
|
||||
bool _isDragging = false;
|
||||
|
||||
TextEditingController controller = TextEditingController(text: '');
|
||||
|
||||
late SettingsController settingsController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
|
@ -93,9 +95,49 @@ class LogViewerScreenState extends State<LogViewerScreen> {
|
|||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Fast Log Viewer'),
|
||||
actions: [
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: controller,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Search Logs',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_filterLogs();
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
DropdownButton<LogLevel>(
|
||||
underline: Container(height: 0),
|
||||
alignment: Alignment.center,
|
||||
value: selectedLogLevel,
|
||||
onChanged: (LogLevel? newValue) {
|
||||
if (newValue != null) {
|
||||
setState(() {
|
||||
selectedLogLevel = newValue;
|
||||
_filterLogs();
|
||||
});
|
||||
}
|
||||
},
|
||||
items: LogLevel.values
|
||||
.map<DropdownMenuItem<LogLevel>>((LogLevel level) {
|
||||
return DropdownMenuItem<LogLevel>(
|
||||
value: level,
|
||||
child: Text(level.name),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.folder_open),
|
||||
onPressed: _openFile,
|
||||
),
|
||||
DropdownButton<ThemeMode>(
|
||||
underline: Container(height: 0),
|
||||
alignment: Alignment.center,
|
||||
value: settingsController.themeMode,
|
||||
onChanged: (ThemeMode? newValue) async {
|
||||
if (newValue != null) {
|
||||
|
|
@ -120,10 +162,7 @@ class LogViewerScreenState extends State<LogViewerScreen> {
|
|||
),
|
||||
],
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.folder_open),
|
||||
onPressed: _openFile,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
),
|
||||
body: DropTarget(
|
||||
|
|
@ -139,10 +178,10 @@ class LogViewerScreenState extends State<LogViewerScreen> {
|
|||
},
|
||||
onDragDone: (details) async {
|
||||
if (details.files.isNotEmpty) {
|
||||
File file = File(details.files.first.path);
|
||||
String fileContent = await file.readAsString();
|
||||
var bytes = await details.files.first.readAsBytes();
|
||||
setState(() {
|
||||
logs = _parseLogFile(fileContent);
|
||||
logs = _parseLogFile(String.fromCharCodes(bytes));
|
||||
controller.clear();
|
||||
_filterLogs();
|
||||
_isDragging = false;
|
||||
});
|
||||
|
|
@ -154,46 +193,6 @@ class LogViewerScreenState extends State<LogViewerScreen> {
|
|||
: Theme.of(context).scaffoldBackgroundColor,
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Search Logs',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
search = value;
|
||||
_filterLogs();
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8.0),
|
||||
DropdownButton<LogLevel>(
|
||||
value: selectedLogLevel,
|
||||
onChanged: (LogLevel? newValue) {
|
||||
if (newValue != null) {
|
||||
setState(() {
|
||||
selectedLogLevel = newValue;
|
||||
_filterLogs();
|
||||
});
|
||||
}
|
||||
},
|
||||
items: LogLevel.values
|
||||
.map<DropdownMenuItem<LogLevel>>((LogLevel level) {
|
||||
return DropdownMenuItem<LogLevel>(
|
||||
value: level,
|
||||
child: Text(level.name),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: SelectionArea(
|
||||
child: ListView.builder(
|
||||
|
|
@ -215,14 +214,19 @@ class LogViewerScreenState extends State<LogViewerScreen> {
|
|||
}
|
||||
|
||||
Future<void> _openFile() async {
|
||||
FilePickerResult? result = await FilePicker.platform.pickFiles();
|
||||
FilePickerResult? result =
|
||||
await FilePicker.platform.pickFiles(withData: true);
|
||||
if (result != null) {
|
||||
File file = File(result.files.single.path!);
|
||||
String fileContent = await file.readAsString();
|
||||
setState(() {
|
||||
logs = _parseLogFile(fileContent);
|
||||
_filterLogs();
|
||||
});
|
||||
if (result.files.isNotEmpty) {
|
||||
var bytes = result.files.first.bytes;
|
||||
if (bytes != null) {
|
||||
setState(() {
|
||||
logs = _parseLogFile(String.fromCharCodes(bytes));
|
||||
controller.clear();
|
||||
_filterLogs();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -230,7 +234,8 @@ class LogViewerScreenState extends State<LogViewerScreen> {
|
|||
setState(() {
|
||||
visibleLogs = logs
|
||||
.where((log) =>
|
||||
log.level.index >= selectedLogLevel.index && log.contains(search))
|
||||
log.level.index >= selectedLogLevel.index &&
|
||||
log.contains(controller.text))
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,9 +7,17 @@
|
|||
#include "generated_plugin_registrant.h"
|
||||
|
||||
#include <desktop_drop/desktop_drop_plugin.h>
|
||||
#include <screen_retriever/screen_retriever_plugin.h>
|
||||
#include <window_manager/window_manager_plugin.h>
|
||||
|
||||
void fl_register_plugins(FlPluginRegistry* registry) {
|
||||
g_autoptr(FlPluginRegistrar) desktop_drop_registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "DesktopDropPlugin");
|
||||
desktop_drop_plugin_register_with_registrar(desktop_drop_registrar);
|
||||
g_autoptr(FlPluginRegistrar) screen_retriever_registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "ScreenRetrieverPlugin");
|
||||
screen_retriever_plugin_register_with_registrar(screen_retriever_registrar);
|
||||
g_autoptr(FlPluginRegistrar) window_manager_registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "WindowManagerPlugin");
|
||||
window_manager_plugin_register_with_registrar(window_manager_registrar);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
list(APPEND FLUTTER_PLUGIN_LIST
|
||||
desktop_drop
|
||||
screen_retriever
|
||||
window_manager
|
||||
)
|
||||
|
||||
list(APPEND FLUTTER_FFI_PLUGIN_LIST
|
||||
|
|
|
|||
|
|
@ -6,9 +6,13 @@ import FlutterMacOS
|
|||
import Foundation
|
||||
|
||||
import desktop_drop
|
||||
import screen_retriever
|
||||
import shared_preferences_foundation
|
||||
import window_manager
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
DesktopDropPlugin.register(with: registry.registrar(forPlugin: "DesktopDropPlugin"))
|
||||
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
|
||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||
WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin"))
|
||||
}
|
||||
|
|
|
|||
16
pubspec.lock
16
pubspec.lock
|
|
@ -237,6 +237,14 @@ packages:
|
|||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.1.8"
|
||||
screen_retriever:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: screen_retriever
|
||||
sha256: "6ee02c8a1158e6dae7ca430da79436e3b1c9563c8cf02f524af997c201ac2b90"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.1.9"
|
||||
shared_preferences:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
|
@ -378,6 +386,14 @@ packages:
|
|||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.5.5"
|
||||
window_manager:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: window_manager
|
||||
sha256: ab8b2a7f97543d3db2b506c9d875e637149d48ee0c6a5cb5f5fd6e0dac463792
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.4.2"
|
||||
xdg_directories:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ dependencies:
|
|||
file_picker:
|
||||
desktop_drop:
|
||||
shared_preferences:
|
||||
window_manager:
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
|
|
|||
|
|
@ -7,8 +7,14 @@
|
|||
#include "generated_plugin_registrant.h"
|
||||
|
||||
#include <desktop_drop/desktop_drop_plugin.h>
|
||||
#include <screen_retriever/screen_retriever_plugin.h>
|
||||
#include <window_manager/window_manager_plugin.h>
|
||||
|
||||
void RegisterPlugins(flutter::PluginRegistry* registry) {
|
||||
DesktopDropPluginRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("DesktopDropPlugin"));
|
||||
ScreenRetrieverPluginRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("ScreenRetrieverPlugin"));
|
||||
WindowManagerPluginRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
list(APPEND FLUTTER_PLUGIN_LIST
|
||||
desktop_drop
|
||||
screen_retriever
|
||||
window_manager
|
||||
)
|
||||
|
||||
list(APPEND FLUTTER_FFI_PLUGIN_LIST
|
||||
|
|
|
|||
Loading…
Reference in New Issue