如何从文本字段中的每一行获取值?
How to Get values from each line in text field?
我有输入的文本框:Foo /line1
栏/line2
当我点击提交按钮时,我使用提交的值(name=foo 和 name=bar)创建了小部件 'Name :',我如何从该文本字段创建和获取值?我必须先制作列表视图生成器吗?
谁能举个例子,
String txt = "";
TextEditingController controllerTxt = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text('Create'),
actions: <Widget>[
FlatButton(
child: Text('Submit'),
textColor: Colors.white,
onPressed: () {
Navigator.pushNamed(context, '/ResultPage',
arguments: (controllerTxt.text));
},
),
],
),
body: new ListView(
children: <Widget>[
new Container(
child: new Column(
children: <Widget>[
new TextField(
controller: controllerTxt,
maxLines: 25,
),
],
),
),
],
),
);
}
class _ResultPageState extends State<ResultPage> {
String result;
@override
Widget build(BuildContext context) {
RouteSettings settings = ModalRoute.of(context).settings;
result = settings.arguments;
return new Scaffold(
appBar: new AppBar(
title: Text('Create'),
actions: <Widget>[
],
),
body: new Container(
padding: EdgeInsets.all(10.0),
child: new Row(
children: <Widget>[
new Text('Name : '),
Expanded(
child: new TextField(
enabled: false,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.black)),
labelText: ('${result}'),
),
)),
],
),
),
);
}
}
根据我的理解,这是我的解决方案
所以我假设您有多行文本字段
你想要这样的东西
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
TextEditingController controllerTxt = new TextEditingController();
@override
void initState() {
super.initState();
}
String txt = "";
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text('Create'),
actions: <Widget>[
FlatButton(
child: Text('Submit'),
textColor: Colors.white,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ResultPage(
result: controllerTxt.text,
)));
},
),
],
),
body: new ListView(
children: <Widget>[
new Container(
child: new Column(
children: <Widget>[
new TextField(
controller: controllerTxt,
maxLines: 25,
),
],
),
),
],
),
);
}
}
class ResultPage extends StatefulWidget {
final String result;
const ResultPage({Key key, this.result}) : super(key: key);
@override
_ResultPageState createState() => _ResultPageState();
}
class _ResultPageState extends State<ResultPage> {
List<String> lines;
@override
void initState() {
super.initState();
lines = widget.result.split('\n');
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text('Create'),
actions: <Widget>[],
),
body: Column(
children: lines
.map((line) => Container(
padding: EdgeInsets.all(10.0),
child: new Row(
children: <Widget>[
new Text('Name : '),
Expanded(
child: new TextField(
enabled: false,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide:
new BorderSide(color: Colors.black)),
labelText: (line),
),
)),
],
),
))
.toList(),
));
}
}
我有输入的文本框:Foo /line1 栏/line2
当我点击提交按钮时,我使用提交的值(name=foo 和 name=bar)创建了小部件 'Name :',我如何从该文本字段创建和获取值?我必须先制作列表视图生成器吗?
谁能举个例子,
String txt = "";
TextEditingController controllerTxt = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text('Create'),
actions: <Widget>[
FlatButton(
child: Text('Submit'),
textColor: Colors.white,
onPressed: () {
Navigator.pushNamed(context, '/ResultPage',
arguments: (controllerTxt.text));
},
),
],
),
body: new ListView(
children: <Widget>[
new Container(
child: new Column(
children: <Widget>[
new TextField(
controller: controllerTxt,
maxLines: 25,
),
],
),
),
],
),
);
}
class _ResultPageState extends State<ResultPage> {
String result;
@override
Widget build(BuildContext context) {
RouteSettings settings = ModalRoute.of(context).settings;
result = settings.arguments;
return new Scaffold(
appBar: new AppBar(
title: Text('Create'),
actions: <Widget>[
],
),
body: new Container(
padding: EdgeInsets.all(10.0),
child: new Row(
children: <Widget>[
new Text('Name : '),
Expanded(
child: new TextField(
enabled: false,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide: new BorderSide(color: Colors.black)),
labelText: ('${result}'),
),
)),
],
),
),
);
}
}
根据我的理解,这是我的解决方案
所以我假设您有多行文本字段
你想要这样的东西
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
TextEditingController controllerTxt = new TextEditingController();
@override
void initState() {
super.initState();
}
String txt = "";
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text('Create'),
actions: <Widget>[
FlatButton(
child: Text('Submit'),
textColor: Colors.white,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ResultPage(
result: controllerTxt.text,
)));
},
),
],
),
body: new ListView(
children: <Widget>[
new Container(
child: new Column(
children: <Widget>[
new TextField(
controller: controllerTxt,
maxLines: 25,
),
],
),
),
],
),
);
}
}
class ResultPage extends StatefulWidget {
final String result;
const ResultPage({Key key, this.result}) : super(key: key);
@override
_ResultPageState createState() => _ResultPageState();
}
class _ResultPageState extends State<ResultPage> {
List<String> lines;
@override
void initState() {
super.initState();
lines = widget.result.split('\n');
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text('Create'),
actions: <Widget>[],
),
body: Column(
children: lines
.map((line) => Container(
padding: EdgeInsets.all(10.0),
child: new Row(
children: <Widget>[
new Text('Name : '),
Expanded(
child: new TextField(
enabled: false,
decoration: new InputDecoration(
border: new OutlineInputBorder(
borderSide:
new BorderSide(color: Colors.black)),
labelText: (line),
),
)),
],
),
))
.toList(),
));
}
}