У меня есть список данных с идентификатором:
[
{
"id": 1,
"title": "close",
},
{
"id": 2,
"title": "car",
},
Я показываю этот список в виде списка с горизонтальной осью:
Column(
children: <Widget>[
Container(
height: 50.0,
color: Colors.white,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: _categories.data.length,
itemBuilder: (_, index) {
return Container(
child: InkWell(
onTap: () {
setState(() {
_selectedCat = _categories.data[index].id;
});
},
child: Container(
color: (index + 1) ==
_categories.pagination.currentPage
? Colors.red
: Colors.white,
margin: EdgeInsets.all(8.0),
child: Text(
_categories.data[index].title,
style: TextStyle(
fontSize: 16.0, fontWeight: FontWeight.bold),
),
),
),
);
},
),
),
Expanded(child: CategoryWidget(selectedCat: _selectedCat))
],
),
В CategoryWidget я показываю элементы, связанные с выше данные:
class CategoryWidget extends StatefulWidget {
final int id;
const CategoryWidget({@required this.id});
@override
_CategoryWidgetState createState() => _CategoryWidgetState();
}
class _CategoryWidgetState extends State<CategoryWidget> {
Future _future;
@override
void initState() {
_future = HomeProvider().getItems(widget.id); // not triggered
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder<ItemModel>(
future: _futureCategory,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.data.length,
itemBuilder: (_, index) {
return Text(snapshot.data.data[index].name);
});
},
),
);
}
}
Но когда я приклеил к каждому горизонтальному элементу:
onTap: () {
setState(() {
_selectedCat = _categories.data[index].id;
});
}
И состояние установки выполнено, initState
в CategoryWidget
не сработало?