Dart 颤振-柱上的设置状态don#x27;行不通
我会尽力解释 我有一个名为createList的方法:(此方法只需创建列表并返回一个包含此列表的列) 显示一个setState工作的按钮,但我真正的构建类在哪里Dart 颤振-柱上的设置状态don#x27;行不通,dart,widget,flutter,setstate,Dart,Widget,Flutter,Setstate,我会尽力解释 我有一个名为createList的方法:(此方法只需创建列表并返回一个包含此列表的列) 显示一个setState工作的按钮,但我真正的构建类在哪里 class RelatorioDasVendasTotsProdutos extends StatefulWidget { @override RelatorioDasVendasTotsProdutosState createState() => RelatorioDasVendasTotsProdutosState()
class RelatorioDasVendasTotsProdutos extends StatefulWidget {
@override
RelatorioDasVendasTotsProdutosState createState() => RelatorioDasVendasTotsProdutosState();
}
class RelatorioDasVendasTotsProdutosState extends State<RelatorioDasVendasTotsProdutos> {
@override
Widget build(BuildContext context) {
double height = (MediaQuery.of(context).size.height) - kRadialReactionRadius - kToolbarHeight;
return new Container(
height: height,
padding: new EdgeInsets.all(10.0),
child: new Card(
elevation: 10.0,
child:
new Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Column(crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new ListTile(
title: new Center(child: new Text('VENDAS POR PRODUTOS', style: new TextStyle(fontWeight: FontWeight.bold))),
subtitle: new Center(child: new Padding(padding: new EdgeInsets.only(top: 5.0), child:
new Text('Top 3 produtos dos ultimos 30 dias', textAlign: TextAlign.center,))),
),
new FutureBuilder<List<RelatorioProdutos>>(
future: fetchRelatorioPorProduto(new http.Client(),'$dateInicial', '$dateFinal'),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error)
;else
auxTelaDetalhada = RelatorioVendasTotalizadasProdutos(prods: snapshot.data, aondeVoltar: 0,);
return snapshot.hasData
? new porProdutoList(listas: snapshot.data)
: new Center(child: new RefreshProgressIndicator());
},
),
],),
new Column(crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
new Padding(padding: new EdgeInsets.only(bottom: 15.0),
child: new RaisedButton(
child: new Text("Ver Relatório Completo",
style: new TextStyle(color: Colors.white, fontSize: 20.0),),
color: Colors.blue,
elevation: 8.0,
splashColor: Colors.lightBlue,
onPressed: () {
setState(() {
});
// Navigator.pushReplacement(
// context,
// new MaterialPageRoute(builder: (context) => auxTelaDetalhada),
// );
},
),
),
]),]
),
),
);
}
}
类relatoriodasvendastostsprodutos扩展StatefulWidget{
@凌驾
RelatorioDasVendasTotsProdutosState createState()=>RelatorioDasVendasTotsProdutosState();
}
类RelatorioDasVendasTotsProdutosState扩展状态{
@凌驾
小部件构建(构建上下文){
double height=(MediaQuery.of(context.size.height)-kRadialReactionRadius-kToolbarHeight;
退回新货柜(
高度:高度,,
填充:新边缘设置。全部(10.0),
孩子:新卡(
标高:10.0,
儿童:
新专栏(
mainAxisSize:mainAxisSize.max,
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
新列(crossAxisAlignment:crossAxisAlignment.start,
儿童:[
新ListTile(
标题:新中心(子项:新文本('VENDAS POR PRODUTOS',样式:新文本样式(fontWeight:fontWeight.bold)),
副标题:新中心(子项:新填充(填充:新边集)。仅限(顶部:5.0),子项:
新文本('Top3 produtos dos ultimos 30 dias',textAlign:textAlign.center,),
),
新未来建设者(
future:FetchRelatoriPorProduto(新的http.Client(),“$dateInicial”,“$dateFinal”),
生成器:(上下文,快照){
if(snapshot.hasError)打印(snapshot.error)
其他的
auxTelaDetalhada=relatorivendastotalizadasprodutos(产品:snapshot.data,aondevertar:0,);
返回snapshot.hasData
?新的porProdutoList(列表:snapshot.data)
:新中心(子项:新刷新进度指示器());
},
),
],),
新列(crossAxisAlignment:crossAxisAlignment.end,
儿童:[
新衬垫(衬垫:仅限新边套件(底部:15.0),
孩子:新升起的按钮(
儿童:新文本(“关系复杂”,
样式:新的文本样式(颜色:Colors.white,fontSize:20.0),
颜色:颜色,蓝色,
标高:8.0,
颜色:颜色。浅蓝色,
已按下:(){
设置状态(){
});
//导航器。更换(
//背景,
//新材料路线(建筑商:(上下文)=>auxTelaDetalhada),
// );
},
),
),
]),]
),
),
);
}
}
在另一个地方,当按下最后一个按钮时,它会工作,但我想在第一个if中输入if(在其他类中)重建它。
新列(…)
在设置状态((){…})
看起来很奇怪,你可能不想这样做<代码>设置状态()只能用于更新状态。这将导致再次执行build()
,并在build()
中根据状态实例化小部件。按下:(){setState((){build(context);});},像这样吗?按钮仍然不起作用。不,不要自己调用build,当您调用setState()
时,flift会为您调用build。在setState()
中,仅修改状态(小部件字段的值)。哇,我在另一个按钮上只做了setState(),它工作了,我得到了重新排序的屏幕,但是如果出现错误,我可以用这个setState属性来确定按钮是否出现了错误?因为它在实际方法的其他按钮上工作
if(widget.listas.length == 1){
return new Text(
widget.listas[0].Descricao,
textAlign: TextAlign.center,
style: new TextStyle(color: Colors.red));
}
class RelatorioDasVendasTotsProdutos extends StatefulWidget {
@override
RelatorioDasVendasTotsProdutosState createState() => RelatorioDasVendasTotsProdutosState();
}
class RelatorioDasVendasTotsProdutosState extends State<RelatorioDasVendasTotsProdutos> {
@override
Widget build(BuildContext context) {
double height = (MediaQuery.of(context).size.height) - kRadialReactionRadius - kToolbarHeight;
return new Container(
height: height,
padding: new EdgeInsets.all(10.0),
child: new Card(
elevation: 10.0,
child:
new Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Column(crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new ListTile(
title: new Center(child: new Text('VENDAS POR PRODUTOS', style: new TextStyle(fontWeight: FontWeight.bold))),
subtitle: new Center(child: new Padding(padding: new EdgeInsets.only(top: 5.0), child:
new Text('Top 3 produtos dos ultimos 30 dias', textAlign: TextAlign.center,))),
),
new FutureBuilder<List<RelatorioProdutos>>(
future: fetchRelatorioPorProduto(new http.Client(),'$dateInicial', '$dateFinal'),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error)
;else
auxTelaDetalhada = RelatorioVendasTotalizadasProdutos(prods: snapshot.data, aondeVoltar: 0,);
return snapshot.hasData
? new porProdutoList(listas: snapshot.data)
: new Center(child: new RefreshProgressIndicator());
},
),
],),
new Column(crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
new Padding(padding: new EdgeInsets.only(bottom: 15.0),
child: new RaisedButton(
child: new Text("Ver Relatório Completo",
style: new TextStyle(color: Colors.white, fontSize: 20.0),),
color: Colors.blue,
elevation: 8.0,
splashColor: Colors.lightBlue,
onPressed: () {
setState(() {
});
// Navigator.pushReplacement(
// context,
// new MaterialPageRoute(builder: (context) => auxTelaDetalhada),
// );
},
),
),
]),]
),
),
);
}
}