@@ -2,7 +2,7 @@ import 'package:app_flowy/plugins/document/presentation/more/cubit/document_appe
22import 'package:flowy_infra_ui/style_widget/text.dart' ;
33import 'package:flutter/material.dart' ;
44import 'package:app_flowy/generated/locale_keys.g.dart' ;
5- import 'package:provider/provider .dart' ;
5+ import 'package:flutter_bloc/flutter_bloc .dart' ;
66import 'package:tuple/tuple.dart' ;
77import 'package:easy_localization/easy_localization.dart' ;
88
@@ -16,74 +16,54 @@ class FontSizeSwitcher extends StatefulWidget {
1616}
1717
1818class _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