我有两个表单添加任务和更新任务。
两者都有两个字段名称和描述。当我尝试编辑现有任务时,它不会将值填充到表单中。
这是添加任务对话框
add_filter('woocommerce_email_recipient_new_order', 'restrict_admin_new_order_mail', 1, 2);
function restrict_admin_new_order_mail( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) )
return $recipient;
if( $order->get_total() > 0 ) {
return $recipient;
} else {
return '';
}
}
并更新任务表单
showDialog(
context: context,
builder: (context) {
Size size = MediaQuery.of(context).size;
return Container(
child: AlertDialog(
title: Text(
'Add a new task',
style: TextStyle(
color: Colors.black,
fontSize: 18.0,
),
),
shape: RoundedRectangleBorder(
side: BorderSide(color: primaryColor)),
content: new Container(
width: size.width * 0.25,
height: size.height,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
color: const Color(0xFFFFFF),
borderRadius:
new BorderRadius.all(new Radius.circular(32.0)),
),
child: StreamBuilder(
stream: bloc.addNewTaskStream,
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasData) {
switch (snapshot.data.status) {
case Status.LOADING:
return Loading(
loadingMessage: snapshot.data.message);
break;
case Status.ERROR:
return Error(errorMessage: snapshot.data.message);
}
}
return Form(
key: _formKey,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// dialog center
new Expanded(
child: new Column(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
StreamBuilder<String>(
stream: bloc.task,
builder: (context, snapshot) {
return TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Name'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter the task';
}
return null;
},
onChanged: bloc.changeTask,
);
}),
StreamBuilder<String>(
stream: bloc.description,
builder: (context, snapshot) {
return TextFormField(
decoration: InputDecoration(
border:
InputBorder.none,
hintText:
'Description'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter the description';
}
return null;
},
onChanged:
bloc.changeDescription);
}),
],
),
),
// dialog bottom
new Expanded(
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
crossAxisAlignment:
CrossAxisAlignment.end,
children: <Widget>[
FlatButton(
color: Color(0XFFEFEFEF),
textColor: primaryColor,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.symmetric(
vertical: 15.0,
horizontal: 10.0),
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
"Close",
style: TextStyle(
fontSize: 15.0,
),
),
),
FlatButton(
color: primaryColor,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.symmetric(
vertical: 15.0,
horizontal: 10.0),
onPressed: () =>bloc.addTask(_formKey, context),
child: Text(
"Add",
style: TextStyle(
fontSize: 15.0,
),
),
),
],
),
),
],
),
);
}),
),
),
);
});
点击 editTask 后,我将添加要接收的值。
showDialog(
context: context,
builder: (context) {
Size size = MediaQuery.of(context).size;
return Container(
child: AlertDialog(
title: Text(
'Update task',
style: TextStyle(
color: Colors.black,
fontSize: 18.0,
),
),
shape: RoundedRectangleBorder(
side: BorderSide(color: primaryColor)),
content: new Container(
width: size.width * 0.25,
height: size.height,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
color: const Color(0xFFFFFF),
borderRadius:
new BorderRadius.all(new Radius.circular(32.0)),
),
child: StreamBuilder(
stream: bloc.addNewTaskStream,
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasData) {
switch (snapshot.data.status) {
case Status.LOADING:
return Loading(
loadingMessage: snapshot.data.message);
break;
case Status.ERROR:
return Error(errorMessage: snapshot.data.message);
}
}
return Form(
key: _formKey,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// dialog center
new Expanded(
child: new Column(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
StreamBuilder<String>(
stream: bloc.task,
builder: (context, snapshot) {
return TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Name'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter the task';
}
return null;
},
onChanged: bloc.changeTask,
);
}),
StreamBuilder<String>(
stream: bloc.description,
builder: (context, snapshot) {
return TextFormField(
decoration: InputDecoration(
border:
InputBorder.none,
hintText:
'Description'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter the description';
}
return null;
},
onChanged:
bloc.changeDescription);
}),
],
),
),
// dialog bottom
new Expanded(
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
crossAxisAlignment:
CrossAxisAlignment.end,
children: <Widget>[
FlatButton(
color: Color(0XFFEFEFEF),
textColor: primaryColor,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.symmetric(
vertical: 15.0,
horizontal: 10.0),
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
"Close",
style: TextStyle(
fontSize: 15.0,
),
),
),
FlatButton(
color: primaryColor,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.symmetric(
vertical: 15.0,
horizontal: 10.0),
onPressed: () =>bloc.updateTask(_formKey, context, selectedTaskId),
child: Text(
"Update",
style: TextStyle(
fontSize: 15.0,
),
),
),
],
),
),
],
),
);
}),
),
),
);
});
这是 AddTAsk 和 UpdateTask 表单
答案 0 :(得分:0)
最好的方法是使用它包含的 bloc 库 BloCProvider.value 并让您更好地控制上下文和数据