Flutter 颤振设置状态,但屏幕没有';t更新
颤振应用程序创建小部件列表(wList)并正确显示屏幕。如果用户按下按钮,它将向wList添加一个分隔符(),并通过setState()更新屏幕。但是,屏幕没有更新。我想我可能不太理解setState的逻辑。如果我更新wList并调用setState()函数,我认为它应该更新屏幕。但事实并非如此Flutter 颤振设置状态,但屏幕没有';t更新,flutter,setstate,Flutter,Setstate,颤振应用程序创建小部件列表(wList)并正确显示屏幕。如果用户按下按钮,它将向wList添加一个分隔符(),并通过setState()更新屏幕。但是,屏幕没有更新。我想我可能不太理解setState的逻辑。如果我更新wList并调用setState()函数,我认为它应该更新屏幕。但事实并非如此 @override Widget build(BuildContext context) { return Scaffold(
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('檯號: ${widget.inputTableNumber}'),
centerTitle: true,
backgroundColor: Colors.black,
actions: <Widget>[
IconButton(icon: Icon(Icons.edit), onPressed: () => _showButtons(), color: Colors.white,)
],
),
body: RepaintBoundary(
key: _renderInvoice,
child: Padding(
padding: EdgeInsets.all(15.0),
child: ListView(
children: wList,
),
)
)
);
}
_showButtons() {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
color: Colors.white54,
height: 500.0,
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20.0),
crossAxisSpacing: 30.0,
mainAxisSpacing: 30.0,
crossAxisCount: 3,
children: <Widget>[
FloatingActionButton(
onPressed: () {_addPercentage(0.1);},
heroTag: null,
backgroundColor: Colors.purpleAccent,
child: Text('+10%', style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w500)),
foregroundColor: Colors.black,
),
],
)
);
});
}
_addPercentage(double d) {
Navigator.pop(context);
setState(() {
wList.add(Divider(color: Colors.black,));
});
}
@覆盖
小部件构建(构建上下文){
返回脚手架(
背景颜色:Colors.white,
appBar:appBar(
标题:文本('檯號: ${widget.InputableNumber}'),
标题:对,
背景颜色:Colors.black,
行动:[
图标按钮(图标:图标(Icons.edit),ON按下:()=>\u showButtons(),颜色:Colors.white,)
],
),
正文:重新绘制边界(
键:_renderInvoice,
孩子:填充(
填充:所有边缘设置(15.0),
子:ListView(
儿童:wList,
),
)
)
);
}
_显示按钮(){
showModalBottomSheet(
上下文:上下文,
生成器:(BuildContext上下文){
返回容器(
颜色:颜色。白色54,
高度:500.0,
子项:GridView.count(
主要:错误,
填充:常数边集全部(20.0),
交叉轴间距:30.0,
主轴间距:30.0,
交叉轴计数:3,
儿童:[
浮动操作按钮(
按下:(){u addPercentage(0.1);},
heroTag:null,
背景颜色:颜色。紫色,
子项:文本(“+10%”,样式:TextStyle(fontSize:20.0,fontWeight:fontWeight.w500)),
前底色:颜色。黑色,
),
],
)
);
});
}
_添加百分比(双d){
Navigator.pop(上下文);
设置状态(){
添加(分隔符(颜色:Colors.black,);
});
}
因此,此操作失败的原因是标准的
Listview
构造函数需要一个const
子参数。显然,您的wList
不是const
值,按下按钮时会发生变化
相反,您应该像这样使用Listview.builder
:
ListView.builder(
itemCount: wList.length,
itemBuilder: (context, index) {
return wList[index];
}
)
因此,此操作失败的原因是标准的
Listview
构造函数需要一个const
子参数。显然,您的wList
不是const
值,按下按钮时会发生变化
相反,您应该像这样使用Listview.builder
:
ListView.builder(
itemCount: wList.length,
itemBuilder: (context, index) {
return wList[index];
}
)
这食物太贵了