当我被迫将其包含在无状态小部件的构建函数中时调用圆圈函数



我正试图在按下按钮时绘制一个圆圈。我不得不这样放置:

@override
Widget build(BuildContext context) {
Future<void> _makeCircles() async {
Set<Circle> circles = Set.from([
Circle(
circleId: CircleId("myCircle"),
radius: 500,
center: _createCenter(),
fillColor: Color.fromRGBO(255, 255, 255, 0.2),
strokeColor: Color.fromRGBO(247, 16, 0, 0.5))
]);
}
if (lat == null || lng == null) {
return Scaffold();
}
return Scaffold(
appBar: AppBar(title: Text(''), actions: <Widget>[
PopupMenuButton<String>(

我试着把它放在initState中,但它不起作用。

现在的问题是,我的地图找不到它:

child: GoogleMap(
markers: Set<Marker>.of(markers.values),
initialCameraPosition:
CameraPosition(target: LatLng(lat, lng), zoom: 6.0),
myLocationEnabled: true,
compassEnabled: true,
mapToolbarEnabled: true,
zoomControlsEnabled: true,
zoomGesturesEnabled: true,
myLocationButtonEnabled: true,
circles: circles,
mapType: MapType.normal,
onMapCreated: (GoogleMapController controller) {
controller.setMapStyle(Utils.mapStyles);
}),

"未定义的名称"圆形";

我怎么能在这里打圈?

在构建方法上方,放置以下代码行

Set<Circle> circles;

在您的构建方法中更改

Set<Circle> circles = Set.from([
Circle(
circleId: CircleId("myCircle"),
radius: 500,
center: _createCenter(),
fillColor: Color.fromRGBO(255, 255, 255, 0.2),
strokeColor: Color.fromRGBO(247, 16, 0, 0.5))
]);

到此

circles = Set.from([
Circle(
circleId: CircleId("myCircle"),
radius: 500,
center: _createCenter(),
fillColor: Color.fromRGBO(255, 255, 255, 0.2),
strokeColor: Color.fromRGBO(247, 16, 0, 0.5))
]);

这是可行的。

对于动态横向、纵向和径向

步骤1:获取纬度、经度和半径变量。

var latitude, longitude,radius;
TextFormField(
decoration:InputDecoration(

hintText: '"Enter latitude',

),
onChanged: (String value) {
latitude=value;
},
),
TextFormField(
decoration:InputDecoration(

hintText: '"Enter radius",

),
onChanged: (String value) {
radius=value;
},
),
TextFormField(
decoration:InputDecoration(

hintText: '"Enter latitude',

),
onChanged: (String value) {
latitude=value;
},
),

现在,在圆圈

center: LatLng(latitude, longitude),
radius: radius,

这就成功了。

@override
Widget build(BuildContext context) {
if (lat == null || lng == null) {
return Scaffold();
}
Set<Circle> circles = Set.from([
Circle(
circleId: CircleId("none"),
center: _createCenter(),
radius: 500,
fillColor: Color.fromRGBO(255, 255, 255, 0.2),
strokeColor: Color.fromRGBO(247, 16, 0, 0.5))
]);
Future<void> _makeCircles() async {
Set<Circle> circles = Set.from([
Circle(
circleId: CircleId("none"),
radius: 500,
center: _createCenter(),
fillColor: Color.fromRGBO(255, 255, 255, 0.2),
strokeColor: Color.fromRGBO(247, 16, 0, 0.5))
]);
}
return Scaffold(
appBar: AppBar(title: Text(''), actions: <Widget>[

更好:

Future<void> _makeCircles() async {
setState(() {
circles = Set.from([
Circle(
circleId: CircleId("none"),
radius: 1500,
center: _createCenter(),
fillColor: Color.fromRGBO(255, 255, 255, 0.2),
strokeColor: Color.fromRGBO(247, 16, 0, 0.5))]);
//    Navigator.pop(context);
//      circles = circles;
});
}

Set<Circle> circles = <Circle>{};

相关内容

  • 没有找到相关文章

最新更新