首页 > 解决方案 > 当使用颤振中的提供程序包在第三屏发生任何变化时,如何更改第一屏中的文本?

问题描述

我有一个名为包括有状态小部件的第一屏幕,PlantFeatureScreen1当更新发生在第三屏幕中时,我必须在其中显示更改,该屏幕还包括一个有状态小部件,CartDetais3.

当第三个屏幕发生更改addQuantity并且subtractQuantity功能将添加到第三个屏幕时,我如何更新第一个屏幕?请建议使用提供程序包。

firstScreen 代码在这里,我必须显示这个小部件树的最后一个中心小部件的变化。

class PlantFeatureScreen1 extends StatefulWidget {
  @override
  _PlantFeatureScreen1State createState() => _PlantFeatureScreen1State();
}

class _PlantFeatureScreen1State extends State<PlantFeatureScreen1> {


  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        TopAppBar(),
        Expanded(
          flex: 1,
          child: Align(
            alignment: Alignment(-1, 0),
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
              ),
              child: Text(
                "Plants",
                style: TextStyle(fontSize: 30, fontWeight: FontWeight.w700),
              ),
            ),
          ),
        ),
        Expanded(
          flex: 5,
          child: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.blue,
            ),
            child: DefaultTabController(
              length: 5,
              child: Column(
                children: [
                  Container(
                    height: 50,
                    width: double.infinity,
                    child: TabBar(
                      isScrollable: true,
                      tabs: ourAllLists.tabMaker(),
                    ),
                  ),
                  Container(
                    height: 317,
                    width: double.infinity,
                    decoration: BoxDecoration(color: Colors.white),
                    child: TabBarView(
                      children: ourAllLists.tabViewerMaker(context),),),
                ],
              ),
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
          child: Container(
            alignment: Alignment.bottomRight,
            height: 120,
            width: double.infinity,
            child: Stack(
              overflow: Overflow.visible,
              children: [
                Container(
                  height: 70,
                  width: 105,
                  decoration: BoxDecoration(
                      color: Color(0xFF96CA2D),
                      borderRadius: BorderRadiusDirectional.horizontal(
                          end: Radius.circular(32),
                          start: Radius.circular(32))),
                          child: Icon(FontAwesomeIcons.shoppingBag,color:Colors.white,size:30),
                ),
                Positioned(
                  // top: 0,
                  bottom: 50,
                  right: 0,
                  child: Container(
                    height: 35,
                    width: 35,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(50),
                      border: Border.all(color: Color(0xFF96CA2D),width: 4)
                    ),
                    child: Center(child: Text(ourAllLists.totalquantity().toString(),style:TextStyle(fontSize: 20,color: Color(0xFF96CA2D)))),
                  ),
                )
              ],
            ),
          ),
        )
      ],
    );
  }
}

thirdScreen 代码在这里,当单击 + 或 - 图标时会发生更新。

class CartDetais3 extends StatefulWidget {
  @override
  _CartDetais3State createState() => _CartDetais3State();
}

class _CartDetais3State extends State<CartDetais3> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: SafeArea(
            child: Scaffold(
      body: Padding(
        padding: const EdgeInsets.fromLTRB(8, 20, 8, 15),
        child: Column(
          children: [
            TopAppBar(),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "Cart",
                  style: kPlantNameStyle,
                ),
                Text(
                  "\$" + "284",
                  style: kItemPrice,
                ),
              ],
            ),
            Expanded(
              child: ListView.builder(
                  itemCount: OurAllLists.cartPlantList3.length,
                  itemBuilder: (context, index) {
                    return Card(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            height: 110,
                            width: 100,
                            child: Image(image: AssetImage("assets/tulip.png")),
                          ),
                          Container(
                            height: 80,
                            width: 120,
                            child: Column(
                              children: [
                                Text(OurAllLists.cartPlantList3[index].pN),
                                Text(OurAllLists.cartPlantList3[index].ca
                                    .toUpperCase()),
                                Text("\$ " +
                                    OurAllLists.cartPlantList3[index].pr
                                        .toString()),
                              ],
                            ),
                          ),
                          Container(
                              height: 120,
                              child: Column(
                                children: [
                                  FlatButton(
                                    onPressed: () {
                                      setState(() {
                                        *here addQuantity function must go*
                                      });
                                    },
                                    child: Icon(
                                      FontAwesomeIcons.plus,
                                      size: 20,
                                    ),
                                  ),
                                  Text(OurAllLists.cartPlantList3[index].qu
                                      .toString()),
                                  FlatButton(
                                    onPressed: () {
                                      *here subtractQuantity function must go*
                                    },
                                    child: Icon(
                                      FontAwesomeIcons.minus,
                                      size: 20,
                                    ),
                                  ),
                                ],
                              ))
                        ],
                      ),
                    );
                  }),
            )
          ],
        ),
      ),
    )));
  }
}

我还有一个单独的班级,叫做ViewTotallItemProvider

class ViewTotalItemProvider extends ChangeNotifier{
  addQuantity(index){
    OurAllLists.cartPlantList3[index].qu++;
    notifyListeners();
  }

  subtrachQuantity(index){
    OurAllLists.cartPlantList3[index].qu--;

  }
}

标签: flutterstatestate-management

解决方案


推荐阅读