我在有状态窗口小部件类中列出了一个列表,当用户选择卡片时,该列表将填充一些值,我的应用程序按钮上具有showDialog函数,每次单击该表单时,都会显示用户。 问题是用户通过单击“确定”按钮从表单返回表单后,将重新创建有状态的类并清理我所有列表的值(再次调用ActivityPeopleCard类)...即使在从showDialog表单回来吗?
class ActivityPeopleCard extends StatefulWidget{
Activity activity;
List<double> _cost ;
ActivityPeopleCard({this.activity});
@override
_ActivityPeopleCardState createState() => _ActivityPeopleCardState();
}
class _ActivityPeopleCardState extends State<ActivityPeopleCard> {
int _peopleIndex = -1;
List<int> _peopleIndexes = new List();
List<int> _longPressed = new List();
@override
Widget build(BuildContext context) {
return Container(
height: height/2.6,
child:new GridView.builder(
itemCount: widget.activity.peopleInvolved.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: (BuildContext context, int index) {
return new GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: ()=> setState(() => {
}
),
child: _buildWidget(index, context)
);
},
),
);
}
_buildWidget(int index, BuildContext context){
bool isSelected = _peopleIndexes.contains(index)?true:false;
return new Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
elevation: 5.0,
child: Stack(
children:<Widget>[
new Container(
height: height/4,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width:height/15,
height: height/15,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(height/20),
border: Border.all(color: Colors.white, width:2),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(height/15),
child: Image.asset(widget.activity.peopleInvolved[index].imagePath, fit: BoxFit.fill)),//CircleAvatar(
),
Center(child: Text(widget.activity.peopleInvolved[index].name))
],
),
),
shareTag(index, isSelected, context)
])
);
}
shareTag(index, isSelected, BuildContext context){
return GestureDetector(
onLongPress: ()=>{
enterPrice(index, context),
},
child: Visibility(
visible: isSelected ? true:false,
child: new Container(
height: height/2,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(height/20),
color: Colors.red.shade500.withOpacity(0.7),
),
child: Center(child: SizedBox(
width: width/4,
height: height/15,
child:Center(child: AutoSizeText("\$${ActivityPeopleCard._cost[index]}", style:TextStyle(fontWeight: FontWeight.bold,color: Colors.white,fontFamily: 'Oxygen',fontSize: height/25),
maxLines: 1,minFontSize: 5,textAlign: TextAlign.center,),
),
))),
)
);
}
enterPrice(int index, BuildContext context){
//print("Long Pressed");
showDialog(
context: context,
builder: (context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25)
),
backgroundColor: Colors.white,
content: Form(
//key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new TextFormField(
initialValue: ActivityPeopleCard._cost[index].toString(),
keyboardType: TextInputType.number,
cursorColor: secondColor,
style: TextStyle(letterSpacing: 1,),
autofocus: true,
decoration: new InputDecoration(
focusedBorder: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(15.0),
borderSide: new BorderSide(
color: secondColor
)),
labelStyle: TextStyle(
color: firstColor
//decorationColor: Colors.yellow
),
prefixIcon: Icon(Icons.attach_money, color: firstColor,),
labelText: "Cost",
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(15.0),
borderSide: new BorderSide(
color: Colors.black87
),
),
),
),
],
),
),
);
});
}
}
用户从表单返回后,_cost列表将再次使用null初始化,并显示null值,而不是以前的值
答案 0 :(得分:0)
在初始状态下创建一个变量,该变量返回您的Container。然后在您的构建方法中返回您的变量。每次调用build方法时,都会停止刷新/重置列表
class ActivityPeopleCard extends StatefulWidget {
Activity activity;
List<double> _cost;
ActivityPeopleCard({this.activity});
@override
_ActivityPeopleCardState createState() => _ActivityPeopleCardState();
}
class _ActivityPeopleCardState extends State<ActivityPeopleCard> {
int _peopleIndex = -1;
List<int> _peopleIndexes = new List();
List<int> _longPressed = new List();
var _vlContainer;
@override
void initState() {
super.initState();
_vlContainer = myListContainer();
}
@override
Widget build(BuildContext context) {
return _vlContainer;
}
myListContainer() {
return Container(
height: height / 2.6,
child: new GridView.builder(
itemCount: widget.activity.peopleInvolved.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: (BuildContext context, int index) {
return new GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () => setState(() => {}),
child: _buildWidget(index, context));
},
),
);
}
_buildWidget(int index, BuildContext context) {
bool isSelected = _peopleIndexes.contains(index) ? true : false;
return new Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
elevation: 5.0,
child: Stack(children: <Widget>[
new Container(
height: height / 4,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: height / 15,
height: height / 15,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(height / 20),
border: Border.all(color: Colors.white, width: 2),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(height / 15),
child: Image.asset(
widget.activity.peopleInvolved[index].imagePath,
fit: BoxFit.fill)), //CircleAvatar(
),
Center(child: Text(widget.activity.peopleInvolved[index].name))
],
),
),
shareTag(index, isSelected, context)
]));
}
shareTag(index, isSelected, BuildContext context) {
return GestureDetector(
onLongPress: () => {
enterPrice(index, context),
},
child: Visibility(
visible: isSelected ? true : false,
child: new Container(
height: height / 2,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(height / 20),
color: Colors.red.shade500.withOpacity(0.7),
),
child: Center(
child: SizedBox(
width: width / 4,
height: height / 15,
child: Center(
child: AutoSizeText(
"\$${ActivityPeopleCard._cost[index]}",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontFamily: 'Oxygen',
fontSize: height / 25),
maxLines: 1,
minFontSize: 5,
textAlign: TextAlign.center,
),
),
))),
));
}
enterPrice(int index, BuildContext context) {
//print("Long Pressed");
showDialog(
context: context,
builder: (context) {
return AlertDialog(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
backgroundColor: Colors.white,
content: Form(
//key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new TextFormField(
initialValue: ActivityPeopleCard._cost[index].toString(),
keyboardType: TextInputType.number,
cursorColor: secondColor,
style: TextStyle(
letterSpacing: 1,
),
autofocus: true,
decoration: new InputDecoration(
focusedBorder: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(15.0),
borderSide: new BorderSide(color: secondColor)),
labelStyle: TextStyle(color: firstColor
//decorationColor: Colors.yellow
),
prefixIcon: Icon(
Icons.attach_money,
color: firstColor,
),
labelText: "Cost",
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(15.0),
borderSide: new BorderSide(color: Colors.black87),
),
),
),
],
),
),
);
});
}
}