Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/common/actions/notes/pop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import '../../constants/constants.dart';
/// Closes the expandable FAB to add a note if it is open.
///
/// Handles both the FAB of the notes page and the one of the label pages.
void closeAddNoteFabIfOpen() {
///
/// Returns whether the FAB was open and has started to close.
bool closeAddNoteFabIfOpen() {
var closing = false;

for (final fabKey in [addNoteFabKey, labeledAddNoteFabKey]) {
if (fabKey.currentState != null && fabKey.currentState!.isOpen) {
fabKey.currentState!.toggle();
closing = true;
}
}

return closing;
}
28 changes: 23 additions & 5 deletions lib/pages/notes/widgets/add_note_fab.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_expandable_fab/flutter_expandable_fab.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -23,20 +25,36 @@ class AddNoteFab extends ConsumerStatefulWidget {
}

class _AddNoteFabState extends ConsumerState<AddNoteFab> {
/// Completed when the FAB has finished closing.
Completer<void>? closedCompleter;

void onOpen() {
canPopNotifier.update();
}

void onClose() {
canPopNotifier.update();

closedCompleter?.complete();
closedCompleter = null;
}

void onPressed(NoteType noteType) {
// Close the FAB before opening the editor, otherwise closing it during the transition removes the focus
// from the editor and closes the keyboard
closeAddNoteFabIfOpen();
Future<void> onPressed(NoteType noteType) async {
// Wait for the FAB to be fully closed before opening the editor.
// When the closing animation ends, the FAB removes its focus scope, which unfocuses the editor
// if it was focused in the meantime, and closes the keyboard.
final completer = closedCompleter = Completer<void>();
if (closeAddNoteFabIfOpen()) {
// The timeout makes sure that the note is still added if the animation could not be completed
await completer.future.timeout(const Duration(milliseconds: 500), onTimeout: () {});
}
closedCompleter = null;

if (!mounted) {
return;
}

addNote(context, ref, noteType: noteType);
await addNote(context, ref, noteType: noteType);
}

@override
Expand Down
Loading