Мне нужно обновлять представление списка всякий раз, когда я нажимаю кнопки, я пытался использовать следующий код, но он не работает идеально. Когда я нажимаю первую кнопку, она ничего не обновляет, когда я нажимаю на вторую кнопку, онаобновляет список списком первой кнопки, а когда я нажимаю третью, он обновляет список списком второй кнопки. Это мой код
import 'package:flutter/material.dart';
import 'products_list.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
List<ProductsList> myinitlist = [];
@override
void initState() {
super.initState();
myinitlist = List.from(productList);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
child: SearchBar(),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(4.0, 4.0, 10.0, 4.0),
child: Hero(
tag: 'logo',
child: Image.asset(
'images/iglo_round.png',
height: 30.0,
width: 30.0,
),
),
),
],
),
Padding(
padding: const EdgeInsets.all(5.0),
child: Row(
children: <Widget>[
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
//i need to update this list with new items
myinitlist.removeRange(0, myinitlist.length);
getAllProductsInCategory(25504207);
myinitlist = List.from(productList);
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
//i need to update this list with new items
myinitlist.removeRange(0, myinitlist.length);
getAllProductsInCategory(25504204);
myinitlist = List.from(productList);
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
//i need to update this list with new items
myinitlist.removeRange(0, myinitlist.length);
getAllProductsInCategory(25504208);
print(myinitlist.length);
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
],
),
),
Flexible(
child: GridView.builder(itemCount: myinitlist.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount
(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index){
return Card(
child: Stack(
children: <Widget>[
GestureDetector(
onTap:(){
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Center(
child: Image.network(myinitlist[index].proThummbnail,
fit: BoxFit.contain,
),
),
flex: 2,
),
Expanded(
child: Center(child: Text(myinitlist[index].proName,
style:
TextStyle(fontSize: 10.0),)),
flex: 1,
)
],
),
)
],
),
);
}),
),
],
),
),
),
);
}
}
В файле products_list.dart содержится следующий код
List<ProductsList> productList = [];
class ProductsList{
int proId;
String proName;
String proThummbnail;
ProductsList({@required this.proId,@required this.proName,@required this.proThummbnail});
}
Future<List<ProductsList>> getAllProductsInCategory(int catid) async{
// print(categoryList[0].catId);
String prodInCategoryUrl = 'https://app.ecwid'
'.com/api/v3/12424132/products?token'
'=public_xxxxxxxxxxxx&enabled=true&category=$catid';
Response allProductsInCategory = await get(prodInCategoryUrl);
print(allProductsInCategory.statusCode);
productList.removeRange(0, productList.length);
if(allProductsInCategory.statusCode == 200)
{
var allProductsInCategoryData = allProductsInCategory.body;
int totalcount = jsonDecode(allProductsInCategoryData)['count'];
for(int i=0;i<totalcount;i++)
{
// print(jsonDecode(categoryData)['items'][i]['id']);
// print(jsonDecode(categoryData)['items'][i]['name']);
productList.add(
ProductsList(proId: jsonDecode(allProductsInCategoryData)['items'][i]['id'],
proName: jsonDecode(allProductsInCategoryData)
['items'][i]['name'], proThummbnail: jsonDecode
(allProductsInCategoryData)
['items'][i]['thumbnailUrl']
)
);
}
print('products list length ${productList.length}');
return productList;
}
return productList;;
}
Может кто-нибудь помочь мне с этим. Заранее спасибо.