amap_map/example/lib/main.dart

149 lines
4.0 KiB
Dart
Raw Normal View History

2023-12-29 21:48:01 +08:00
import 'package:amap_map/amap_map.dart';
2023-12-30 07:54:59 +08:00
import 'package:amap_map_example/animated_category_item.dart';
import 'package:amap_map_example/category_list_item.dart';
2023-12-29 21:48:01 +08:00
import 'package:amap_map_example/const_config.dart';
2023-12-30 07:54:59 +08:00
import 'package:amap_map_example/data/demos.dart';
import 'package:amap_map_example/routes.dart';
import 'package:amap_map_example/theme.dart';
2023-12-22 21:23:24 +08:00
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
final List<Permission> needPermissionList = [
Permission.location,
Permission.storage,
Permission.phone,
];
class AMapDemo extends StatefulWidget {
@override
2023-12-30 07:54:59 +08:00
State<StatefulWidget> createState() => _AMapDemoState();
2023-12-22 21:23:24 +08:00
}
2023-12-30 07:54:59 +08:00
class _AMapDemoState extends State<AMapDemo>
with RestorationMixin, SingleTickerProviderStateMixin {
late AnimationController _animationController;
final RestorableBool _isMapListExpanded = RestorableBool(false);
2023-12-22 21:23:24 +08:00
@override
void initState() {
super.initState();
2023-12-29 21:48:01 +08:00
2023-12-22 21:23:24 +08:00
_checkPermissions();
2023-12-30 07:54:59 +08:00
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 800),
);
2023-12-22 21:23:24 +08:00
}
@override
void reassemble() {
super.reassemble();
_checkPermissions();
}
void _checkPermissions() async {
Map<Permission, PermissionStatus> statuses =
await needPermissionList.request();
statuses.forEach((key, value) {
print('$key premissionStatus is $value');
});
}
@override
Widget build(BuildContext context) {
2024-01-04 18:48:31 +08:00
AMapInitializer.init(context, apiKey: ConstConfig.amapApiKeys);
2023-12-29 21:48:01 +08:00
AMapInitializer.updatePrivacyAgree(ConstConfig.amapPrivacyStatement);
2023-12-22 21:23:24 +08:00
return Scaffold(
appBar: AppBar(title: const Text('高德地图示例')),
2023-12-30 07:54:59 +08:00
body: ListView(
children: [
AnimatedCategoryItem(
startDelayFraction: 0,
controller: _animationController,
child: CategoryListItem(
category: DemoCategory.basic,
demos: mapDemos(),
)),
AnimatedCategoryItem(
startDelayFraction: 0.05,
controller: _animationController,
child: CategoryListItem(
category: DemoCategory.interactive,
demos: interactiveDemos(),
)),
AnimatedCategoryItem(
startDelayFraction: 0.1,
controller: _animationController,
child: CategoryListItem(
category: DemoCategory.overlay,
demos: overlayDemos(),
)),
2023-12-30 16:42:18 +08:00
AnimatedCategoryItem(
startDelayFraction: 0.15,
controller: _animationController,
child: CategoryListItem(
category: DemoCategory.extension,
demos: extensionDemos(),
)),
2023-12-30 07:54:59 +08:00
],
2023-12-22 21:23:24 +08:00
),
);
}
2023-12-30 07:54:59 +08:00
@override
String? get restorationId => 'demos';
@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(_isMapListExpanded, 'map_list');
}
2023-12-22 21:23:24 +08:00
}
void main() {
2023-12-30 07:54:59 +08:00
runApp(MaterialApp(
theme: ThemeData(colorScheme: DemoThemeData.lightColorScheme),
themeMode: ThemeMode.light,
onGenerateRoute: RouteConfig.onGenerateRoute,
home: AMapDemo()));
}
enum DemoCategory {
basic,
interactive,
2023-12-30 16:42:18 +08:00
overlay,
extension;
2023-12-30 07:54:59 +08:00
String toDisplayTitle() {
switch (this) {
case basic:
return '创建地图';
case interactive:
return '与地图交互';
case overlay:
return '在地图上绘制';
2023-12-30 16:42:18 +08:00
case extension:
return '拓展插件';
2023-12-30 07:54:59 +08:00
}
}
}
class Demo {
const Demo({
required this.title,
required this.category,
required this.subtitle,
// Parameters below are required for non-study demos.
this.slug,
this.configurations = const [],
}) : assert(slug != null);
final String title;
final DemoCategory category;
final String subtitle;
final String? slug;
final List<DemoConfiguration> configurations;
String get describe => '$slug@${category.name}';
2023-12-22 21:23:24 +08:00
}