[英]How to auto click button in flutter
我有这个代码,它从 Google mab 获取地址信息 有没有办法让 Google mab 在我进入地址页面时自动加载,而不是单击图标来加载它?
setAddress() {
return Row(
children: [
Expanded(
child: TextFormField(
keyboardType: TextInputType.text,
textInputAction: TextInputAction.next,
textCapitalization: TextCapitalization.sentences,
style: Theme.of(context)
.textTheme
.subtitle2
.copyWith(color: colors.fontColor),
focusNode: addFocus,
controller: addressC,
validator: (val) =>
validateField(val, getTranslated(context, 'FIELD_REQUIRED')),
onSaved: (String value) {
address = value;
},
onFieldSubmitted: (v) {
_fieldFocusChange(context, addFocus, locationFocus);
},
decoration: InputDecoration(
hintText: getTranslated(context, 'ADDRESS_LBL'),
isDense: true,
),
),
),
Container(
margin: EdgeInsetsDirectional.only(start: 5),
width: 40,
child: IconButton(
icon: new Icon(
Icons.my_location,
size: 20,
),
focusNode: locationFocus,
onPressed: () async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Map(
latitude: latitude == null
? position.latitude
: double.parse(latitude),
longitude: longitude == null
? position.longitude
: double.parse(longitude),
from: getTranslated(context, 'ADDADDRESS'),
)));
if (mounted) setState(() {});
List<Placemark> placemark = await placemarkFromCoordinates(
double.parse(latitude), double.parse(longitude));
state = placemark[0].administrativeArea;
country = placemark[0].country;
pincode = placemark[0].postalCode;
// address = placemark[0].name;
if (mounted)
setState(() {
countryC.text = country;
stateC.text = state;
pincodeC.text = pincode;
// addressC.text = address;
});
},
),
)
],
);
}
https://i.stack.imgur.com/6F34B.png
有没有办法在我进入地址页面时自动加载 Google mab,而不是单击图标来加载它?
您可以使用输入控制器或更具体地说是 TextEditingController https://flutter.dev/docs/cookbook/forms/text-field-changes
通过这种方式,您可以检测用户何时在字段中输入文本或用户何时离开该字段转到下一个等。您必须决定触发地图的内容。
// create a text field controller
final myController = TextEditingController();
...
// add controller to text field
TextField(
controller: myController,
),
...
// add a listener to the controller
@override
void initState() {
super.initState();
// Start listening to changes.
myController.addListener(_printLatestValue);
}
// function called every time the text field is changed
void _printLatestValue() {
print('Second text field: ${myController.text}');
// HERE YOU CAN CALL THE MAP
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Map(
latitude: latitude == null
? position.latitude
: double.parse(latitude),
longitude: longitude == null
? position.longitude
: double.parse(longitude),
from: getTranslated(context, 'ADDADDRESS'),
)));
if (mounted) setState(() {});
List<Placemark> placemark = await placemarkFromCoordinates(
double.parse(latitude), double.parse(longitude));
state = placemark[0].administrativeArea;
country = placemark[0].country;
pincode = placemark[0].postalCode;
// address = placemark[0].name;
if (mounted)
setState(() {
countryC.text = country;
stateC.text = state;
pincodeC.text = pincode;
// addressC.text = address;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.