1、新建状态管理类
class CategoryState with ChangeNotifier{
List<BxMallSubDto> _titleList = [];
get titleList => _titleList;
// 接口方法
void updateRightTitles(List<BxMallSubDto> titleList) {
_titleList = titleList;
notifyListeners();
}
}
2、注册状态管理类
void main() {
return runApp(
MultiProvider(
providers:[
ChangeNotifierProvider(create: (context)=>CategoryState())
],
child: WineShop(),
)
);
}
3、状态组件绑定
child: Consumer<CategoryState>(
builder: (context, categoryState, child){
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categoryState.titleList.length,
itemBuilder: (context, index){
return buildListViewCell(categoryState.titleList[index]);
}
);
}
)
4、状态变更
onTap: (){
// 更新状态管理数据
List<BxMallSubDto> titles = categories[index].bxMallSubDto;
Provider.of<CategoryState>(context, listen: false).updateRightTitles(titles);
setState(() {
selectedIndex = index;
});
}
Tips:listen这个参数好像要传值false,否则没效果,具体请参考Stack Overflow