main.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'dart:async';
  2. import 'package:amap_location/amap_export.dart';
  3. import 'package:amap_location_example/map_view_page.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. void main() {
  8. WidgetsFlutterBinding.ensureInitialized();
  9. AmapLocation.registerView();
  10. runApp(MyApp());
  11. }
  12. class MyApp extends StatelessWidget {
  13. @override
  14. Widget build(BuildContext context) {
  15. return MaterialApp(
  16. home: HomePage()
  17. );
  18. }
  19. }
  20. class HomePage extends StatefulWidget {
  21. @override
  22. State<StatefulWidget> createState() {
  23. return HomePageState();
  24. }
  25. }
  26. class HomePageState extends State<HomePage> {
  27. String _platformVersion = 'Unknown';
  28. @override
  29. void initState() {
  30. super.initState();
  31. initPlatformState();
  32. }
  33. // Platform messages are asynchronous, so we initialize in an async method.
  34. Future<void> initPlatformState() async {
  35. String platformVersion;
  36. // Platform messages may fail, so we use a try/catch PlatformException.
  37. try {
  38. platformVersion = await AmapLocation.platformVersion;
  39. } on PlatformException {
  40. platformVersion = 'Failed to get platform version.';
  41. }
  42. // If the widget was removed from the tree while the asynchronous platform
  43. // message was in flight, we want to discard the reply rather than calling
  44. // setState to update our non-existent appearance.
  45. if (!mounted) return;
  46. setState(() {
  47. _platformVersion = platformVersion;
  48. });
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return Scaffold(
  53. appBar: AppBar(
  54. title: const Text('Plugin example app'),
  55. ),
  56. body: Center(
  57. child: Column(
  58. mainAxisAlignment: MainAxisAlignment.center,
  59. children: <Widget>[
  60. RawMaterialButton(
  61. onPressed: () async {
  62. await AmapLocation.instance.startLocation(
  63. options: LocationOption(
  64. isOnceLocation: false,
  65. locationInterval: 5000,
  66. locationTimeOut: 10,
  67. locationMode: LocationMode.Battery_Saving,));
  68. AmapLocation.instance.locationStream.listen((d) {
  69. print(d.city);
  70. // stop location when isOnceLocation is true
  71. // AmapLocation.instance.disposed();
  72. });
  73. await Future.delayed(Duration(seconds: 60));
  74. AmapLocation.instance.disposed();
  75. },
  76. child: Text("get lcoation"),
  77. ),
  78. SizedBox(height: 10,),
  79. RawMaterialButton(
  80. onPressed: () async {
  81. Navigator.of(context).push(MaterialPageRoute(builder: (ctx){
  82. return MapViewPage();
  83. }));
  84. },
  85. child: Text("jump to mapview"),
  86. ),
  87. ],
  88. ),
  89. ),
  90. );
  91. }
  92. }