Skip to content

Commit

Permalink
fix dismissible tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonataslaw committed Mar 8, 2024
1 parent 2f2d128 commit 3a4dced
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 156 deletions.
176 changes: 32 additions & 144 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,171 +1,59 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';

// void main() {
// runApp(const MyApp());
// }

// class MyApp extends StatelessWidget {
// const MyApp({Key? key}) : super(key: key);

// @override
// Widget build(BuildContext context) {
// return GetMaterialApp(
// theme: ThemeData(useMaterial3: true),
// debugShowCheckedModeBanner: false,
// enableLog: true,
// logWriterCallback: Logger.write,
// initialRoute: AppPages.INITIAL,
// getPages: AppPages.routes,
// locale: TranslationService.locale,
// fallbackLocale: TranslationService.fallbackLocale,
// translations: TranslationService(),
// );
// }
// }

/// Nav 2 snippet
void main() {
runApp(const MyApp());
runApp(MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
getPages: [
GetPage(
participatesInRootNavigator: true,
name: '/first',
page: () => const First()),
GetPage(
name: '/second',
page: () => const Second(),
transition: Transition.downToUp,
),
GetPage(
name: '/third',
page: () => const Third(),
),
],
title: 'Scaffold demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}

class FirstController extends GetxController {
@override
void onClose() {
print('on close first');
super.onClose();
}
}

class First extends StatelessWidget {
const First({Key? key}) : super(key: key);

class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('First rebuild');
Get.put(FirstController());
return Scaffold(
appBar: AppBar(
title: const Text('page one'),
leading: IconButton(
icon: const Icon(Icons.more),
title: Text('Test'),
centerTitle: true,
backgroundColor: Colors.green,
),
bottomNavigationBar: SizedBox(
width: double.infinity,
child: ElevatedButton(
child: Text('Tap me when Snackbar appears'),
onPressed: () {
Get.snackbar(
'title',
"message",
mainButton:
TextButton(onPressed: () {}, child: const Text('button')),
isDismissible: false,
);
// print('THEME CHANGED');
// Get.changeTheme(
// Get.isDarkMode ? ThemeData.light() : ThemeData.dark());
print('This should clicked');
},
),
),
body: Center(
child: SizedBox(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {
Get.toNamed('/second?id=123');
},
child: const Text('next screen'),
),
),
),
);
}
}

class SecondController extends GetxController {
final textEdit = TextEditingController();
@override
void onClose() {
print('on close second');
textEdit.dispose();
super.onClose();
}
}

class Second extends StatelessWidget {
const Second({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
final controller = Get.put(SecondController());
print('second rebuild');
return Scaffold(
appBar: AppBar(
title: Text('page two ${Get.parameters["id"]}'),
),
body: Center(
child: Column(
children: [
Expanded(
child: TextField(
controller: controller.textEdit,
)),
SizedBox(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {},
child: const Text('next screen'),
),
),
],
),
),
);
}
}

class Third extends StatelessWidget {
const Third({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(
title: const Text('page three'),
),
body: Center(
child: SizedBox(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {},
child: const Text('go to first screen'),
),
child: ElevatedButton(
child: Text('Open Snackbar'),
onPressed: () {
Get.snackbar(
"Snackbar Showed",
"Please click the button on BottomNavigationBar",
icon: Icon(Icons.check, color: Colors.green),
backgroundColor: Colors.white,
snackStyle: SnackStyle.floating,
borderRadius: 20,
isDismissible: false,
snackPosition: SnackPosition.bottom,
margin: EdgeInsets.fromLTRB(50, 15, 50, 15),
);
},
),
),
);
Expand Down
3 changes: 1 addition & 2 deletions lib/get_navigation/src/root/get_root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,12 @@ class GetRootState extends State<GetRoot> with WidgetsBindingObserver {
void onClose() {
config.onDispose?.call();
Get.clearTranslations();
config.snackBarQueue.disposeControllers();
RouterReportManager.instance.clearRouteKeys();
RouterReportManager.dispose();
Get.resetInstance(clearRouteBindings: true);
_controller = null;
ambiguate(Engine.instance)!.removeObserver(this);
config.snackBarQueue.cancelAllJobs();
config.snackBarQueue.disposeControllers();
}

@override
Expand Down
8 changes: 8 additions & 0 deletions lib/get_navigation/src/snackbar/snackbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class GetSnackBar extends StatefulWidget {
/// The title displayed to the user
final String? title;

/// Defines how the snack bar area, including margin, will behave during hit testing.
///
/// If this property is null and [margin] is not null, then [HitTestBehavior.deferToChild] is used by default.
///
/// Please refer to [HitTestBehavior] for a detailed explanation of every behavior.
final HitTestBehavior? hitTestBehavior;

/// The direction in which the SnackBar can be dismissed.
///
/// Default is [DismissDirection.down] when
Expand Down Expand Up @@ -203,6 +210,7 @@ class GetSnackBar extends StatefulWidget {
this.overlayColor = Colors.transparent,
this.userInputForm,
this.snackbarStatus,
this.hitTestBehavior,
}) : super(key: key);

@override
Expand Down
13 changes: 12 additions & 1 deletion lib/get_navigation/src/snackbar/snackbar_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class SnackbarController {
snackbar.onHover?.call(snackbar, SnackHoverState.entered),
onExit: (_) => snackbar.onHover?.call(snackbar, SnackHoverState.exited),
child: GestureDetector(
behavior: snackbar.hitTestBehavior ?? HitTestBehavior.deferToChild,
onTap: snackbar.onTap != null
? () => snackbar.onTap?.call(snackbar)
: null,
Expand All @@ -263,6 +264,7 @@ class SnackbarController {

Widget _getDismissibleSnack(Widget child) {
return Dismissible(
behavior: snackbar.hitTestBehavior ?? HitTestBehavior.opaque,
direction: snackbar.dismissDirection ?? _getDefaultDismissDirection(),
resizeDuration: null,
confirmDismiss: (_) {
Expand Down Expand Up @@ -382,10 +384,19 @@ class SnackBarQueue {
_snackbarList.clear();
}

Future<void> disposeControllers() async {
void disposeControllers() {
if (_currentSnackbar != null) {
_currentSnackbar?._removeOverlay();
_currentSnackbar?._controller.dispose();
_snackbarList.remove(_currentSnackbar);
}

_queue.cancelAllJobs();

for (var element in _snackbarList) {
element._controller.dispose();
}
_snackbarList.clear();
}

Future<void> closeCurrentJob() async {
Expand Down
20 changes: 11 additions & 9 deletions test/navigation/snackbar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ void main() {
});

testWidgets("test snackbar dismissible", (tester) async {
const dismissDirection = DismissDirection.vertical;
const dismissDirection = DismissDirection.down;
const snackBarTapTarget = Key('snackbar-tap-target');

const GetSnackBar getBar = GetSnackBar(
key: ValueKey('dismissible'),
message: 'bar1',
duration: Duration(seconds: 2),
isDismissible: true,
snackPosition: SnackPosition.bottom,
dismissDirection: dismissDirection,
);

Expand Down Expand Up @@ -149,14 +151,14 @@ void main() {
await tester.tap(find.byKey(snackBarTapTarget));
await tester.pumpAndSettle();

// expect(Get.isSnackbarOpen, true);
// await tester.pump(const Duration(milliseconds: 500));
// expect(find.byWidget(getBar), findsOneWidget);
// await tester.ensureVisible(find.byWidget(getBar));
// await tester.drag(find.byWidget(getBar), const Offset(0.0, 50.0));
// await tester.pump(const Duration(milliseconds: 500));

// expect(Get.isSnackbarOpen, false);
expect(Get.isSnackbarOpen, true);
await tester.pump(const Duration(milliseconds: 500));
expect(find.byWidget(getBar), findsOneWidget);
await tester.ensureVisible(find.byWidget(getBar));
await tester.drag(find.byType(Dismissible), const Offset(0.0, 50.0));
await tester.pumpAndSettle();
await tester.pump(const Duration(milliseconds: 500));
expect(Get.isSnackbarOpen, false);
});

testWidgets("test snackbar onTap", (tester) async {
Expand Down

0 comments on commit 3a4dced

Please sign in to comment.