Created
May 3, 2021 10:17
-
-
Save NaarGes/94689c2cd9daadfed5baeb339c407d8a to your computer and use it in GitHub Desktop.
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
import 'package:flutter/material.dart'; | |
import 'package:mamania_app/ui/shared/component/option_selector/radio_choice/radio_grid_item.dart'; | |
import 'package:mamania_app/ui/shared/component/option_selector/radio_generator/radio_grid_item_info.dart'; | |
/// [RadioImageTileGenerator<T>] | |
/// Simple usage example | |
/// List<int> options = [1, 2, 3]; | |
/// return RadioImageTileGenerator<int>( | |
/// initialSelectedOption: 3, | |
/// options: options, | |
/// contentMapper: (option) { | |
/// if (option == 1) { | |
/// return RadioImageTileInfo( | |
/// description: "a", | |
/// imagePath: Assets.alert, | |
/// selectedTextStyle: TextStyle(fontWeight: FontWeight.w900)); | |
/// } else if (option == 2) { | |
/// return RadioImageTileInfo( | |
/// description: "b", | |
/// imagePath: Assets.covidMan, | |
/// ); | |
/// } else { | |
/// return RadioImageTileInfo( | |
/// description: "c", | |
/// imagePath: Assets.heart, | |
/// ); | |
/// } | |
/// }, | |
/// onChanged: (newOption) => print("new Option is: $newOption"), | |
/// buildArrangment: (radioTiles) { | |
/// return Row( | |
/// children: radioTiles, | |
/// ); | |
/// }, | |
/// ); | |
/// } | |
class RadioGridGenerator<T> extends StatelessWidget { | |
final List<T> options; | |
final RadioGridItemInfo Function(T) contentMapper; | |
final Function(T) onChanged; | |
final Widget Function(List<Widget>) buildArrangement; | |
final T value; | |
RadioGridGenerator({ | |
@required this.options, | |
@required this.contentMapper, | |
@required this.onChanged, | |
@required this.value, | |
this.buildArrangement, | |
}); | |
Widget defaultArranger(ws) => GridView.builder( | |
shrinkWrap: true, | |
gridDelegate: | |
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2), | |
itemBuilder: (BuildContext context, int index) { | |
return ws[index]; | |
}, | |
itemCount: ws.length, | |
); | |
@override | |
Widget build(BuildContext context) { | |
return (buildArrangement ?? defaultArranger)(options.map((option) { | |
RadioGridItemInfo radioImageTileInfo = contentMapper(option); | |
return RadioGridItem( | |
value: options.indexOf(option), | |
groupValue: options.indexOf(value) ?? -1, | |
onTap: (newValue) { | |
onChanged(options[newValue]); | |
}, | |
description: radioImageTileInfo.description, | |
imagePath: radioImageTileInfo.imagePath, | |
selectedTextStyle: radioImageTileInfo.selectedTextStyle, | |
); | |
}).toList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment