Skip to content

Commit

Permalink
refactor: update font picker to accept onChanged callback
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshuaro committed Dec 14, 2022
1 parent 8ac0a02 commit fd71365
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
6 changes: 4 additions & 2 deletions lib/font/widgets/font_picker.dart
Expand Up @@ -5,7 +5,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class FontPicker extends StatelessWidget {
const FontPicker({Key? key}) : super(key: key);
const FontPicker({Key? key, required this.onChanged}) : super(key: key);

final ValueChanged<FontData> onChanged;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -46,7 +48,7 @@ class FontPicker extends StatelessWidget {

void _onChanged(BuildContext context, FontData? data) {
if (data != null) {
context.read<TextThemeCubit>().fontFamilyChanged(data.family);
onChanged(data);
}
}
}
6 changes: 3 additions & 3 deletions lib/text_theme/cubits/text_theme_cubit.dart
@@ -1,8 +1,8 @@
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/text_theme/text_theme.dart';

part 'text_theme_cubit.g.dart';
part 'text_theme_state.dart';
Expand Down Expand Up @@ -76,7 +76,7 @@ class TextThemeCubit extends Cubit<TextThemeState> {
overlineTextStyleCubit.styleChanged(theme.overline);
}

void fontFamilyChanged(String fontFamily) {
emit(state.copyWith(fontFamily: fontFamily));
void fontFamilyChanged(FontData data) {
emit(state.copyWith(fontFamily: data.family));
}
}
5 changes: 4 additions & 1 deletion lib/text_theme/view/text_theme_editor.dart
Expand Up @@ -3,6 +3,7 @@ import 'package:appainter/font/font.dart';
import 'package:appainter/text_theme/text_theme.dart';
import 'package:appainter/widgets/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class TextThemeEditor extends ExpansionPanelItem {
const TextThemeEditor({Key? key}) : super(key: key);
Expand All @@ -14,7 +15,9 @@ class TextThemeEditor extends ExpansionPanelItem {
Widget build(BuildContext context) {
return NestedListView(
children: [
const FontPicker(),
FontPicker(
onChanged: context.read<TextThemeCubit>().fontFamilyChanged,
),
MyExpansionPanelList(
color: Theme.of(context).brightness == Brightness.dark
? Colors.grey[700]
Expand Down
10 changes: 7 additions & 3 deletions test/text_theme/font_picker_test.dart
Expand Up @@ -4,6 +4,7 @@ import 'package:bloc_test/bloc_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:mocktail/mocktail.dart';

import '../mocks.dart';
Expand All @@ -17,6 +18,7 @@ void main() {

testWidgets('font picker should update font family', (tester) async {
const fontFamily = 'ABeeZee';
final style = GoogleFonts.aBeeZee();

whenListen(
cubit,
Expand All @@ -29,9 +31,11 @@ void main() {
create: (context) => FontRepository(),
child: BlocProvider.value(
value: cubit,
child: const MaterialApp(
child: MaterialApp(
home: Scaffold(
body: FontPicker(),
body: FontPicker(
onChanged: cubit.fontFamilyChanged,
),
),
),
),
Expand All @@ -44,6 +48,6 @@ void main() {
await tester.pumpAndSettle();

expect(find.text(fontFamily), findsOneWidget);
verify(() => cubit.fontFamilyChanged(fontFamily));
verify(() => cubit.fontFamilyChanged(FontData(fontFamily, style)));
});
}
9 changes: 6 additions & 3 deletions test/text_theme/text_theme_cubit_test.dart
@@ -1,12 +1,13 @@
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:appainter/text_theme/text_theme.dart';
import 'package:mocktail/mocktail.dart';

import '../mocks.dart';

void main() {
const fontFamily = 'fontFamily';
late TextThemeCubit textThemeCubit;
late TextTheme textTheme;

Expand Down Expand Up @@ -166,7 +167,9 @@ void main() {
blocTest<TextThemeCubit, TextThemeState>(
'should emit font family',
build: () => textThemeCubit,
act: (cubit) => cubit.fontFamilyChanged('newFont'),
expect: () => [const TextThemeState(fontFamily: 'newFont')],
act: (cubit) => cubit.fontFamilyChanged(
const FontData(fontFamily, TextStyle()),
),
expect: () => [const TextThemeState(fontFamily: fontFamily)],
);
}

0 comments on commit fd71365

Please sign in to comment.