main.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import 'dart:async';
  2. import 'package:amap_location/amap_export.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. void main() => runApp(MyApp());
  6. class MyApp extends StatefulWidget {
  7. @override
  8. _MyAppState createState() => _MyAppState();
  9. }
  10. class _MyAppState extends State<MyApp> {
  11. String _platformVersion = 'Unknown';
  12. @override
  13. void initState() {
  14. super.initState();
  15. initPlatformState();
  16. }
  17. // Platform messages are asynchronous, so we initialize in an async method.
  18. Future<void> initPlatformState() async {
  19. String platformVersion;
  20. // Platform messages may fail, so we use a try/catch PlatformException.
  21. try {
  22. platformVersion = await AmapLocation.platformVersion;
  23. } on PlatformException {
  24. platformVersion = 'Failed to get platform version.';
  25. }
  26. // If the widget was removed from the tree while the asynchronous platform
  27. // message was in flight, we want to discard the reply rather than calling
  28. // setState to update our non-existent appearance.
  29. if (!mounted) return;
  30. setState(() {
  31. _platformVersion = platformVersion;
  32. });
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return MaterialApp(
  37. home: Scaffold(
  38. appBar: AppBar(
  39. title: const Text('Plugin example app'),
  40. ),
  41. body: Center(
  42. child: RawMaterialButton(
  43. onPressed: () async {
  44. await AmapLocation.instance.startLocation(
  45. options: LocationOption(
  46. isOnceLocation: false,
  47. locationInterval: 5000,
  48. ));
  49. AmapLocation.instance.locationStream.listen((d) {
  50. print(d.city);
  51. // stop location when isOnceLocation is true
  52. // AmapLocation.instance.disposed();
  53. });
  54. await Future.delayed(Duration(seconds: 60));
  55. AmapLocation.instance.disposed();
  56. },
  57. child: Text("get lcoation"),
  58. ),
  59. ),
  60. ),
  61. );
  62. }
  63. }