Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

delegates - How do I get a black banner with text to dynamically show depending upon what a user does on another screen in Flutter?

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(),
     ],
    );
  }
}
question from:https://stackoverflow.com/questions/65922190/how-do-i-get-a-black-banner-with-text-to-dynamically-show-depending-upon-what-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...