mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 14:58:34 +00:00
[platform_channels] adds EventChannel Demo (#462)
This commit is contained in:
37
platform_channels/lib/src/accelerometer_event_channel.dart
Normal file
37
platform_channels/lib/src/accelerometer_event_channel.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// This class includes the implementation for [EventChannel] to listen to value
|
||||
/// changes from the Accelerometer sensor from native side. It has a [readings]
|
||||
/// getter to provide a stream of [AccelerometerReadings].
|
||||
class Accelerometer {
|
||||
static final _eventChannel = const EventChannel('eventChannelDemo');
|
||||
|
||||
/// Method responsible for providing a stream of [AccelerometerReadings] to listen
|
||||
/// to value changes from the Accelerometer sensor.
|
||||
static Stream<AccelerometerReadings> get readings {
|
||||
return _eventChannel.receiveBroadcastStream().map(
|
||||
(dynamic event) => AccelerometerReadings(
|
||||
event[0] as double,
|
||||
event[1] as double,
|
||||
event[2] as double,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AccelerometerReadings {
|
||||
/// Acceleration force along the x-axis.
|
||||
final double x;
|
||||
|
||||
/// Acceleration force along the y-axis.
|
||||
final double y;
|
||||
|
||||
/// Acceleration force along the z-axis.
|
||||
final double z;
|
||||
|
||||
AccelerometerReadings(this.x, this.y, this.z);
|
||||
}
|
||||
58
platform_channels/lib/src/event_channel_demo.dart
Normal file
58
platform_channels/lib/src/event_channel_demo.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:platform_channels/src/accelerometer_event_channel.dart';
|
||||
|
||||
/// Demonstrates how to use [EventChannel] to listen continuous values
|
||||
/// of Accelerometer Sensor from platform.
|
||||
///
|
||||
/// The widget uses a [StreamBuilder] to rebuild it's descendant whenever it
|
||||
/// listens a new value from the [Accelerometer.readings] stream. It has three
|
||||
/// [Text] widgets to display the value of [AccelerometerReadings.x],
|
||||
/// [AccelerometerReadings.y], and [AccelerometerReadings.z] respectively.
|
||||
class EventChannelDemo extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final textStyle = Theme.of(context).textTheme.headline5;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('EventChannel Demo'),
|
||||
),
|
||||
body: Center(
|
||||
child: StreamBuilder<AccelerometerReadings>(
|
||||
stream: Accelerometer.readings,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Text(snapshot.error.toString());
|
||||
} else if (snapshot.hasData) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
'x axis: ' + snapshot.data.x.toStringAsFixed(3),
|
||||
style: textStyle,
|
||||
),
|
||||
Text(
|
||||
'y axis: ' + snapshot.data.y.toStringAsFixed(3),
|
||||
style: textStyle,
|
||||
),
|
||||
Text(
|
||||
'z axis: ' + snapshot.data.z.toStringAsFixed(3),
|
||||
style: textStyle,
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return Text(
|
||||
'No Data Available',
|
||||
style: textStyle,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user