1

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,),
Aakash Solanki
  • 565
  • 1
  • 6
  • 18

1 Answers1

1

The error you are getting is because you are trying to change something that was declared final

Add

class _ItemsDrawerState extends State<ItemsDrawer> {

 Item item

 @override
  void initState() {
    item = widte.item // maybe copy it... or reinit if you need
    super.initState();
  }

But if you want to update a widget from another one you might want to check this post out or this one

Durdu
  • 4,649
  • 2
  • 27
  • 47