Flutter i am adding data by forEach loop in an array
Code.
class _BrowserCategoryPage2State extends State<BrowserCategoryPage2> {
var items = {'Items': []};
@override
Widget build(BuildContext context) {
print('browse subcategory');
print(widget.subCategory);
widget.subCategory.forEach((subcategory) {
items['Items'].addAll(subcategory['Items']);
});
print(items);
print('sada');
return Scaffold(
appBar: buildAppBar(),
body: Container(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
buildCategoryHeading(context),
GridView.builder(
itemCount: widget.subCategory.length,
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: ScrollPhysics(),
padding: EdgeInsets.symmetric(horizontal: 18.0),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
crossAxisSpacing: 15.0,
mainAxisSpacing: 15.0,
childAspectRatio: 4.0 / 7.0,
),
itemBuilder: (context, index) {
var category = categoryList[index];
double duration = index / 2;
return FadeInAnimation(
duration.toInt(),
child: GestureDetector(
onTap: (){
print(widget.subCategory[index]['Items']);
setState(() {
var items = {'Items': []};
print(items);
});
},
child: Container(
width: 80.0,
child: Column(
children: <Widget>[
Container(
width: 60.0,
height: 60.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Theme.of(context)
.accentColor
.withOpacity(.2)),
image: DecorationImage(
image: AssetImage(
'assets/icons/shirt.png'),
),
),
),
SizedBox(height: 12.0),
Text(
widget.subCategory[index]['Name'],
textAlign: TextAlign.center,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: Theme.of(context)
.textTheme
.subtitle2,
).tr(),
],
),
)
),
);
},
),
],
),
),
),
);
}
}
Now you can see I am showing products that are coming in the Items array. Now what I need to do is onTap I need to clear all items. So after then ill insert another item so need to remove all arrays from Items.
Hope my question is understandable its simple mean I need to clear all arrays from Items when I click on GestureDetectore
items.clear()is not what you want?