Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update font picker to accept onChanged callback #542

Merged
merged 1 commit into from Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)],
);
}