Skip to content

Commit c64b83c

Browse files
authored
Merge pull request #1541 from LucasXu0/refactor_font_size
chore: refactor font size design
2 parents 52d65c4 + 4da786a commit c64b83c

File tree

3 files changed

+52
-74
lines changed

3 files changed

+52
-74
lines changed

frontend/app_flowy/lib/plugins/document/editor_styles.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import 'package:flutter/material.dart';
44
import 'package:provider/provider.dart';
55

66
EditorStyle customEditorTheme(BuildContext context) {
7-
final documentStyle =
8-
context.watch<DocumentAppearanceCubit>().documentAppearance;
7+
final documentStyle = context.watch<DocumentAppearanceCubit>().state;
98
var editorStyle = Theme.of(context).brightness == Brightness.dark
109
? EditorStyle.dark
1110
: EditorStyle.light;
@@ -29,8 +28,7 @@ EditorStyle customEditorTheme(BuildContext context) {
2928
}
3029

3130
Iterable<ThemeExtension<dynamic>> customPluginTheme(BuildContext context) {
32-
final documentStyle =
33-
context.watch<DocumentAppearanceCubit>().documentAppearance;
31+
final documentStyle = context.watch<DocumentAppearanceCubit>().state;
3432
final baseFontSize = documentStyle.fontSize;
3533
const basePadding = 12.0;
3634
var headingPluginStyle = Theme.of(context).brightness == Brightness.dark

frontend/app_flowy/lib/plugins/document/presentation/more/cubit/document_appearance_cubit.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ class DocumentAppearance {
2222
class DocumentAppearanceCubit extends Cubit<DocumentAppearance> {
2323
DocumentAppearanceCubit() : super(const DocumentAppearance(fontSize: 14.0));
2424

25-
late DocumentAppearance documentAppearance;
26-
2725
void fetch() async {
2826
final prefs = await SharedPreferences.getInstance();
2927
final fontSize = prefs.getDouble(_kDocumentAppearenceFontSize) ?? 14.0;
30-
documentAppearance = DocumentAppearance(fontSize: fontSize);
31-
emit(documentAppearance);
28+
emit(state.copyWith(
29+
fontSize: fontSize,
30+
));
3231
}
3332

34-
void sync(DocumentAppearance documentAppearance) async {
33+
void syncFontSize(double fontSize) async {
3534
final prefs = await SharedPreferences.getInstance();
36-
prefs.setDouble(_kDocumentAppearenceFontSize, documentAppearance.fontSize);
37-
this.documentAppearance = documentAppearance;
38-
emit(documentAppearance);
35+
prefs.setDouble(_kDocumentAppearenceFontSize, fontSize);
36+
emit(state.copyWith(
37+
fontSize: fontSize,
38+
));
3939
}
4040
}

frontend/app_flowy/lib/plugins/document/presentation/more/font_size_switcher.dart

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:app_flowy/plugins/document/presentation/more/cubit/document_appe
22
import 'package:flowy_infra_ui/style_widget/text.dart';
33
import 'package:flutter/material.dart';
44
import 'package:app_flowy/generated/locale_keys.g.dart';
5-
import 'package:provider/provider.dart';
5+
import 'package:flutter_bloc/flutter_bloc.dart';
66
import 'package:tuple/tuple.dart';
77
import 'package:easy_localization/easy_localization.dart';
88

@@ -16,74 +16,54 @@ class FontSizeSwitcher extends StatefulWidget {
1616
}
1717

1818
class _FontSizeSwitcherState extends State<FontSizeSwitcher> {
19-
final List<bool> _selectedFontSizes = [false, true, false];
20-
final List<Tuple2<String, double>> _fontSizes = [
21-
Tuple2(LocaleKeys.moreAction_small.tr(), 12.0),
22-
Tuple2(LocaleKeys.moreAction_medium.tr(), 14.0),
23-
Tuple2(LocaleKeys.moreAction_large.tr(), 18.0),
19+
final List<Tuple3<String, double, bool>> _fontSizes = [
20+
Tuple3(LocaleKeys.moreAction_small.tr(), 12.0, false),
21+
Tuple3(LocaleKeys.moreAction_medium.tr(), 14.0, true),
22+
Tuple3(LocaleKeys.moreAction_large.tr(), 18.0, false),
2423
];
2524

26-
@override
27-
void initState() {
28-
super.initState();
29-
30-
final fontSize =
31-
context.read<DocumentAppearanceCubit>().documentAppearance.fontSize;
32-
final index = _fontSizes.indexWhere((element) => element.item2 == fontSize);
33-
_updateSelectedFontSize(index);
34-
}
35-
3625
@override
3726
Widget build(BuildContext context) {
38-
return Column(
39-
crossAxisAlignment: CrossAxisAlignment.start,
40-
children: [
41-
FlowyText.semibold(
42-
LocaleKeys.moreAction_fontSize.tr(),
43-
fontSize: 12,
44-
),
45-
const SizedBox(
46-
height: 5,
47-
),
48-
ToggleButtons(
49-
isSelected: _selectedFontSizes,
50-
onPressed: (int index) {
51-
_updateSelectedFontSize(index);
52-
_sync(index);
53-
},
54-
borderRadius: const BorderRadius.all(Radius.circular(5)),
55-
selectedBorderColor: Theme.of(context).colorScheme.primaryContainer,
56-
selectedColor: Theme.of(context).colorScheme.onSurface,
57-
fillColor: Theme.of(context).colorScheme.primaryContainer,
58-
color: Theme.of(context).hintColor,
59-
constraints: const BoxConstraints(
60-
minHeight: 40.0,
61-
minWidth: 80.0,
27+
return BlocBuilder<DocumentAppearanceCubit, DocumentAppearance>(
28+
builder: (context, state) {
29+
return Column(
30+
crossAxisAlignment: CrossAxisAlignment.start,
31+
children: [
32+
FlowyText.semibold(
33+
LocaleKeys.moreAction_fontSize.tr(),
34+
fontSize: 12,
6235
),
63-
children: _fontSizes
64-
.map((e) => Text(
65-
e.item1,
66-
style: TextStyle(fontSize: e.item2),
67-
))
68-
.toList(),
69-
),
70-
],
71-
);
72-
}
73-
74-
void _updateSelectedFontSize(int index) {
75-
setState(() {
76-
for (int i = 0; i < _selectedFontSizes.length; i++) {
77-
_selectedFontSizes[i] = i == index;
78-
}
36+
const SizedBox(
37+
height: 5,
38+
),
39+
ToggleButtons(
40+
isSelected:
41+
_fontSizes.map((e) => e.item2 == state.fontSize).toList(),
42+
onPressed: (int index) {
43+
_updateSelectedFontSize(_fontSizes[index].item2);
44+
},
45+
borderRadius: const BorderRadius.all(Radius.circular(5)),
46+
selectedBorderColor: Theme.of(context).colorScheme.primaryContainer,
47+
selectedColor: Theme.of(context).colorScheme.onSurface,
48+
fillColor: Theme.of(context).colorScheme.primaryContainer,
49+
color: Theme.of(context).hintColor,
50+
constraints: const BoxConstraints(
51+
minHeight: 40.0,
52+
minWidth: 80.0,
53+
),
54+
children: _fontSizes
55+
.map((e) => Text(
56+
e.item1,
57+
style: TextStyle(fontSize: e.item2),
58+
))
59+
.toList(),
60+
),
61+
],
62+
);
7963
});
8064
}
8165

82-
void _sync(int index) {
83-
if (index < 0 || index >= _fontSizes.length) return;
84-
final fontSize = _fontSizes[index].item2;
85-
final cubit = context.read<DocumentAppearanceCubit>();
86-
final documentAppearance = cubit.documentAppearance;
87-
cubit.sync(documentAppearance.copyWith(fontSize: fontSize));
66+
void _updateSelectedFontSize(double fontSize) {
67+
context.read<DocumentAppearanceCubit>().syncFontSize(fontSize);
8868
}
8969
}

0 commit comments

Comments
 (0)