FutureBuilder<List<CategoryColor>>(
future: GetIt.I<LocalDatabase>().getCategoryColors(),
builder: (context, snapshot) {
// selected color default to first one on start
// it can be null during loading
if (snapshot.hasData &&
selectedColorId == null &&
snapshot.data!.isNotEmpty) {
selectedColorId = snapshot.data![0].id;
}
return _ColorPicker(
colors: snapshot.hasData ? snapshot.data! : [],
selectedColorId: selectedColorId,
colorIdSetter: (int id){ // type defined by TypeDef and Function
setState(() {
selectedColorId = id;
};
},
);
},
),
// when use // get selected as bool
colors.map((e) => renderColor(e, selectedColorId == e.id)).toList()
Dart
복사
typedef ColorIdSetter = void Function(int id);
final ColorIdSetter colorIdSetter;
required this.colorIdSetter,
getDbData
.map(
(e) => GestureDetector(
onTap: () {
colorIdSetter(e.id);
},
)
Dart
복사
// DB Table Class. // if selected
Widget renderColor(CategoryColor color, bool isSelected) {
return Container();
}
Dart
복사
1.
define typedef outside the stless
2.
pass in the id inside the typedef defined in No.1, under GestureDetector
3.
pass in the function as parameter, the function must pass in the parameter stated inside the Function()

.png&blockId=627be4b9-301b-4edc-ba78-3514b1a70eeb)
.png&blockId=627be4b9-301b-4edc-ba78-3514b1a70eeb&width=256)