Created
February 6, 2024 06:07
-
-
Save hansbak/4aed8c6bdbc3de70710a1935ef05f6f4 to your computer and use it in GitHub Desktop.
define the provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in your startup main.dart file | |
RestClient(await buildDioClient()) | |
RepositoryProvider(create: (context) => restClient, child: .... | |
// The class preceding the statefull class with the dropdown search | |
class AssetDialog extends StatelessWidget { | |
final Asset asset; | |
const AssetDialog(this.asset, {super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return BlocProvider( | |
create: (BuildContext context) => | |
ProductBloc(context.read<RestClient>(), context.read<String>()) | |
..add(const ProductFetch(isForDropDown: true)), | |
child: AssetDialogFull(asset), | |
); | |
} | |
} | |
// part of the statefull widget | |
BlocBuilder<ProductBloc, ProductState>(builder: (context, state) { | |
switch (state.status) { | |
case ProductStatus.failure: | |
return const FatalErrorForm( | |
message: 'server connection problem'); | |
case ProductStatus.success: | |
return DropdownSearch<Product>( | |
key: const Key('productDropDown'), | |
selectedItem: _selectedProduct, | |
popupProps: PopupProps.menu( | |
showSearchBox: true, | |
searchFieldProps: TextFieldProps( | |
autofocus: true, | |
decoration: InputDecoration( | |
labelText: 'Product id'), | |
controller: _productSearchBoxController, | |
), | |
title: 'Product', | |
), | |
dropdownDecoratorProps: DropDownDecoratorProps( | |
dropdownSearchDecoration: InputDecoration( | |
labelText: classificationId == 'AppHotel' | |
? 'Room Type' | |
: 'Product', | |
), | |
), | |
itemAsString: (Product? u) => | |
" ${u!.productName}", // invisible char for test | |
onChanged: (Product? newValue) { | |
_selectedProduct = newValue; | |
}, | |
asyncItems: (String filter) { | |
_productBloc.add(ProductFetch(searchString: filter)); | |
return Future.value(state.products); | |
}, | |
validator: (value) => | |
value == null ? 'field required' : null, | |
); | |
default: | |
return const Center(child: CircularProgressIndicator()); | |
} | |
}), | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment