我需要使用phpmyadmin将动态创建的文本字段中键入的值添加到我的数据库中。我正在使用本地数据库。我不明白如何为动态添加的字段实现控制器,以将数据传递到php文件。(编辑:我可以使用添加的字段)。
代码如下:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class AddProject extends StatefulWidget {
@override
_AddProject createState() => new _AddProject();
}
class _AddProject extends State<AddProject> {
List<Widget> listDynamic = new List();
TextEditingController pnameController = new TextEditingController();
TextEditingController role1Controller = new TextEditingController();
TextEditingController role2Controller = new TextEditingController();
TextEditingController role3Controller = new TextEditingController();
TextEditingController user1Controller = new TextEditingController();
TextEditingController user2Controller = new TextEditingController();
TextEditingController user3Controller = new TextEditingController();
addDynamic() {
listDynamic.add(new DynamicWidget());
setState(() {});
}
Future<String> _addProject() async {
var url = "";
final response = await http.post(url,
headers: { "accept": "application/json"},
body: {
"pname": pnameController.text,
"role1": role1Controller.text,
"role2": role2Controller.text,
"role3": role3Controller.text,
"user1": user1Controller.text,
"user2": user2Controller.text,
"user3": user3Controller.text,
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Add Project"),
),
body: Container(
padding: const EdgeInsets.all(10.0),
child: ListView(children: <Widget>[
new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new TextField(
decoration: new InputDecoration(
hintText: "Project Name", labelText: "Project Name"),
controller: pnameController,
),
new Container(
margin: EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
new Flexible(
child: Padding(padding: const EdgeInsets.all(8.0),
child: new TextField(
decoration:
const InputDecoration(helperText: "Enter Role", contentPadding: EdgeInsets.all(8.0)),
controller: role1Controller,
style: Theme.of(context).textTheme.body1,
),
)
),
new Flexible(
child: Padding(padding: const EdgeInsets.all(8.0),
child: new TextField(
decoration:
const InputDecoration(helperText: "Enter User", contentPadding: EdgeInsets.all(8.0)),
controller: user1Controller,
style: Theme.of(context).textTheme.body1,
)),
)
],
),
),
new Container(
margin: EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
new Flexible(
child: Padding(padding: const EdgeInsets.all(8.0),
child: new TextField(
decoration:
const InputDecoration(helperText: "Enter Role", contentPadding: EdgeInsets.all(8.0)),
controller: role2Controller,
style: Theme.of(context).textTheme.body1,
),
)
),
new Flexible(
child: Padding(padding: const EdgeInsets.all(8.0),
child: new TextField(
decoration:
const InputDecoration(helperText: "Enter User", contentPadding: EdgeInsets.all(8.0)),
controller: user2Controller,
style: Theme.of(context).textTheme.body1,
)),
)
],
),
),
new Container(
margin: EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
new Flexible(
child: Padding(padding: const EdgeInsets.all(8.0),
child: new TextField(
decoration:
const InputDecoration(helperText: "Enter Role", contentPadding: EdgeInsets.all(8.0)),
controller: role3Controller,
style: Theme.of(context).textTheme.body1,
),
)
),
new Flexible(
child: Padding(padding: const EdgeInsets.all(8.0),
child: new TextField(
decoration:
const InputDecoration(helperText: "Enter User", contentPadding: EdgeInsets.all(8.0)),
controller: user3Controller,
style: Theme.of(context).textTheme.body1,
)),
)
],
),
),
new Flexible(
child: new ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: listDynamic.length,
itemBuilder: (_, index) => listDynamic[index])),
new Container(
child: new RaisedButton(
onPressed: _addProject,
child: new Text('Add Project'),
),
)
],
),
])),
floatingActionButton: FloatingActionButton(
tooltip: 'Add roles',
child: Icon(Icons.add),
onPressed: addDynamic,
),
);
}
}
class DynamicWidget extends StatelessWidget {
TextEditingController controller = new TextEditingController();
TextEditingController controller1 = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Container(
margin: EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
new Flexible(
child: Padding(padding: const EdgeInsets.all(8.0),
child: new TextField(
decoration:
const InputDecoration(helperText: "Enter Role", contentPadding: EdgeInsets.all(8.0)),
controller: controller,
style: Theme.of(context).textTheme.body1,
),
)
),
new Flexible(
child: Padding(padding: const EdgeInsets.all(8.0),
child: new TextField(
decoration:
const InputDecoration(helperText: "Enter User", contentPadding: EdgeInsets.all(8.0)),
controller: controller1,
style: Theme.of(context).textTheme.body1,
)),
)
],
),
);
}
}
我需要知道如何在_addProject()函数中使用DynamicWidget类中存在的controller.text和controller1.text。