我要实现的是在照看这些帖子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?之后,从另一个小部件更新更改。我尝试了所有这些解决方案,但是由于我的树没有工作,经过多番思考 我正在考虑ValueListenableBuilder用于将状态从孩子传递给孩子,从孩子传递给父母,以及从父母传递给我要更新的孩子,因为InheritedWidget似乎不适合我的需要,而该窗口小部件将不断变化而且我认为我的树使使用Stream来实现我想要的东西变得很困难。我不知道这是使州从孩子到孩子,从孩子到父母以及从父母到我想要的孩子的最佳方法吗更新。this is my tree and what I am trying to achieve
ScrollableExhibitionSheet类是一个StatefulWidget; 这是cardcontent内的Raisedbutton的代码:
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);
},
),
),
这是cardcontent类的代码:
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),
],
)
],
),
);
}
}