I need some idiot proof setState help... So I have a location picker (Google Maps) and I want the pickedlocation to be shown as a text in the place where you see 'Placeholder result'. Anyone has an idea how to approach this?
ListTile(
leading: Icon(EvaIcons.pinOutline),
contentPadding: const EdgeInsets.fromLTRB(5, 2, 5, 2),
title: Text('Placeholder result'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return PlacePicker(
apiKey: "API-KEY",
initialPosition: AddEventPage.kInitialPosition,
useCurrentLocation: true,
selectInitialPosition: true,
onPlacePicked: (result) {
setState(() {});
Navigator.of(context).pop();
},
);
}),
);
})
CodePudding user response:
You'll need to have a variable within the State
of a StatefulWidget
. I've used ResultTile
as an example here:
class ResultTile extends StatefulWidget {
@override
_ResultTileState createState() => _ResultTileState();
}
class _ResultTileState extends State<ResultTile> {
String _result = ''; // initially the result is an empty string
@override
Widget build(BuildContext context) {
ListTile(
leading: Icon(EvaIcons.pinOutline),
contentPadding: const EdgeInsets.fromLTRB(5, 2, 5, 2),
title: Text(_result), // display the result
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return PlacePicker(
apiKey: "API-KEY",
initialPosition: AddEventPage.kInitialPosition,
useCurrentLocation: true,
selectInitialPosition: true,
onPlacePicked: (result) {
setState(() {
_result = result
.getText(); // get the String from the result object, if it's not a String already.
});
Navigator.of(context).pop();
},
);
}),
);
},
);
}
}