1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
StreamBuilder<AccelerometerReadings>( stream: Accelerometer.readings, builder: (context, snapshot) { if (snapshot.hasError) { return Text((snapshot.error as PlatformException).message!); } 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, ); }, )
class Accelerometer { static const _eventChannel = EventChannel('eventChannelDemo');
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 { final double x;
final double y;
final double z;
AccelerometerReadings(this.x, this.y, this.z); }
|