I am trying to use flutter bloc so i create this bloc:
class CategoriesBloc extends Bloc<CategoriesEvent, CategoriesState> {
CategoriesBloc() : super(CategoriesInitial());
List<CategoriesItem> _catItems = [
CategoriesItem(id: 1, title: "Sugar", prefixIcon: Icons.access_alarms),
CategoriesItem(id: 2, title: "Calories", prefixIcon: Icons.access_alarms),
CategoriesItem(id: 3, title: "Salt", prefixIcon: Icons.access_alarms),
CategoriesItem(id: 4, title: "Fibre", prefixIcon: Icons.access_alarms),
CategoriesItem(id: 5, title: "Fat", prefixIcon: Icons.access_alarms)
];
List<CategoriesItem> get items => _catItems;
@override
Stream<CategoriesState> mapEventToState(
CategoriesEvent event,
) async* {
if (event is TopCategoriesEvent) {
yield* _makeCatList(items);
}
}
}
Stream<CategoriesState> _makeCatList(List<CategoriesItem> items) async* {
yield CategoriesStarted(items);
}
class CategoriesItem {
final int id;
final String title;
final IconData prefixIcon;
final IconData suffixIcon;
bool selected;
CategoriesItem(
{this.id,
this.title,
this.prefixIcon,
this.suffixIcon,
this.selected = false});
}
Now in main page i am using like this:
Container(
height: 80,
color: Colors.green[500],
child: BlocBuilder<CategoriesBloc, CategoriesState>(
builder: (context, state) {
if (state is CategoriesInitial) {
return Center(
child: CircularProgressIndicator(),
);
} else if (state is CategoriesStarted) {
return _makeCatItems(state.catItems);
}
return Container();
}),
and this is my _makeCatItems method:
Widget _makeCatItems(List<CategoriesItem> catItems) {
print(catItems.length);
return Container(
margin: EdgeInsets.only(top: 8.0),
child: ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: catItems
.map(
(item) => Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
InkWell(
onTap: () {},
child: Container(
width: 100,
height: 40,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
color: Colors.green[300],// change to white when tapped
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(
item.prefixIcon,
color: Colors.white,
),
Text(
item.title,
style: TextStyle(color: Colors.white),
)
],
)),
),
SizedBox(
width: 16.0,
)
],
),
)
.toList(),
),
);
}
How could i change background item color when user clicked on each item in bloc way?