Show menu

main.dart

  void main() => runApp(DemoApp());

  class DemoApp extends StatelessWidget {
    const DemoApp();

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("Using sensors"),
          ),
          body: const Center(
            child: SensorWidget(),
          )
        ),

      );
    }

  }
  

chip_icon.dart

  class ChipWithIcon extends StatelessWidget {
    final Icon icon;
    final String text;
    const ChipWithIcon({
      required this.icon,
      required this.text
    });

    @override
    Widget build(BuildContext context) {
      return Chip(
        avatar: icon,
        padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
        labelPadding: const EdgeInsets.only(left: 10),
        label: Text(text),
      );
    }
  }
  

accelerometer.dart

  class AccelerometerData extends StatelessWidget {
    static final initialData = AccelerometerEvent(0, 0, 0);
    const AccelerometerData();

    @override
    Widget build(BuildContext context) {
      return StreamBuilder<AccelerometerEvent>(
        stream: accelerometerEvents,
        initialData: initialData,
        builder: (context, accel) {
          final data = accel.data;

          if (data != null) {
            final x = data.x.toStringAsFixed(2);
            final y = data.y.toStringAsFixed(2);
            final z = data.z.toStringAsFixed(2);

            return ChipWithIcon(
                icon: Icon(Icons.drive_eta),
                text: "Accelerometer: $x, $y, $z"
            );
          }

          return const Text("No data to display.");
        },
      );
    }
  }
  

useraccelerometer.dart

  class UserAccelerometerData extends StatelessWidget {
    static final initialData = UserAccelerometerEvent(0, 0, 0);
    const UserAccelerometerData();

    @override
    Widget build(BuildContext context) {
      return StreamBuilder<UserAccelerometerEvent>(
        stream: userAccelerometerEvents,
        initialData: initialData,
        builder: (context, accel) {
          final data = accel.data;

          if (data != null) {
            final x = data.x.toStringAsFixed(2);
            final y = data.y.toStringAsFixed(2);
            final z = data.z.toStringAsFixed(2);

            return ChipWithIcon(
                icon: Icon(Icons.drive_eta),
                text: "User Accelerometer: $x, $y, $z"
            );
          }

          return const Text("No data to display.");
        },
      );
    }
  }
  

gyroscope.dart

  class GyroscopeData extends StatelessWidget {
    static final initialData = GyroscopeEvent(0, 0, 0);
    const GyroscopeData();

    @override
    Widget build(BuildContext context) {
      return StreamBuilder<GyroscopeEvent>(
        stream: gyroscopeEvents,
        initialData: initialData,
        builder: (context, accel) {
          final data = accel.data;

          if (data != null) {
            final x = data.x.toStringAsFixed(2);
            final y = data.y.toStringAsFixed(2);
            final z = data.z.toStringAsFixed(2);

            return ChipWithIcon(
                icon: Icon(Icons.compass_calibration),
                text: "User Accelerometer: $x, $y, $z"
            );
          }

          return const Text("No data to display.");
        },
      );
    }
  }
  

This website and the book are not official Google products. No affiliations are involved. Built with Java 14 and Vert.X

"Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC"