0

What I am trying to achieve is to update change from another widget after looking after these posts Trigger a function from a widget to a State object, How to set state from another widget?, How to force Flutter to rebuild / redraw all widgets?.I tried all these solutions but because of my tree its did not worked and after many reflections I am thinking of ValueListenableBuilder using to pass the State from child to child, child to parent, and parent to the child I want to update because InheritedWidget seems to don't fit my need more over the widget will be constantly change and I think my tree make things difficult to use Stream to achieve what I want.I don't know if it's the best way to do that passing the State from child to child, child to parent, and parent to the child I want to update .this is my tree and what I am trying to achieve

The ScrollableExhibitionSheet class is a StatefulWidget; this is the code of the Raisedbutton inside the cardcontent:

   Transform.translate(
            offset: Offset(48 * widget.offset, 0),
            child: RaisedButton(
              color: Colors.green, //Color(0xFF162A49),
              child: Transform.translate(
                offset: Offset(24 * widget.offset, 0),
                child: Text('Reserve'),
              ),
              textColor: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(32),
              ),
              onPressed: ()async {
                SharedPreferences preferences = await SharedPreferences.getInstance();
                String stringValue = preferences.getString('token');
                print("Token $stringValue");
                Dio dio = Dio(
                    BaseOptions(
                      baseUrl:base,
                      method: "POST",
                      headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json',
                        'Authorization': 'Token $stringValue',
                      },
                    ));
               response4 = await dio.post(
                    "/orders/",
                    data: {
                      "product":widget.id,
                      "paiment_status": false,
                      "timing_paiement": false,
                      "reserve_status": true,
                      "description": "",
                      "price":widget.price,
                      "number":_counter,
                      "relaypoint":1
                    });
               setState(() {
                 print(response4.statusCode);
                 //ScrollableExhibitionSheet();
               });
               if(_counter !=0){
                 if(response4.statusCode == 200 || response4.statusCode == 201){
                   Flushbar(
                       flushbarPosition: FlushbarPosition.TOP,
                       title: "XXXXXXXXXX",
                       message:"${widget.name} X${_counter}",
                       duration: Duration(seconds: 3)
                   )
                     ..show(context);
                 }
                 setState(()async{
                   _counter =0;
                   print(response4.statusCode);
                   getDataFromUi();
                   //prices(_counter,widget.price);
                   //sum = sum + (_counter *widget.price);
                 });

               }else{
                 Flushbar(
                     flushbarPosition: FlushbarPosition.TOP,
                     title: "XXXXXXXXX",
                     message:"xxxxxxxxxxxxxxxx",
                     duration: Duration(seconds: 3)
                 )
                   ..show(context);
               }

                //ApiData.add_orders(context,widget.id,widget.price,widget.name,_counter);
              },
            ),
          ),

and this is the code of the cardcontent class:

   class CardContent extends StatefulWidget {
     final int id;
     final String name;
     final String date;
     final int price;
     final double offset;
     Function callback;
     CardContent(
       {Key key,
        this.callback,
         @required this.id,
         @required this.name,
         @required this.date,
         @required this.price,
         @required this.offset})
         : super(key: key);

  @override
_CardContentState createState() => _CardContentState();
}

 class _CardContentState extends State<CardContent> {
    num _counter = 0;
    num _defaultValue = 0;
   Response response4;
  //Count2Bloc count1Bloc;
   final number = new ValueNotifier(0);


 getDataFromUi() async {
   await ApiData.getDataorders();
  }

@override
Widget build(BuildContext context) {
   void add() {
  setState(() {
    _counter++;
  });
}
//bool loading = false;

void minus() {
  setState(() {
    if (_counter != 0)
      _counter--;
  });
}


return Padding(
  padding: const EdgeInsets.all(8.0),
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      Transform.translate(
        offset: Offset(8 * widget.offset, 0),
        child: Text(widget.name, style: TextStyle(fontSize: 20),
          overflow: TextOverflow.ellipsis,
          maxLines:1,
        ),
      ),
      SizedBox(height: 6),
      Transform.translate(
        offset: Offset(32 * widget.offset, 0),
        child: Text(
          widget.date,
          style: TextStyle(color: Colors.grey,fontSize: 12),
          overflow: TextOverflow.ellipsis,
          maxLines:1,
        ),
      ),
      Spacer(),
      new Row(
        mainAxisAlignment: MainAxisAlignment.end,
        // crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[

          Transform.translate(
            offset: Offset(32 * widget.offset, 0),
            child:GestureDetector(
              onTap: minus,
              child: new Icon(
                  const IconData(0xe15b, fontFamily: 'MaterialIcons'),
                  color: Colors.black,size: 30,),
            ),
          ),

          SizedBox(width: 5,),
          Transform.translate(
            offset: Offset(32 * widget.offset, 0),
            child:new Text('$_counter',
                style: new TextStyle(fontSize: 14.0)
            ),
          ),

          SizedBox(width: 5,),
          Transform.translate(
            offset: Offset(32 * widget.offset, 0),
            child:GestureDetector(
                onTap: add,
                child: new Icon(Icons.add, color: Colors.black,size: 30)
            ),
          ),
          SizedBox(width: 16,)
        ],
      ),
      Spacer(),
      Row(
        children: <Widget>[
          Transform.translate(
            offset: Offset(48 * widget.offset, 0),
            child: RaisedButton(
              color: Colors.green, //Color(0xFF162A49),
              child: Transform.translate(
                offset: Offset(24 * widget.offset, 0),
                child: Text('Reserve'),
              ),
              textColor: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(32),
              ),
              onPressed: ()async {
                SharedPreferences preferences = await SharedPreferences.getInstance();
                String stringValue = preferences.getString('token');
                print("Token $stringValue");
                Dio dio = Dio(
                    BaseOptions(
                      baseUrl:base, //"http://digixpress.herokuapp.com/api/",
                      method: "POST",
                      headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json',
                        'Authorization': 'Token $stringValue',
                      },
                    ));
               response4 = await dio.post(
                    "/orders/",
                    data: {
                      "product":widget.id,
                      "paiment_status": false,
                      "timing_paiement": false,
                      "reserve_status": true,
                      "description": "",
                      "price":widget.price,
                      "number":_counter,
                      "relaypoint":1
                    });
               setState(() {
                 print(response4.statusCode);
                 //ScrollableExhibitionSheet();
               });
               if(_counter !=0){
                 if(response4.statusCode == 200 || response4.statusCode == 201){
                   Flushbar(
                       flushbarPosition: FlushbarPosition.TOP,
                       title: "Panier",
                       message:"${widget.name} X${_counter}",
                       duration: Duration(seconds: 3)
                   )
                     ..show(context);
                 }
                 setState(()async{
                   _counter =0;
                   print(response4.statusCode);
                   getDataFromUi();
                   //prices(_counter,widget.price);
                   //sum = sum + (_counter *widget.price);
                 });

               }else{
                 Flushbar(
                     flushbarPosition: FlushbarPosition.TOP,
                     title: "Aucune commande",
                     message:"renseignez le nombre de plats/boissons",
                     duration: Duration(seconds: 3)
                 )
                   ..show(context);
               }

                //ApiData.add_orders(context,widget.id,widget.price,widget.name,_counter);
              },
            ),
          ),
          Spacer(),
          Transform.translate(
            offset: Offset(32 * widget.offset, 0),
            child: Text(
              '${widget.price} Fcfa',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 15,
              ),
            ),
          ),
          SizedBox(width:16),
        ],
      )
    ],
  ),
);
   }
  }
sdegras
  • 89
  • 1
  • 12
  • Scoped Model and Bloc helped me a lot to solve my problem – sdegras Apr 15 '20 at 17:55
  • [Google I/O 2018 build reactive mobile app](https://www.youtube.com/watch?v=RS36gBEp8OI) I found my solution among all the solution proposed in this video. – sdegras Apr 15 '20 at 17:59
  • I thoroughly recommend having a look at "Pragmatic State management in Flutter" from Google I/O 2019 indeed: https://www.youtube.com/watch?v=d_m5csmrf7I – brickpop Sep 28 '20 at 13:33

0 Answers0