1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-11 23:39:14 +00:00

[federated_plugin] adds platform interface implementation and plugin implementation for Android (#503)

This commit is contained in:
Ayush Bherwani
2020-07-29 09:17:36 +05:30
committed by GitHub
parent 3bc860f5df
commit 6d909874db
19 changed files with 622 additions and 101 deletions

View File

@@ -2,60 +2,74 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:federated_plugin/federated_plugin.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await FederatedPlugin.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
home: HomePage(),
);
}
}
/// Demonstrates how to use the getLocation method from federated_plugin to access
/// location data.
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Location location;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Federated Plugin Demo'),
),
body: Builder(
builder: (context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
location == null
? SizedBox.shrink()
: Text(
'Latitude: ${location.latitude}\n'
'Longitude: ${location.longitude}',
style: Theme.of(context).textTheme.headline5,
),
SizedBox(height: 16),
RaisedButton(
child: Text('Get Location'),
onPressed: () async {
try {
final result = await getLocation();
setState(() {
location = result;
});
} catch (error) {
Scaffold.of(context).showSnackBar(
SnackBar(
backgroundColor: Theme.of(context).primaryColor,
content: Text(error.message as String),
),
);
}
},
),
],
),
);
},
),
);
}

View File

@@ -64,6 +64,13 @@ packages:
relative: true
source: path
version: "0.0.1"
federated_plugin_platform_interface:
dependency: transitive
description:
path: "../../federated_plugin_platform_interface"
relative: true
source: path
version: "0.0.1"
flutter:
dependency: "direct main"
description: flutter
@@ -102,6 +109,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.2"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
sky_engine:
dependency: transitive
description: flutter

View File

@@ -2,23 +2,35 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:federated_plugin/federated_plugin.dart';
import 'package:federated_plugin_example/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:federated_plugin_example/main.dart';
void main() {
testWidgets('Verify Platform version', (tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());
group('federated plugin demo tests', () {
final location = Location(latitude: 131.0, longitude: 221.0);
setUpAll(() {
MethodChannel('location').setMockMethodCallHandler((call) async {
if (call.method == 'getLocation') {
return [location.longitude, location.latitude];
}
});
});
// Verify that platform version is retrieved.
expect(
find.byWidgetPredicate(
(widget) => widget is Text &&
widget.data.startsWith('Running on:'),
),
findsOneWidget,
);
testWidgets('get location from platform', (tester) async {
await tester.pumpWidget(MyApp());
// Tap button to get location from platform.
await tester.tap(find.byType(RaisedButton));
await tester.pumpAndSettle();
expect(
find.text('Latitude: ${location.latitude}\n'
'Longitude: ${location.longitude}'),
findsOneWidget,
);
});
});
}