-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[platform_channels] adds EventChannel Demo #462
Conversation
testWidgets('EventChannel accelerometer Stream test', (tester) async { | ||
await tester.pumpWidget(MaterialApp( | ||
home: EventChannelDemo(), | ||
)); | ||
|
||
expect(find.text('No Data Available'), findsOneWidget); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how can I mock the EventChannel.recieveBroadcastStream
. Suggestions and ideas for testing are much appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The EventChannel
test in the framework helped me.
builder: (context, snapshot) { | ||
if (snapshot.hasData) { | ||
return Center( | ||
child: Column( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestions for improving UI are welcome. We can also have three Contianer
s which change properties when a value changes. But since the sample is for learning purpose I kept it simple enough. I'm happy to iterate over sample to improve the UI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed offline, I think you could do something simple with Animated Container, which always manages to make UI look cool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RedBrogdon Instead of using AnimatedContainer
I tried Transform
to rotate the containers whenever the value changes. There are three containers which resemble 3 Axis. Each Container
rotates on a single axis.
GIF
The above example works fine on the Emulator, but when testing on the real device, the value changes too fast and hence the Container
s keep rotating even while you are holding the phone.
I also tried with the AnimatedContainer
, based on the values of x and y-axis from the Stream I was changing the height and width of the AnimatedContainer
respectively, and on the value of z-axis, I'm changing the borderRadius
, which works fine on the real device too.
GIF
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The recent commit uses AnimatedContainer
example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great. I have a few notes, but nothing huge.
private lateinit var eventSink: EventChannel.EventSink | ||
|
||
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { | ||
if (events != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that events
could ever be null here without something having gone very, very wrong. If you're going to have a null check, I would probably put the registerListener
call in there as well. No point in having the listener if you can't send events back.
// Creates a EventChannel, and sets a StreamHandler. The AccelerometerStreamHandler | ||
// implements a StreamHandler and SensorEventListener to listen the value changes from | ||
// the sensor and send it to dart side. | ||
EventChannel(flutterEngine.dartExecutor, "accelerometer") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider aligning the names of the two channels a little more. The previous one is "methodChannelDemo," and this one is "accelerometer" rather than "eventChannelDemo" or something similar. You could always rename the method channel one to "counter."
@@ -27,5 +35,14 @@ class MainActivity : FlutterActivity() { | |||
} | |||
} | |||
} | |||
|
|||
val sensorManger: SensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager | |||
// Use instance of Sensor Service to get the ACCELEROMETER sensor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend cutting the comments on line 40 and 42-44. Your code is easy to read and very direct. I don't think people need the comments to understand what you're doing.
|
||
import 'package:flutter/services.dart'; | ||
|
||
/// This class includes the implementation for [EventChannel] to listen value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "listen to value" and "from the accelerometer."
class Accelerometer { | ||
static final _eventChannel = const EventChannel('accelerometer'); | ||
|
||
/// Method responsible to provide a stream of [AccelerometerReadings] to listen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "responsible for providing" and "to listen to"
/// Acceleration force along the z-axis. | ||
final double z; | ||
|
||
AccelerometerReadings(this.x, this.y, this.z); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional suggestion, but you could make this a const constructor.
import 'package:flutter/material.dart'; | ||
import 'package:platform_channels/src/accelerometer_event_channel.dart'; | ||
|
||
/// The widget demonstrates how to use [EventChannel] to listen continuous values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're getting way into the Dart style guide here, but it's recommended to start class comments like these with a verb:
In this case, you'd just drop "the widget" from the beginning.
/// The widget 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, "the widget" is fine. It's just for the initial sentence that the noun is typically dropped.
builder: (context, snapshot) { | ||
if (snapshot.hasData) { | ||
return Center( | ||
child: Column( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed offline, I think you could do something simple with Animated Container, which always manages to make UI look cool.
body: StreamBuilder<AccelerometerReadings>( | ||
stream: Accelerometer.getReadings(), | ||
builder: (context, snapshot) { | ||
if (snapshot.hasData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider checking for errors and displaying them as well. It's a good practice, and one some devices it's possible a user could get one.
@amirh, would you mind taking a quick look at the tests in |
final height = 150 + snapshot.data.x * 10; | ||
final borderRadius = snapshot.data.z; | ||
|
||
return AnimatedContainer( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes you've made everywhere else are solid, so I think the only remaining question is what to do about displaying the data. While the AnimatedContainer code you have here is clean, valid, Flutter, I don't think it's obvious to someone looking at the app how the container visually relates to the numbers coming out of the accelerometer (I don't have the eyes to see small changes in border radius, for example).
For now, I would consider just going back to Text widgets, so you can put your time into finishing out the rest of the app (and the plugin app). Once those are done, you'll likely have time to come back to this section of the code, and (probably) you'll have had an idea for a great way to present the data.
That's just a suggestion, though. If you want to iterate on a few designs and try to find a great one, that's cool. You might try an animated bar chart, similar to what audio equalizers do, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes you've made everywhere else are solid, so I think the only remaining question is what to do about displaying the data. While the AnimatedContainer code you have here is clean, valid, Flutter, I don't think it's obvious to someone looking at the app how the container visually relates to the numbers coming out of the accelerometer (I don't have the eyes to see small changes in border radius, for example).
Completely makes sense to me.
For now, I would consider just going back to Text widgets, so you can put your time into finishing out the rest of the app (and the plugin app). Once those are done, you'll likely have time to come back to this section of the code, and (probably) you'll have had an idea for a great way to present the data.
SGTM, as we finish the project we can iterate over the design. Also, if I come across any idea for displaying data, I will submit a separate PR for that, so we don't have our progress on hold.
Thanks for some more great code! |
Description
Adds an
EventChannel
Demo where it streams the value of Accelerometer sensor.GIF