嗨,我有一个从 firestore 显示的带有标记的 googlemap,问题是:我有一些集合,如酒吧、餐馆……我想让用户用 RadioButton 选择他想要显示的内容。
String selectedIndex = "";
class MapPage extends StatefulWidget {
@override
_MapPageState createState() => _MapPageState();
}
class _MapPageState extends State<MapPage> {
GoogleMapController myController;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
void initMarker(specify, specifyId) async{
var markerIdVal = specifyId;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position: LatLng(specify['location'].latitude, specify['location'].longitude),
infoWindow: InfoWindow(title: specify['name'], snippet: specify['address'])
);
setState(() {
markers[markerId] = marker;
});
}
// -----------------------------------------Marker---------------------------
getMarkerData() async{
FirebaseFirestore.instance.collection('Bars').get().then((myData){
if(myData.docs.isNotEmpty){
for (int i = 0; i < myData.docs.length ; i++) {
initMarker(myData.docs[i].data(), myData.docs[i].id);
}
}
});
}
void initState(){
getMarkerData();
super.initState();
}
@override
Widget build(BuildContext context) {
Set<Marker> getMarker() {
return <Marker>[
Marker(
markerId: MarkerId('Test'),
position: LatLng(47.4750741,-0.5213625),
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow(title: 'no')
)
].toSet();
}
return Scaffold(
body: Stack(
children: <Widget>[
Positioned.fill(child: GoogleMap(
markers: Set<Marker>.of(markers.values),
mapType: MapType.normal,
initialCameraPosition: CameraPosition(
target: LatLng(47.4573359,-0.589259), zoom: 10,),
onMapCreated: (GoogleMapController controller) {
myController = controller;
}),
),
Randomizer(), // TODO : le randomizer a programmer
RadioButton(),
],
),
);
}
}
And the RadioButton side :
class RadioButton extends StatefulWidget {
@override
_RadioButtonState createState() => _RadioButtonState();
}
class _RadioButtonState extends State<RadioButton> {
List<String> lst = ['Resto', 'Bars', 'encours'];
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
customRadio(lst[0], 'Resto'), // Resto
customRadio(lst[1], 'Bars'), // bars
//customRadio(lst[2], ),
],
),
);
}
void changeIndex(String index) {
setState(() {
selectedIndex = index;
_MapPageState();
print(index);
});
}
Widget customRadio(String txt, String index) {
return OutlineButton(
onPressed: () => changeIndex(index),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
borderSide: BorderSide(
color: selectedIndex == index ? Colors.cyan : Colors.grey),
child: Text(txt, style: TextStyle(
color: selectedIndex == index ? Colors.cyan : Colors.grey),),
);
}
}
我尝试使用 selectedIndex 可以具有值“Bars”和“Restaurant”,并且在查询中使用,但它没有刷新它。有人有想法吗?
感谢您的时间