dencity_map_plugin 0.0.4
dencity_map_plugin: ^0.0.4 copied to clipboard
An Android Native Plugin Support for Dencitylite Android Applications
example/lib/main.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String distanceCallResponse = "Not Avaiable";
static const distanceCallChannel = const MethodChannel(
'com.dencity.dencity_map_plugin/search_screen_distance');
@override
void initState() {
super.initState();
requestLocationPermission();
getPlatformVersion();
}
Future<void> requestLocationPermission() async {
PermissionStatus status = await Permission.location.request();
if (status.isGranted) {
// The location permission is granted. You can now access the user's location.
} else if (status.isDenied) {
// The location permission is denied. You can't access the user's location.
} else if (status.isPermanentlyDenied) {
// The location permission is permanently denied. The user needs to enable it in the settings.
}
}
Future<void> getPlatformVersion() async {
try {
distanceCallChannel.setMethodCallHandler((call) async {
switch (call.method) {
case 'getDistance':
setState(() {
distanceCallResponse = call.arguments['key'];
});
print('Distance call received ${call.arguments['key']}');
return Future.value('Distance call received');
default:
throw MissingPluginException();
}
});
} on PlatformException {
setState(() {
distanceCallResponse = 'Failed to get distance.';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(distanceCallResponse),
),
body: PlatformViewLink(
viewType: 'com.dencity.dencity_map_plugin/map_view',
surfaceFactory: (context, controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: const <Factory<
OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: 'com.dencity.dencity_map_plugin/search_screen_view',
layoutDirection: TextDirection.ltr,
creationParams: const <String, dynamic>{
"origin": "48.8566, 2.3522",
"destination": "48.8606, 2.3376",
"apiKey": "GOOGLE_MAPS_API_KEY",
"parkingLotsURL": "API_FOR_PARKING_LOTS",
},
creationParamsCodec: const StandardMessageCodec(),
onFocus: () {
params.onFocusChanged(true);
},
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..create();
},
),
),
);
}
}