From 8906421969f122f8762b1857bb8e67505926b5e6 Mon Sep 17 00:00:00 2001 From: burakJs Date: Sun, 12 Jan 2025 08:00:30 +0300 Subject: [PATCH] feat: implement custom widget with builder param --- ios/Podfile.lock | 2 +- .../core/custom_selection_demo_view.dart | 38 ++++++++++++++++- .../core/custom_selection_sheet.dart | 41 +++++++++++-------- module/codegen/android/local.properties | 4 +- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 50a8cef..857d496 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -100,4 +100,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 4387b88d4dfddf360388490fa92ca628d37f317c -COCOAPODS: 1.15.2 +COCOAPODS: 1.16.2 diff --git a/lib/use_case/selection/core/custom_selection_demo_view.dart b/lib/use_case/selection/core/custom_selection_demo_view.dart index e44436f..316c833 100644 --- a/lib/use_case/selection/core/custom_selection_demo_view.dart +++ b/lib/use_case/selection/core/custom_selection_demo_view.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:kartal/kartal.dart'; import 'package:use_case_flutter/use_case/selection/core/custom_selection_sheet.dart'; import 'package:use_case_flutter/use_case/selection/core/model/product_model.dart'; import 'package:use_case_flutter/use_case/selection/core/model/product_model_factory.dart'; @@ -25,7 +26,12 @@ class _CustomSelectionDemoViewState extends State { final response = await CustomSelectionSheet.show( context, items: ProductModelFactory.shopItems().items, - selected: _productModelNotifier.value, + builder: (BuildContext context, ProductModel item) { + return _SelectionItem( + productModel: item, + isSelected: _productModelNotifier.value == item, + ); + }, ); _productModelNotifier.value = response; }, @@ -40,3 +46,33 @@ class _CustomSelectionDemoViewState extends State { ); } } + +/// A widget that displays a selection item. +/// [isSelected] is used to determine if the selection is selected. +/// [productModel] is the product model to display. +final class _SelectionItem extends StatelessWidget { + const _SelectionItem({ + required this.productModel, + required this.isSelected, + }); + + final ProductModel productModel; + final bool isSelected; + + @override + Widget build(BuildContext context) { + return ListTile( + onTap: () { + Navigator.pop(context, productModel); + }, + title: Text(productModel.name), + subtitle: Text(productModel.price.toString()).ext.toVisible( + value: productModel.price > 100, + ), + leading: Icon( + Icons.check_circle, + color: isSelected ? Colors.green : Colors.grey, + ), + ); + } +} diff --git a/lib/use_case/selection/core/custom_selection_sheet.dart b/lib/use_case/selection/core/custom_selection_sheet.dart index 5fe3713..961b53c 100644 --- a/lib/use_case/selection/core/custom_selection_sheet.dart +++ b/lib/use_case/selection/core/custom_selection_sheet.dart @@ -2,13 +2,19 @@ import 'package:flutter/material.dart'; import 'package:kartal/kartal.dart'; import 'package:use_case_flutter/use_case/selection/core/custom_selection_abstract.dart'; +/// A builder that builds a widget for a given item. +typedef SelectionWidgetBuilder = Widget Function( + BuildContext context, + T item, +); + /// A sheet that allows the user to select a custom selection. /// [isSelected] is used to determine if the selection is selected. final class CustomSelectionSheet extends StatelessWidget { const CustomSelectionSheet._({ required this.items, - required this.selected, + required this.builder, super.key, }); @@ -16,19 +22,19 @@ final class CustomSelectionSheet static Future show( BuildContext context, { required List items, - required T? selected, + required SelectionWidgetBuilder builder, }) { return showModalBottomSheet( context: context, builder: (context) => CustomSelectionSheet._( items: items, - selected: selected, + builder: builder, ), ); } final List items; - final T? selected; + final SelectionWidgetBuilder builder; @override Widget build(BuildContext context) { @@ -41,16 +47,17 @@ final class CustomSelectionSheet shrinkWrap: true, itemBuilder: (BuildContext context, int index) { final selection = items[index]; - return ListTile( - onTap: () { - Navigator.pop(context, selection); - }, - title: Text(selection.name), - leading: Icon( - Icons.check_circle, - color: selected == selection ? Colors.green : Colors.grey, - ), - ); + return builder(context, selection); + // return ListTile( + // onTap: () { + // Navigator.pop(context, selection); + // }, + // title: Text(selection.name), + // leading: Icon( + // Icons.check_circle, + // color: selected == selection ? Colors.green : Colors.grey, + // ), + // ); }, ), ], @@ -58,10 +65,8 @@ final class CustomSelectionSheet } } -class _Notch extends StatelessWidget { - const _Notch({ - super.key, - }); +final class _Notch extends StatelessWidget { + const _Notch(); @override Widget build(BuildContext context) { diff --git a/module/codegen/android/local.properties b/module/codegen/android/local.properties index 9b7877e..5946268 100644 --- a/module/codegen/android/local.properties +++ b/module/codegen/android/local.properties @@ -1,2 +1,2 @@ -sdk.dir=/Users/vb10/Library/Android/sdk -flutter.sdk=/Users/vb10/development/flutter \ No newline at end of file +sdk.dir=/Users/burak/Library/Android/sdk +flutter.sdk=/Users/burak/fvm/versions/stable \ No newline at end of file