From ed97e0ed796c55ae5caa04c0cc3d9304b41b3a8f Mon Sep 17 00:00:00 2001 From: Carlos Costa Date: Sun, 3 Nov 2024 10:49:59 -0300 Subject: [PATCH] Complete refactoring VWSheet to use extensions --- example/lib/examples/vw_sheet_example.dart | 19 ++++++------------- .../vw_sheet_extension.dart} | 14 +++++--------- lib/vw.dart | 4 +++- test/vw_sheet_test.dart | 9 +++------ 4 files changed, 17 insertions(+), 29 deletions(-) rename lib/{vw_sheet.dart => extensions/vw_sheet_extension.dart} (81%) diff --git a/example/lib/examples/vw_sheet_example.dart b/example/lib/examples/vw_sheet_example.dart index a0dc067..78700ef 100644 --- a/example/lib/examples/vw_sheet_example.dart +++ b/example/lib/examples/vw_sheet_example.dart @@ -22,9 +22,8 @@ class _VWSheetExampleState extends State { FilledButton( child: const Text('Open first page'), onPressed: () { - VWSheet.open( + context.openSheet( level: 1, - context: context, appBar: AppBar( title: const Text('First page'), ), @@ -32,9 +31,8 @@ class _VWSheetExampleState extends State { child: FilledButton( child: const Text('open second page'), onPressed: () { - VWSheet.open( + context.openSheet( level: 2, - context: context, appBar: AppBar( title: const Text('Second page'), ), @@ -42,9 +40,8 @@ class _VWSheetExampleState extends State { child: FilledButton( child: const Text('open third page'), onPressed: () { - VWSheet.open( + context.openSheet( level: 3, - context: context, appBar: AppBar( title: const Text('Third page'), ), @@ -52,9 +49,8 @@ class _VWSheetExampleState extends State { child: FilledButton( child: const Text('open fourth page'), onPressed: () { - VWSheet.open( + context.openSheet( level: 4, - context: context, appBar: AppBar( title: const Text('Fourth page'), ), @@ -62,9 +58,8 @@ class _VWSheetExampleState extends State { child: FilledButton( child: const Text('open fifth page'), onPressed: () { - VWSheet.open( + context.openSheet( level: 5, - context: context, appBar: AppBar( title: const Text('Fifth page'), ), @@ -92,9 +87,7 @@ class _VWSheetExampleState extends State { ), FilledButton( onPressed: () { - VWSheet.open( - borderRadius: 28, - context: context, + context.openSheet( appBar: AppBar( leading: IconButton( icon: const Icon(Icons.close), diff --git a/lib/vw_sheet.dart b/lib/extensions/vw_sheet_extension.dart similarity index 81% rename from lib/vw_sheet.dart rename to lib/extensions/vw_sheet_extension.dart index 865199f..06485eb 100644 --- a/lib/vw_sheet.dart +++ b/lib/extensions/vw_sheet_extension.dart @@ -1,13 +1,10 @@ import 'package:flutter/material.dart'; -sealed class VWSheet { - static Future open({ +extension BottomSheetExtension on BuildContext { + openSheet({ ///[appBar] is the header of the page. required AppBar appBar, - ///[context] is the BuildContext of the page. - required BuildContext context, - ///[bottomBar] is the bottom bar of the page. Widget? bottomBar, @@ -26,11 +23,11 @@ sealed class VWSheet { ///[borderColor] is the border color of the page. bool isScrollControlled = true, }) { - return showModalBottomSheet( + showModalBottomSheet( useSafeArea: true, backgroundColor: Colors.transparent, isScrollControlled: isScrollControlled, - context: context, + context: this, builder: (context) { return Container( height: MediaQuery.of(context).size.height * 0.96 - (15 * level), @@ -42,8 +39,7 @@ sealed class VWSheet { ), ), child: Scaffold( - backgroundColor: - backgroundColor ?? Theme.of(context).colorScheme.surface, + backgroundColor: backgroundColor ?? Theme.of(context).colorScheme.surface, appBar: appBar, body: body, bottomNavigationBar: bottomBar, diff --git a/lib/vw.dart b/lib/vw.dart index 68a63bc..d9a9440 100644 --- a/lib/vw.dart +++ b/lib/vw.dart @@ -1,7 +1,6 @@ export './vw_column.dart'; export './vw_row.dart'; export './vw_button.dart'; -export './vw_sheet.dart'; export './vw_timepicker.dart'; export './vw_select.dart'; export './vw_modal.dart'; @@ -9,5 +8,8 @@ export './vw_box.dart'; export './vw_reveal.dart'; export './vw_text.dart'; export './vw_if.dart'; + +// Extensions export './extensions/vw_screen_size_extension.dart'; export './extensions/vw_text_extension.dart'; +export './extensions/vw_sheet_extension.dart'; diff --git a/test/vw_sheet_test.dart b/test/vw_sheet_test.dart index 5a77d77..651f347 100644 --- a/test/vw_sheet_test.dart +++ b/test/vw_sheet_test.dart @@ -13,11 +13,10 @@ void main() { return Center( child: FilledButton( onPressed: () { - VWSheet.open( + context.openSheet( appBar: AppBar( title: const Text('VW Sheet header'), ), - context: context, body: const Center( child: Text('VW Sheet body'), ), @@ -59,9 +58,8 @@ void main() { return FilledButton( child: const Text('Open first page'), onPressed: () { - VWSheet.open( + context.openSheet( level: 1, - context: context, appBar: AppBar( title: const Text('First page'), actions: [ @@ -77,9 +75,8 @@ void main() { child: FilledButton( child: const Text('open second page'), onPressed: () { - VWSheet.open( + context.openSheet( level: 2, - context: context, appBar: AppBar( title: const Text('Second page'), actions: [