mirror of
https://github.com/flutter/samples.git
synced 2026-03-31 16:55:34 +00:00
[federated_plugin_sample] modify the sample to expose battery API instead of geolocation API (#526)
This commit is contained in:
@@ -18,15 +18,15 @@ class MyApp extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// Demonstrates how to use the getLocation method from federated_plugin to access
|
||||
/// location data.
|
||||
/// Demonstrates how to use the getBatteryLevel method from federated_plugin to retrieve
|
||||
/// current battery level of device.
|
||||
class HomePage extends StatefulWidget {
|
||||
@override
|
||||
_HomePageState createState() => _HomePageState();
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
Location location;
|
||||
int batteryLevel;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -40,21 +40,20 @@ class _HomePageState extends State<HomePage> {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
location == null
|
||||
batteryLevel == null
|
||||
? SizedBox.shrink()
|
||||
: Text(
|
||||
'Latitude: ${location.latitude}\n'
|
||||
'Longitude: ${location.longitude}',
|
||||
'Battery Level: $batteryLevel',
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
RaisedButton(
|
||||
child: Text('Get Location'),
|
||||
child: Text('Get Battery Level'),
|
||||
onPressed: () async {
|
||||
try {
|
||||
final result = await getLocation();
|
||||
final result = await getBatteryLevel();
|
||||
setState(() {
|
||||
location = result;
|
||||
batteryLevel = result;
|
||||
});
|
||||
} catch (error) {
|
||||
Scaffold.of(context).showSnackBar(
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// 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';
|
||||
@@ -10,27 +9,23 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('federated plugin demo tests', () {
|
||||
final location = Location(latitude: 131.0, longitude: 221.0);
|
||||
final batteryLevel = 45;
|
||||
setUpAll(() {
|
||||
MethodChannel('location').setMockMethodCallHandler((call) async {
|
||||
if (call.method == 'getLocation') {
|
||||
return [location.longitude, location.latitude];
|
||||
MethodChannel('battery').setMockMethodCallHandler((call) async {
|
||||
if (call.method == 'getBatteryLevel') {
|
||||
return batteryLevel;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('get location from platform', (tester) async {
|
||||
testWidgets('get current battery level from platform', (tester) async {
|
||||
await tester.pumpWidget(MyApp());
|
||||
|
||||
// Tap button to get location from platform.
|
||||
// Tap button to retrieve current battery level from platform.
|
||||
await tester.tap(find.byType(RaisedButton));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
find.text('Latitude: ${location.latitude}\n'
|
||||
'Longitude: ${location.longitude}'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text('Battery Level: $batteryLevel'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user