I want to create an ItemsDrawer Stateful Widget, The ListTile supposed to change the content of the Item screen, but it is not changing. Also, how can I use List.builder to create a List of ListTile?
Creating a method itemDrawer() works but now I want to pass the Object into the Drawer.
Drawer itemDrawer(BuildContext context) {
return Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: InkWell(
onTap: () {
setState(() {
item = items[0];
Navigator.of(context).pop();
});
},
child: Text(
'item1',
style: TextStyle(fontSize: 20),
),
),
),
ListTile(
title: InkWell(
onTap: () {
setState(() {
item= items[1];
});
Navigator.of(context).pop();
},
child: Text(
'item2',
style: TextStyle(fontSize: 20),
),
),
),
],
),
);
}
items is a List of class Item.
List<Item> items= [
item1Data,
item2Data,
item3Data,
item4Data,
item5Data,
];
Instead of a method itemDrawer(context), I want to create:
class ItemsDrawer extends StatefulWidget {
final Item item;
ItemsDrawer({Key key, this.item}) : super(key: key);
@override
_ItemsDrawer State createState() => _ItemsDrawerState();
}
class _ItemsDrawerState extends State<ItemsDrawer> {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
children: <Widget>[
Container(
decoration: BoxDecoration(),
),
ListTile(
title: InkWell(
onTap: () {
setState(() {
widget.item= items[0];
Navigator.of(context).pop();
});
},
child: Text(
'item1',
style: TextStyle(fontSize: 20),
),
),
),
.....
On widget.item it is saying that: 'item' can't be used as a setter because it is final
Update: ItemScreen where the content is shown:
class ItemScreen extends StatefulWidget {
@override
_ItemScreen State createState() => _ItemScreenState();
}
class _ItemScreentate extends State<ItemScreen> {
Item item;
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState();
@override
void initState() {
super.initState();
item = items[0];
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
backgroundColor: kcAppBackground,
appBar: AppBar(
//App Bar Title
title: modifiedAppBarTitleText(item.itemTitle),
.....
drawer: ItemDrawer(key: _scaffoldKey, item: item,),