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