我需要一些帮助
我正在创建一个应用程序,用户可以选择一个自定义拾取点我已设法使预测下拉列表工作,但我未能将列表中的值传递到文本字段
我需要一些帮助
我正在创建一个应用程序,用户可以选择一个自定义拾取点我已设法使预测下拉列表工作,但我未能将列表中的值传递到文本字段
我已附上代码,任何帮助将不胜感激
class SearchScreen extends StatefulWidget {
@override
_SearchScreenState createState() => _SearchScreenState();
}
class _SearchScreenState extends State<SearchScreen>
{
TextEditingController pickUpTextEditingController = TextEditingController();
TextEditingController dropOffTextEditingController = TextEditingController();
List<PlacePredictions> placePredictionList = [];
@override
Widget build(BuildContext context)
{
return Scaffold(
resizeToAvoidBottomInset: false,
body: Column(
children: [
new Container(
height: 190.0,
decoration: BoxDecoration(
image: new DecorationImage(image: new AssetImage("images/mapbg2.jpg"), fit: BoxFit.cover,),
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
boxShadow: [
BoxShadow(
color: Colors.grey,
blurRadius: 6.0,
spreadRadius: 0.5,
offset: Offset(0.7, 0.7),
),
],
),
child: Padding(
padding: EdgeInsets.only(left: 25.0, top: 30.0, right: 25.0, bottom: 2.0),
child: Column(
children: [
SizedBox(height: 5.0),
Stack(
children: [
GestureDetector(
onTap:()
{
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back),
),
Center(
child: Text("Enter Drop of Location", style: TextStyle(fontSize: 17.0, fontFamily: "Brand-Regular"),),
),
],
),
SizedBox(height: 16.0),
Row(
children: [
Image.asset("images/pickicon.png", height: 20.0, width: 20.0,),
SizedBox(width: 18.0),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(5.0),
),
child: Padding(
padding: EdgeInsets.all(3.0),
child: TextField(
onChanged: (text)
{
findPlace(text);
},
controller: pickUpTextEditingController,
decoration: InputDecoration(
hintText: "PickUp Location",
hintStyle: TextStyle(color: Colors.grey[400],),
fillColor: Colors.grey[200],
filled: true,
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.only(left: 11.0, top: 8.0, bottom: 8.0),
),
),
),
),
),
],
),
SizedBox(height: 10.0),
Row(
children: [
Image.asset("images/desticon.png", height: 20.0, width: 20.0,),
SizedBox(width: 18.0),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(5.0),
),
child: Padding(
padding: EdgeInsets.all(3.0),
child: TextField(
onChanged: (val)
{
findPlace(val);
},
controller: dropOffTextEditingController,
decoration: InputDecoration(
hintText: "Where to",
hintStyle: TextStyle(color: Colors.grey[400],),
fillColor: Colors.grey[200],
filled: true,
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.only(left: 11.0, top: 8.0, bottom: 8.0),
),
),
),
),
),
],
),
],
),
),
),
//tile for predictions
SizedBox(height: 5.0,),
(placePredictionList.length > 0)
? Padding(
padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 10.0),
child: ListView.separated(
padding: EdgeInsets.all(0.0),
itemBuilder: (context, index)
{
return PredictionTile(placePredictions: placePredictionList[index],);
},
separatorBuilder: (BuildContext context, int index) => Divider(),
itemCount: placePredictionList.length,
shrinkWrap: true,
physics: ClampingScrollPhysics(),
),
)
: Container(),
],
),
);
}
void findPlace(String placeName) async
{
if(placeName.length > 2)
{
String autoCompleteUrl = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$placeName&key=$mapkey&sessiontoken=1234567890&components=country:za";
var res = await RequestAssistant.getRequest(autoCompleteUrl);
if(res == "failed")
{
return;
}
if(res["status"] == "OK")
{
var predictions = res["predictions"];
var placesList = (predictions as List).map((e) => PlacePredictions.fromJson(e)).toList();
setState(() {
placePredictionList = placesList;
});
}
}
}
}
class PredictionTile extends StatelessWidget
{
TextEditingController pickUpTextEditingController = TextEditingController();
final PlacePredictions placePredictions;
PredictionTile({Key key, this.placePredictions}) : super(key: key);
@override
Widget build(BuildContext context)
{
return TextButton(
onPressed: ()
{
getPlaceAddressDetails(placePredictions.main_text, context);
},
child: Container(
child: Column
(
children: [
SizedBox(width: 10.0),
Row(
children: [
Icon(Icons.add_location_alt),
SizedBox(width: 14.0),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(placePredictions.main_text, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 14.0),),
SizedBox(height: 3.0,),
Text(placePredictions.secondary_text, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 10.0, color: Colors.grey),),
],
),
),
],
),
SizedBox(width: 10.0),
],
),
),
);
}
void getPlaceAddressDetails(String placeId, context) async
{
/*showDialog(
context: context,
builder: (BuildContext context) => ProgressBox(message: "Getting drop-off location...",)
);*/
String placeDetailsUrl = "https://maps.googleapis.com/maps/api/place/details/json?place_id=$placeId&key=$mapkey";
var res = await RequestAssistant.getRequest(placeDetailsUrl);
Navigator.pop(context);
if(res == "failed")
{
return;
}
if(res["status"] == "OK")
{
Address address = Address();
address.placeName = res["result"]["name"];
address.latitude = res["result"]["geometry"]["location"]["lat"];
address.longitude = res["result"]["geometry"]["location"]["lng"];
Provider.of<AppData>(context, listen: false).updatedropOffLocationAddress(address);
print("Drop off location Selected ::");
print(address.placeName);
Navigator.pop(context, "obtainDirection");
}
}
}