amap_map/example/lib/pages/overlays/place_polygon.dart

212 lines
6.3 KiB
Dart
Raw Permalink Normal View History

2023-12-22 21:23:24 +08:00
import 'package:amap_map_example/widgets/amap_switch_button.dart';
import 'package:flutter/material.dart';
import 'package:amap_map/amap_map.dart';
2023-12-22 23:13:56 +08:00
import 'package:x_amap_base/x_amap_base.dart';
2023-12-22 21:23:24 +08:00
2023-12-30 07:54:59 +08:00
class PolygonDemoPage extends StatefulWidget {
const PolygonDemoPage();
2023-12-22 21:23:24 +08:00
@override
State<StatefulWidget> createState() => _State();
}
2023-12-30 07:54:59 +08:00
class _State extends State<PolygonDemoPage> {
2023-12-22 21:23:24 +08:00
_State();
// Values when toggling Polygon color
int colorsIndex = 0;
List<Color> colors = <Color>[
Colors.purple,
Colors.red,
Colors.green,
Colors.pink,
];
2024-08-26 09:55:49 +08:00
final Map<String, Polygon> _polygons = <String, Polygon>{};
2023-12-22 21:23:24 +08:00
String? selectedPolygonId;
void _onMapCreated(AMapController controller) {}
LatLng _createLatLng(double lat, double lng) {
return LatLng(lat, lng);
}
List<LatLng> _createPoints() {
final List<LatLng> points = <LatLng>[];
final int polygonCount = _polygons.length;
final double offset = polygonCount * 0.01;
points.add(_createLatLng(39.835334 + offset, 116.3710069));
points.add(_createLatLng(39.843082 + offset, 116.3709830));
points.add(_createLatLng(39.845932 + offset, 116.3642213));
points.add(_createLatLng(39.845924 + offset, 116.3595219));
points.add(_createLatLng(39.841562 + offset, 116.345568));
points.add(_createLatLng(39.835347 + offset, 116.34575));
return points;
}
void _add() {
final Polygon polygon = Polygon(
strokeColor: colors[++colorsIndex % colors.length],
fillColor: colors[++colorsIndex % colors.length],
strokeWidth: 15,
points: _createPoints(),
);
setState(() {
selectedPolygonId = polygon.id;
_polygons[polygon.id] = polygon;
});
}
void _remove() {
if (selectedPolygonId != null) {
//有选中的Marker
setState(() {
_polygons.remove(selectedPolygonId);
});
}
}
void _changeStrokeWidth() {
final Polygon? selectedPolygon = _polygons[selectedPolygonId];
if (selectedPolygon != null) {
double currentWidth = selectedPolygon.strokeWidth;
if (currentWidth < 50) {
currentWidth += 10;
} else {
currentWidth = 5;
}
//有选中的Polygon
setState(() {
_polygons[selectedPolygonId!] =
selectedPolygon.copyWith(strokeWidthParam: currentWidth);
});
} else {
print('无选中的Polygon无法修改宽度');
}
}
void _changeColors() {
final Polygon polygon = _polygons[selectedPolygonId]!;
setState(() {
_polygons[selectedPolygonId!] = polygon.copyWith(
strokeColorParam: colors[++colorsIndex % colors.length],
fillColorParam: colors[(colorsIndex + 1) % colors.length],
);
});
}
Future<void> _toggleVisible(value) async {
final Polygon polygon = _polygons[selectedPolygonId]!;
setState(() {
_polygons[selectedPolygonId!] = polygon.copyWith(
visibleParam: value,
);
});
}
void _changePoints() {
final Polygon polygon = _polygons[selectedPolygonId]!;
List<LatLng> currentPoints = polygon.points;
List<LatLng> newPoints = <LatLng>[];
newPoints.addAll(currentPoints);
newPoints.add(LatLng(39.828809, 116.360364));
setState(() {
_polygons[selectedPolygonId!] = polygon.copyWith(
pointsParam: newPoints,
);
});
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
final AMapWidget map = AMapWidget(
initialCameraPosition:
CameraPosition(target: LatLng(39.828809, 116.360364), zoom: 13),
onMapCreated: _onMapCreated,
polygons: Set<Polygon>.of(_polygons.values),
);
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.6,
width: MediaQuery.of(context).size.width,
child: map,
),
Expanded(
child: SingleChildScrollView(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
children: <Widget>[
Column(
children: <Widget>[
TextButton(
child: const Text('添加'),
onPressed: _add,
),
TextButton(
child: const Text('删除'),
onPressed:
(selectedPolygonId == null) ? null : _remove,
),
TextButton(
child: const Text('修改边框宽度'),
onPressed: (selectedPolygonId == null)
? null
: _changeStrokeWidth,
),
],
),
Column(
children: <Widget>[
TextButton(
child: const Text('修改边框和填充色'),
onPressed: (selectedPolygonId == null)
? null
: _changeColors,
),
AMapSwitchButton(
label: Text('显示'),
onSwitchChanged: (selectedPolygonId == null)
? null
: _toggleVisible,
defaultValue: true,
),
TextButton(
child: const Text('修改坐标'),
onPressed: (selectedPolygonId == null)
? null
: _changePoints,
),
],
),
],
)
],
),
),
),
],
),
);
}
}