问题描述
我需要帮助
我正在创建一个应用程序,用户可以选择一个自定义取件点我已经设法使预测下拉列表工作,但我无法将值从列表传递到文本字段
我需要帮助
我正在创建一个应用程序,用户可以选择一个自定义取件点我已经设法使预测下拉列表工作,但我无法将值从列表传递到文本字段
我附上了代码,任何帮助将不胜感激
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",child: TextField(
onChanged: (val)
{
findplace(val);
},controller: dropOffTextEditingController,decoration: Inputdecoration(
hintText: "Where to",//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,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");
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)