Flutter 如何将变量的文本带入文本字段
我是振动方面的初学者,我正在尝试开发我的第一个应用程序 我做了很多研究,没有找到我问题的答案 我正在使用API通过邮政编码查找地址、社区和城市信息 我将这些数据存储在变量中,我希望,当单击按钮搜索这些信息时,返回将出现在文本字段中,而不是文本中,就像现在在我的代码中一样 这是我在用户注册时带来的信息,我想把它放在一个标准的布局中 这是我的密码Flutter 如何将变量的文本带入文本字段,flutter,flutter-layout,Flutter,Flutter Layout,我是振动方面的初学者,我正在尝试开发我的第一个应用程序 我做了很多研究,没有找到我问题的答案 我正在使用API通过邮政编码查找地址、社区和城市信息 我将这些数据存储在变量中,我希望,当单击按钮搜索这些信息时,返回将出现在文本字段中,而不是文本中,就像现在在我的代码中一样 这是我在用户注册时带来的信息,我想把它放在一个标准的布局中 这是我的密码 import 'package:flutter/material.dart'; import 'package:http/http.dar
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class cadastro extends StatefulWidget {
@override
_cadastroState createState() => _cadastroState();
}
class _cadastroState extends State<cadastro> {
String _logradouro = "";
String _localidade = "";
String _bairro = "";
TextEditingController _controllerCep = TextEditingController();
_recuperarCep() async {
String cepDigitado = _controllerCep.text;
String url = "http://viacep.com.br/ws/${cepDigitado}/json/";
http.Response response;
response = await http.get(url);
Map<String, dynamic> retorno = json.decode(response.body);
String logradouro = retorno["logradouro"];
String bairro = retorno["bairro"];
String localidade = retorno["localidade"];
setState(() {
_logradouro = "${logradouro}";
_localidade = "${localidade}";
_bairro = "${bairro}";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(
width: double.infinity,
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Cep",
style: (TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
)),
),
Padding(
padding: EdgeInsets.only(right: 20),
child: RaisedButton(
color: Color(0xffffdb2e),
textColor: Color(0xff4f4138),
padding: EdgeInsets.all(15),
child: Text(
"Buscar",
style: TextStyle(
fontSize: 20,
),
),
onPressed: _recuperarCep,
),
)
],
),
),
Padding(
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
child: TextField(
maxLength: 8,
keyboardType: TextInputType.number,
style: TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
),
decoration: InputDecoration(
fillColor: Color(0xff4f4138),
),
controller: _controllerCep,
),
),
Padding(
padding: EdgeInsets.only(left: 20, bottom: 30, top: 30),
child: Text(
"Logradouro: " + _logradouro,
style: (TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
)),
),
),
Padding(
padding:
EdgeInsets.only(left: 20, right: 20, bottom: 20, top: 20),
child: Text(
"Bairro: " + _bairro,
style: (TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
)),
),
),
Padding(
padding:
EdgeInsets.only(left: 20, right: 20, bottom: 30, top: 20),
child: Text(
"Cidade: " + _localidade,
style: (TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
)),
),
),
],
),
),
));
}
}
将3个文本元素更改为TextField。
定义3个TextEditingController并将其分配给TextFields。
在RecurperArcep方法中,不要将变量设置为获得的值,而是将相应TextField的控制器的“text”属性设置为值。
这是你的代码这应该是你正在寻找的
TextEditingController _controllerLogradouro = TextEditingController();
TextEditingController _controllerBairro = TextEditingController();
TextEditingController _controllerCidade = TextEditingController();
//Initialize this with your other TextEditingController
setState(() {
_controllerLogradouro.text = "Logradouro: ${logradouro}";
_controllerCidade.text = "Cidade: ${localidade}";
_controllerBairro.text = "Bairro: ${bairro}";
});
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Cep",
style: (TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
)),
),
Padding(
padding: EdgeInsets.only(right: 20),
child: RaisedButton(
color: Color(0xffffdb2e),
textColor: Color(0xff4f4138),
padding: EdgeInsets.all(15),
child: Text(
"Buscar",
style: TextStyle(
fontSize: 20,
),
),
onPressed: _recuperarCep,
),
)
],
),
Padding(
padding: EdgeInsets.only(left: 20, right: 20, bottom: 20),
child: TextField(
maxLength: 8,
keyboardType: TextInputType.number,
style: TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
),
decoration: InputDecoration(
fillColor: Color(0xff4f4138),
),
controller: _controllerCep,
),
),
Padding(
padding: EdgeInsets.only(left: 20, bottom: 30, top: 30),
child: TextFormField(
style: TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
),
controller: _controllerLogradouro,
),
),
Padding(
padding:
EdgeInsets.only(left: 20, right: 20, bottom: 20, top: 20),
child: TextFormField(
controller: _controllerBairro,
style: TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
),
),
),
Padding(
padding:
EdgeInsets.only(left: 20, right: 20, bottom: 30, top: 20),
child: TextFormField(
controller: _controllerCidade,
style: TextStyle(
color: Color(0xff4f4138),
fontSize: 20,
),
),
),
],
),
我想我把你的变量放对了抱歉如果我没有我只会说英语假设你在你的函数中: 添加这一行
setState((){
_controllerCep.text = _logradouro.toString();
});
现在,无论您的_logradouro变量的值是文本字段中的值,您的欢迎如果它起作用,您能否接受并向上投票答案?如果其他人有相同的问题,它将在将来帮助他们: