From a1e54ba828ded633d1d7c164e2e6bb2bc6753953 Mon Sep 17 00:00:00 2001 From: Joshua Tang Date: Thu, 15 Dec 2022 22:23:04 +1100 Subject: [PATCH] feat: add font family support to app bar theme --- .../cubit/abstract_text_style_cubit.dart | 12 +++-- .../view/abstract_text_style_editor.dart | 4 +- .../abstract_text_style_cubit_test.dart | 48 ++++++++++++------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/lib/abstract_text_style/cubit/abstract_text_style_cubit.dart b/lib/abstract_text_style/cubit/abstract_text_style_cubit.dart index 019347d8..84163771 100644 --- a/lib/abstract_text_style/cubit/abstract_text_style_cubit.dart +++ b/lib/abstract_text_style/cubit/abstract_text_style_cubit.dart @@ -1,10 +1,11 @@ +import 'package:appainter/common/common.dart'; +import 'package:appainter/models/models.dart'; +import 'package:appainter/services/services.dart'; +import 'package:appainter/text_theme/text_theme.dart'; import 'package:bloc/bloc.dart'; import 'package:copy_with_extension/copy_with_extension.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter/material.dart'; -import 'package:appainter/common/common.dart'; -import 'package:appainter/models/models.dart'; -import 'package:appainter/services/services.dart'; part 'abstract_text_style_cubit.g.dart'; part 'text_style_state.dart'; @@ -148,4 +149,9 @@ abstract class AbstractTextStyleCubit extends Cubit { emit(state.copyWith(style: style)); } } + + void fontFamilyChanged(FontData data) { + final style = state.style.merge(data.style); + emit(state.copyWith(style: style)); + } } diff --git a/lib/abstract_text_style/view/abstract_text_style_editor.dart b/lib/abstract_text_style/view/abstract_text_style_editor.dart index fa43407c..e8d6ab50 100644 --- a/lib/abstract_text_style/view/abstract_text_style_editor.dart +++ b/lib/abstract_text_style/view/abstract_text_style_editor.dart @@ -1,10 +1,11 @@ import 'package:appainter/abstract_text_style/abstract_text_style.dart'; import 'package:appainter/common/common.dart'; +import 'package:appainter/font/font.dart'; import 'package:appainter/models/models.dart'; import 'package:appainter/services/services.dart'; +import 'package:appainter/widgets/widgets.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:appainter/widgets/widgets.dart'; abstract class AbstractTextStyleEditor extends ExpansionPanelItem { @@ -20,6 +21,7 @@ abstract class AbstractTextStyleEditor return SideBySideList( padding: kPaddingAll, children: [ + FontPicker(onChanged: cubit.fontFamilyChanged), ColorListTile( key: const Key('colorPicker'), title: 'Color', diff --git a/test/abstract_text_style/abstract_text_style_cubit_test.dart b/test/abstract_text_style/abstract_text_style_cubit_test.dart index b995ce1a..37340510 100644 --- a/test/abstract_text_style/abstract_text_style_cubit_test.dart +++ b/test/abstract_text_style/abstract_text_style_cubit_test.dart @@ -4,14 +4,19 @@ import 'package:appainter/abstract_text_style/abstract_text_style.dart'; import 'package:appainter/common/common.dart'; import 'package:appainter/models/models.dart'; import 'package:appainter/services/services.dart'; +import 'package:appainter/text_theme/text_theme.dart'; import 'package:bloc_test/bloc_test.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:google_fonts/google_fonts.dart'; import '../utils.dart'; import 'mocks.dart'; void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + final fontData = FontData('aBeeZee', GoogleFonts.aBeeZee()); + late TestTextStyleCubit cubit; late TextStyle style; late Color color; @@ -30,7 +35,7 @@ void main() { group('test style brightness', () { for (var isDark in [true, false]) { blocTest( - 'should emit style with isDark=$isDark', + 'emit style with isDark=$isDark', build: () => cubit, act: (cubit) => cubit.styleBrightnessChanged(isDark), expect: () => [ @@ -45,21 +50,21 @@ void main() { }); blocTest( - 'should emit style', + 'emit style', build: () => cubit, act: (cubit) => cubit.styleChanged(style), expect: () => [TextStyleState(style: style)], ); blocTest( - 'should emit color', + 'emit color', build: () => cubit, act: (cubit) => cubit.colorChanged(color), expect: () => [TextStyleState(style: style.copyWith(color: color))], ); blocTest( - 'should emit background color', + 'emit background color', build: () => cubit, act: (cubit) => cubit.backgroundColorChanged(color), expect: () => [ @@ -68,7 +73,7 @@ void main() { ); blocTest( - 'should emit font size', + 'emit font size', build: () => cubit, act: (cubit) => cubit.fontSizeChanged(doubleStr), expect: () => [ @@ -79,7 +84,7 @@ void main() { group('test font weights', () { for (var weight in MyFontWeight().values) { blocTest( - 'should emit $weight', + 'emit $weight', build: () => cubit, act: (cubit) { cubit.fontWeightChanged(MyFontWeight().convertToString(weight)!); @@ -94,7 +99,7 @@ void main() { group('test font styles', () { for (var fontStyle in FontStyle.values) { blocTest( - 'should emit $fontStyle', + 'emit $fontStyle', build: () => cubit, act: (cubit) { cubit.fontStyleChanged(UtilService.enumToString(fontStyle)); @@ -107,7 +112,7 @@ void main() { }); blocTest( - 'should emit letter spacing', + 'emit letter spacing', build: () => cubit, act: (cubit) => cubit.letterSpacingChanged(doubleStr), expect: () => [ @@ -116,7 +121,7 @@ void main() { ); blocTest( - 'should emit word spacing', + 'emit word spacing', build: () => cubit, act: (cubit) => cubit.wordSpacingChanged(doubleStr), expect: () => [ @@ -127,7 +132,7 @@ void main() { group('test text baselines', () { for (var baseline in TextBaseline.values) { blocTest( - 'should emit $baseline', + 'emit $baseline', build: () => cubit, act: (cubit) { cubit.textBaselineChanged(UtilService.enumToString(baseline)); @@ -140,7 +145,7 @@ void main() { }); blocTest( - 'should emit height', + 'emit height', build: () => cubit, act: (cubit) => cubit.heightChanged(doubleStr), expect: () => [ @@ -151,7 +156,7 @@ void main() { group('test leading distributions', () { for (var dist in TextLeadingDistribution.values) { blocTest( - 'should emit $dist', + 'emit $dist', build: () => cubit, act: (cubit) { cubit.leadingDistributionChanged(UtilService.enumToString(dist)); @@ -166,7 +171,7 @@ void main() { group('test decorations', () { for (var decoration in MyTextDecoration().values) { blocTest( - 'should emit $decoration', + 'emit $decoration', build: () => cubit, act: (cubit) { cubit.decorationChanged( @@ -181,7 +186,7 @@ void main() { }); blocTest( - 'should emit decoration color', + 'emit decoration color', build: () => cubit, act: (cubit) => cubit.decorationColorChanged(color), expect: () => [ @@ -191,7 +196,7 @@ void main() { group('text decoration styles', () { blocTest( - 'should emit null', + 'emit null', build: () => cubit, act: (cubit) => cubit.decorationStyleChanged(kNone), expect: () => [ @@ -201,7 +206,7 @@ void main() { for (var decorationStyle in TextDecorationStyle.values) { blocTest( - 'should emit $decorationStyle', + 'emit $decorationStyle', build: () => cubit, act: (cubit) { cubit.decorationStyleChanged( @@ -218,11 +223,20 @@ void main() { }); blocTest( - 'should emit decoration thickness', + 'emit decoration thickness', build: () => cubit, act: (cubit) => cubit.decorationThicknessChanged(doubleStr), expect: () => [ TextStyleState(style: style.copyWith(decorationThickness: doubleNum)), ], ); + + blocTest( + 'emit font family', + build: () => cubit, + act: (cubit) => cubit.fontFamilyChanged(fontData), + expect: () => [ + TextStyleState(style: style.merge(fontData.style)), + ], + ); }