@@ -5,6 +5,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
55import '../../../common/actions/notes/add.dart' ;
66import '../../../common/constants/constants.dart' ;
77import '../../../models/note/note.dart' ;
8+ import '../../../providers/preferences/preferences_provider.dart' ;
89
910/// Floating action button to add a note.
1011class AddNoteFab extends ConsumerStatefulWidget {
@@ -18,50 +19,78 @@ class AddNoteFab extends ConsumerStatefulWidget {
1819class _AddNoteFabState extends ConsumerState <AddNoteFab > {
1920 final fabKey = GlobalKey <ExpandableFabState >();
2021
21- void onPressed <NoteType >() {
22- final fabState = fabKey.currentState;
22+ void onFocusChange (bool hasFocus) {
23+ if (! hasFocus) {
24+ final fabState = fabKey.currentState;
25+
26+ if (fabState == null ) {
27+ return ;
28+ }
2329
24- if (fabState == null ) {
25- return ;
30+ if (fabState.isOpen) {
31+ fabState.toggle ();
32+ }
2633 }
34+ }
2735
36+ void onPressed <NoteType >() {
2837 addNote <NoteType >(context, ref);
29- if (fabState.isOpen) {
30- fabState.toggle ();
38+
39+ if (fabKey.currentState != null && fabKey.currentState! .isOpen) {
40+ fabKey.currentState! .toggle ();
3141 }
3242 }
3343
3444 @override
3545 Widget build (BuildContext context) {
36- return ExpandableFab (
37- key: fabKey,
38- type: ExpandableFabType .up,
39- childrenAnimation: ExpandableFabAnimation .none,
40- distance: 75 ,
41- openButtonBuilder: RotateFloatingActionButtonBuilder (
42- heroTag: '<open add note FAB hero tag>' ,
43- child: const Icon (Icons .add),
44- ),
45- closeButtonBuilder: RotateFloatingActionButtonBuilder (
46- heroTag: '<close add note FAB hero tag>' ,
47- child: const Icon (Icons .close),
48- ),
49- children: [
50- FloatingActionButton .extended (
51- heroTag: '<add plain text note hero tag>' ,
52- tooltip: l.tooltip_fab_add_plain_text_note,
53- onPressed: onPressed< PlainTextNote > ,
54- icon: const Icon (Icons .text_fields),
55- label: Text ('Plain text' ),
56- ),
57- FloatingActionButton .extended (
58- heroTag: '<add rich text note hero tag>' ,
59- tooltip: l.tooltip_fab_add_rich_text_note,
60- onPressed: onPressed< RichTextNote > ,
61- icon: const Icon (Icons .format_paint),
62- label: Text ('Rich text' ),
63- ),
64- ],
46+ final availableNotesTypes = ref.watch (
47+ preferencesProvider.select ((preferences) => preferences.availableNotesTypes),
6548 );
49+
50+ return availableNotesTypes.length == 1
51+ ? FloatingActionButton (
52+ tooltip: l.tooltip_fab_add_note,
53+ onPressed: switch (availableNotesTypes.first) {
54+ == PlainTextNote => onPressed< PlainTextNote > ,
55+ == RichTextNote => onPressed< RichTextNote > ,
56+ _ => throw Exception ('Unknown note type when adding a note: ${availableNotesTypes .first }' ),
57+ },
58+ child: const Icon (Icons .add),
59+ )
60+ : Focus (
61+ onFocusChange: onFocusChange,
62+ child: ExpandableFab (
63+ key: fabKey,
64+ type: ExpandableFabType .up,
65+ childrenAnimation: ExpandableFabAnimation .none,
66+ distance: 75 ,
67+ openButtonBuilder: RotateFloatingActionButtonBuilder (
68+ heroTag: '<open add note FAB hero tag>' ,
69+ child: const Icon (Icons .add),
70+ ),
71+ closeButtonBuilder: RotateFloatingActionButtonBuilder (
72+ heroTag: '<close add note FAB hero tag>' ,
73+ child: const Icon (Icons .close),
74+ ),
75+ children: [
76+ if (availableNotesTypes.contains (PlainTextNote ))
77+ FloatingActionButton .extended (
78+ heroTag: '<add plain text note hero tag>' ,
79+ tooltip: l.tooltip_fab_add_plain_text_note,
80+ onPressed: onPressed< PlainTextNote > ,
81+ icon: const Icon (Icons .text_fields),
82+ label: Text ('Plain text' ),
83+ ),
84+ if (availableNotesTypes.contains (RichTextNote ))
85+ FloatingActionButton .extended (
86+ heroTag: '<add rich text note hero tag>' ,
87+ tooltip: l.tooltip_fab_add_rich_text_note,
88+ onPressed: onPressed< RichTextNote > ,
89+ icon: const Icon (Icons .format_paint),
90+ label: Text ('Rich text' ),
91+ ),
92+ ],
93+ ),
94+ );
6695 }
6796}
0 commit comments