0

I am tasked with the challenge of getting a black banner to show when a user returns from a screen on that same tab upon ordering an item on another screen. The current code below only shows the black banner appropriately if the user leaves by selecting another tab then returns to the screen. The part that I need to be dynamically presented starts with "if (OrderProvider.listOrderSummary.isNotEmpty)". I believe that this involves making a stateless widget stateful, but that results in errors that suggest that may not be the right approach.

class GridItems extends StatelessWidget {
  GridItems({@required this.infinityPageController});

  final InfinityPageController infinityPageController;

  @override
  Widget build(BuildContext context) {
    final List<Item> itemList = Provider.of<List<Item>>(context);
    if (itemList == null) {
       return Center(
       child: CupertinoActivityIndicator(
         radius: 20,
       ),
    );
  }
  final List<Item> mapItemList = itemList.toList();
    return Column(
      children: <Widget>[
        TopMenuNavigation(
          infinityPageController: infinityPageController,
          title: 'ALL ITEMS',
        ),
        Divider(
          color: Colors.grey,
        ),
        Expanded(
          child: GridView.builder(
            gridDelegate:
               SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
            itemCount: itemList.length,
            itemBuilder: (BuildContext contex, int index) {
              return Center(
                child: InkWell(
                  onTap: () {
                    Navigator.of(context)
                        .pushNamed('/order', arguments: mapItemList[index]);
                  },
                  child: Container(
                    width: 310,
                    margin: EdgeInsets.all(7),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      border: Border.all(width: 1, color: Colors.grey),
                      borderRadius: BorderRadius.all(
                        Radius.circular(7.0),
                      ),
                    ),
                    child: Column(
                      children: <Widget>[
                        Container(
                          padding: EdgeInsets.only(left: 10, top: 10),
                          width: double.infinity,
                          child: Text('${mapItemList[index].itemName}'),
                        ),
                        Expanded(
                          child: Image.network(
                            '${mapItemList[index].imageUrl}',
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              );
            },
          ),
        ),
        if (OrderProvider.listOrderSummary.isNotEmpty)
          GestureDetector(
            onTap: () {
              Navigator.of(context).pushNamed('/review_order');
            },
            child: Container(
              height: 55,
              color: Colors.black,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  Spacer(),
                  Text(
                    'REVIEW ORDER (${OrderProvider.listOrderSummary.length} items)',
                    style: TextStyle(
                        fontSize: 17,
                        color: Colors.white,
                        fontFamily: 'OpenSans',
                        fontWeight: FontWeight.bold),
                  ),
                  SizedBox(width: 8),
                  Icon(
                    Icons.navigate_next,
                    color: Colors.white,
                    size: 35,
                  ),
                  SizedBox(
                    width: MediaQuery.of(context).size.height * 0.065,
                  ),
                ],
              ),
            ),
          )
        else
          SizedBox(),
     ],
    );
  }
}
michaeldebo
  • 3,065
  • 4
  • 14
  • 20

1 Answers1

0

You have to somehow rebuild your screen (or part of it) where you want to return to. This is where a state-management solution should come into place :).

Create a BLoC or a ValueNotifier and rebuild the widget with a StreamBuilder or a ValueListenableBuilder. If you want to make it simple just create a shared state inside a stateful widget between your tab screens and call setState if the black banner should appear

soft_and_ware
  • 87
  • 1
  • 8