Fix backspace delete

This commit is contained in:
2025-04-21 23:33:15 +02:00
parent 1051805463
commit ad767ac33a

View File

@@ -38,9 +38,6 @@ void main() async {
class JournalerApp extends StatelessWidget { class JournalerApp extends StatelessWidget {
const JournalerApp({super.key}); const JournalerApp({super.key});
static final GlobalKey<MainPageState> _mainPageKey =
GlobalKey<MainPageState>();
static final TextTheme _baseTextTheme = const TextTheme( static final TextTheme _baseTextTheme = const TextTheme(
bodyMedium: TextStyle(fontSize: 24), bodyMedium: TextStyle(fontSize: 24),
); );
@@ -114,30 +111,7 @@ class JournalerApp extends StatelessWidget {
theme: lightTheme, theme: lightTheme,
darkTheme: darkTheme, darkTheme: darkTheme,
themeMode: ThemeMode.system, themeMode: ThemeMode.system,
home: Focus( home: const MainPage(),
// Using RawKeyboardListener despite deprecation because KeyboardListener
// and Shortcuts/Actions didn't reliably capture the Escape key press
// before the TextField handled it for unfocusing, requiring two presses.
// RawKeyboardListener intercepts the event earlier.
child: RawKeyboardListener(
// Revert to RawKeyboardListener
focusNode: FocusNode(),
onKey: (RawKeyEvent event) {
// Revert to onKey and RawKeyEvent
if (event is RawKeyDownEvent && // Revert to RawKeyDownEvent
event.logicalKey == LogicalKeyboardKey.escape) {
debugPrint("Escape pressed (RawKeyboardListener - Workaround)");
final state = _mainPageKey.currentState;
if (state != null) {
// Re-add unfocus, as it seemed necessary with Raw listener
FocusManager.instance.primaryFocus?.unfocus();
state.onWindowClose();
}
}
},
child: MainPage(key: _mainPageKey),
),
),
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
); );
} }
@@ -286,66 +260,81 @@ class MainPageState extends State<MainPage> with WindowListener {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( // Wrap Scaffold with RawKeyboardListener as workaround for Escape key
appBar: AppBar(title: const Text('Journaler'), actions: const []), return RawKeyboardListener(
body: Padding( focusNode: FocusNode(), // Needs its own node
padding: const EdgeInsets.all(8.0), onKey: (RawKeyEvent event) {
child: Row( if (event is RawKeyDownEvent &&
crossAxisAlignment: CrossAxisAlignment.stretch, event.logicalKey == LogicalKeyboardKey.escape) {
children: [ debugPrint(
Expanded( "Escape pressed inside MainPage (RawKeyboardListener - Workaround)",
flex: 9, );
child: Column( // Call method directly since we are in the state
crossAxisAlignment: CrossAxisAlignment.stretch, FocusManager.instance.primaryFocus?.unfocus(); // Keep unfocus attempt
children: [ onWindowClose();
Expanded( }
child: TextField( },
controller: _previousEntryController, child: Scaffold(
maxLines: null, appBar: AppBar(title: const Text('Journaler'), actions: const []),
expands: true, body: Padding(
style: Theme.of(context).textTheme.bodyMedium, padding: const EdgeInsets.all(8.0),
decoration: const InputDecoration( child: Row(
labelText: 'Previous Entry', crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 9,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: TextField(
controller: _previousEntryController,
maxLines: null,
expands: true,
style: Theme.of(context).textTheme.bodyMedium,
decoration: const InputDecoration(
labelText: 'Previous Entry',
),
), ),
), ),
), const SizedBox(height: 8),
const SizedBox(height: 8), Expanded(
Expanded( child: TextField(
child: TextField( controller: _currentEntryController,
controller: _currentEntryController, focusNode: _currentEntryFocusNode,
focusNode: _currentEntryFocusNode, maxLines: null,
maxLines: null, expands: true,
expands: true, autofocus: true,
autofocus: true, style: Theme.of(context).textTheme.bodyMedium,
style: Theme.of(context).textTheme.bodyMedium, decoration: const InputDecoration(
decoration: const InputDecoration( labelText: 'Current Entry (What\'s on your mind?)',
labelText: 'Current Entry (What\'s on your mind?)', ),
onChanged: (text) {},
), ),
onChanged: (text) {},
), ),
), ],
],
),
),
const SizedBox(width: 8),
Expanded(
flex: 3,
child: TextField(
controller: _todoController,
maxLines: null,
expands: true,
style:
Theme.of(
context,
).textTheme.bodyMedium, // Apply theme text style
decoration: const InputDecoration(
labelText: 'Todo',
// border: OutlineInputBorder(), // Handled by theme
// contentPadding: EdgeInsets.all(8.0), // Handled by theme or default
), ),
), ),
), const SizedBox(width: 8),
], Expanded(
flex: 3,
child: TextField(
controller: _todoController,
maxLines: null,
expands: true,
style:
Theme.of(
context,
).textTheme.bodyMedium, // Apply theme text style
decoration: const InputDecoration(
labelText: 'Todo',
// border: OutlineInputBorder(), // Handled by theme
// contentPadding: EdgeInsets.all(8.0), // Handled by theme or default
),
),
),
],
),
), ),
), ),
); );