Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
feat: update bottom sheet vs drawer logic (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
omartinma authored May 12, 2021
1 parent b60625b commit 5e0ea59
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ class MobileStickersDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
final screenHeight = MediaQuery.of(context).size.height;
return Container(
margin: const EdgeInsets.only(top: 30),
height: screenHeight < PhotoboothBreakpoints.small
? screenHeight
: screenHeight * 0.75,
decoration: const BoxDecoration(
color: PhotoboothColors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(12)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:io_photobooth/photobooth/photobooth.dart';
import 'package:io_photobooth/stickers/stickers.dart';
import 'package:photobooth_ui/photobooth_ui.dart';
import 'package:platform_helper/platform_helper.dart';

class StickersDrawerLayer extends StatelessWidget {
StickersDrawerLayer({
Key? key,
PlatformHelper? platformHelper,
}) : platformHelper = platformHelper ?? PlatformHelper(),
super(key: key);

final PlatformHelper platformHelper;
StickersDrawerLayer({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
final orientation = MediaQuery.of(context).orientation;
return BlocConsumer<StickersBloc, StickersState>(
listenWhen: (previous, current) =>
current.isDrawerActive && current != previous,
listener: (context, state) {
if (platformHelper.isMobile || orientation == Orientation.portrait) {
if (MediaQuery.of(context).size.width < PhotoboothBreakpoints.small) {
showModalBottomSheet(
context: context,
barrierColor: PhotoboothColors.black.withOpacity(0.75),
Expand All @@ -36,9 +28,10 @@ class StickersDrawerLayer extends StatelessWidget {
context.read<StickersBloc>().add(const StickersDrawerToggled());
}
},
buildWhen: (previous, current) => !isMobile && current != previous,
buildWhen: (previous, current) => current != previous,
builder: (context, state) {
if (orientation == Orientation.landscape && state.isDrawerActive)
if (MediaQuery.of(context).size.width >= PhotoboothBreakpoints.small &&
state.isDrawerActive)
return const Positioned(
right: 0,
top: 0,
Expand Down
4 changes: 4 additions & 0 deletions test/helpers/set_display_size.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ extension PhotoboothWidgetTester on WidgetTester {
void setPortraitDisplaySize() {
setDisplaySize(const Size(PhotoboothBreakpoints.small, 1000));
}

void setSmallDisplaySize() {
setDisplaySize(const Size(PhotoboothBreakpoints.small - 1, 1000));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:io_photobooth/assets.g.dart';
import 'package:io_photobooth/photobooth/photobooth.dart';
import 'package:io_photobooth/stickers/stickers.dart';
import 'package:mocktail/mocktail.dart';
import 'package:platform_helper/platform_helper.dart';

import '../../../helpers/helpers.dart';

Expand All @@ -25,8 +24,6 @@ class FakePhotoboothState extends Fake implements PhotoboothState {}
class MockPhotoboothBloc extends MockBloc<PhotoboothEvent, PhotoboothState>
implements PhotoboothBloc {}

class MockPlatformHelper extends Mock implements PlatformHelper {}

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

Expand All @@ -39,40 +36,35 @@ void main() {
group('StickersDrawerLayer', () {
late PhotoboothBloc photoboothBloc;
late StickersBloc stickersBloc;
late PlatformHelper platformHelper;

setUp(() {
photoboothBloc = MockPhotoboothBloc();
stickersBloc = MockStickersBloc();
platformHelper = MockPlatformHelper();
});

group('DesktopStickersDrawer', () {
testWidgets(
'renders DesktopStickersDrawer when '
'is not mobile, mode is active and orientation is landscape',
(tester) async {
when(() => platformHelper.isMobile).thenReturn(false);
'does not render DesktopStickersDrawer when '
'drawer is inactive', (tester) async {
when(() => stickersBloc.state).thenReturn(
StickersState(isDrawerActive: true),
StickersState(isDrawerActive: false),
);
tester.setLandscapeDisplaySize();
await tester.pumpApp(
BlocProvider.value(
value: stickersBloc,
child: Scaffold(body: Stack(children: [StickersDrawerLayer()])),
),
photoboothBloc: photoboothBloc,
);
expect(find.byType(DesktopStickersDrawer), findsOneWidget);
expect(find.byType(DesktopStickersDrawer), findsNothing);
});

testWidgets(
'does not render DesktopStickersDrawer when '
'is not mobile, and mode is inactive', (tester) async {
when(() => platformHelper.isMobile).thenReturn(false);
'renders DesktopStickersDrawer when '
'width greater than mobile breakpoint and it is drawer active',
(tester) async {
when(() => stickersBloc.state).thenReturn(
StickersState(isDrawerActive: false),
StickersState(isDrawerActive: true),
);
tester.setLandscapeDisplaySize();
await tester.pumpApp(
Expand All @@ -82,17 +74,16 @@ void main() {
),
photoboothBloc: photoboothBloc,
);
expect(find.byType(DesktopStickersDrawer), findsNothing);
expect(find.byType(DesktopStickersDrawer), findsOneWidget);
});

testWidgets(
'does not render DesktopStickersDrawer when '
'is not mobile, and orientation is portrait', (tester) async {
when(() => platformHelper.isMobile).thenReturn(false);
'width smaller than mobile and it is drawer active', (tester) async {
when(() => stickersBloc.state).thenReturn(
StickersState(isDrawerActive: true),
);
tester.setPortraitDisplaySize();
tester.setSmallDisplaySize();
await tester.pumpApp(
BlocProvider.value(
value: stickersBloc,
Expand All @@ -106,7 +97,6 @@ void main() {
testWidgets('adds StickerSelected when StickerChoice tapped',
(tester) async {
final sticker = Assets.props.first;
when(() => platformHelper.isMobile).thenReturn(false);
when(() => stickersBloc.state).thenReturn(
StickersState(isDrawerActive: true),
);
Expand Down Expand Up @@ -148,14 +138,13 @@ void main() {
group('MobileStickersDrawer', () {
testWidgets(
'opens MobileStickersDrawer when '
'is mobile, is active and is portrait', (tester) async {
when(() => platformHelper.isMobile).thenReturn(true);
'is active and width smaller than mobile breakpoint', (tester) async {
whenListen(
stickersBloc,
Stream.fromIterable([StickersState(isDrawerActive: true)]),
initialState: StickersState(isDrawerActive: false),
);
tester.setPortraitDisplaySize();
tester.setSmallDisplaySize();

await tester.pumpApp(
BlocProvider.value(value: stickersBloc, child: StickersDrawerLayer()),
Expand All @@ -169,25 +158,27 @@ void main() {
});

testWidgets(
'opens MobileStickersDrawer when '
'is not mobile and is portrait', (tester) async {
when(() => platformHelper.isMobile).thenReturn(false);
'does not open MobileStickersDrawer when '
'width greater than mobile breakpoint', (tester) async {
whenListen(
stickersBloc,
Stream.fromIterable([StickersState(isDrawerActive: true)]),
initialState: StickersState(isDrawerActive: false),
);
tester.setPortraitDisplaySize();
tester.setLandscapeDisplaySize();

await tester.pumpApp(
BlocProvider.value(value: stickersBloc, child: StickersDrawerLayer()),
BlocProvider.value(
value: stickersBloc,
child: Scaffold(body: Stack(children: [StickersDrawerLayer()])),
),
photoboothBloc: photoboothBloc,
);
await tester.pump();
expect(find.byType(MobileStickersDrawer), findsOneWidget);
expect(find.byType(MobileStickersDrawer), findsNothing);

tester.widget<IconButton>(find.byType(IconButton)).onPressed!();
await tester.pumpAndSettle();
await tester.pump();
});

testWidgets('can select stickers on MobileStickersDrawer',
Expand All @@ -200,6 +191,8 @@ void main() {
]),
initialState: StickersState(isDrawerActive: false),
);
tester.setSmallDisplaySize();

await tester.pumpApp(
BlocProvider.value(value: stickersBloc, child: StickersDrawerLayer()),
photoboothBloc: photoboothBloc,
Expand All @@ -220,6 +213,8 @@ void main() {
Stream.fromIterable([StickersState(isDrawerActive: true)]),
initialState: StickersState(isDrawerActive: false),
);
tester.setSmallDisplaySize();

await tester.pumpApp(
BlocProvider.value(
value: stickersBloc,
Expand Down

0 comments on commit 5e0ea59

Please sign in to comment.