heart_rate 0.0.1+1 copy "heart_rate: ^0.0.1+1" to clipboard
heart_rate: ^0.0.1+1 copied to clipboard

Flutter plugin to measure heart rate using smartphone camera for both Android and IOS

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:heart_rate/heart_rate.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Heart Rate Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<SensorValue> data = [];
  List<SensorValue> bpmValues = [];
  bool isBPMEnabled = false;

  void _toggleBPM() {
    setState(() {
      isBPMEnabled = !isBPMEnabled;
      if (!isBPMEnabled) {
        data.clear();
        bpmValues.clear();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Heart Rate Demo'),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Card(
                clipBehavior: Clip.antiAlias,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12)),
                child: Column(
                  children: [
                    if (isBPMEnabled)
                      AspectRatio(
                        aspectRatio: 1,
                        child: HeartRateDialog(
                          onRawData: (value) {
                            setState(() {
                              if (data.length >= 100) data.removeAt(0);
                              data.add(value);
                            });
                          },
                          onBPM: (value) {
                            if (value > 30 && value < 200) {
                              setState(() {
                                if (bpmValues.length >= 100) {
                                  bpmValues.removeAt(0);
                                }
                                bpmValues.add(SensorValue(
                                    value: value.toDouble(),
                                    time: DateTime.now()));
                              });
                            }
                          },
                        ),
                      ),
                    Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            "Heart Rate",
                            style: Theme.of(context).textTheme.titleLarge,
                          ),
                          Text(
                            bpmValues.isNotEmpty
                                ? "${bpmValues.last.value.round()} BPM"
                                : "---",
                            style: Theme.of(context)
                                .textTheme
                                .headlineMedium
                                ?.copyWith(fontWeight: FontWeight.bold),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 16),
            ],
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: FloatingActionButton.extended(
        onPressed: _toggleBPM,
        label: Text(isBPMEnabled ? "Stop Measurement" : "Measure Heart Rate"),
        icon: Icon(isBPMEnabled ? Icons.stop : Icons.favorite_rounded),
        backgroundColor:
            isBPMEnabled ? Colors.redAccent : Colors.white,
      ),
    );
  }
}
2
likes
140
points
3
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter plugin to measure heart rate using smartphone camera for both Android and IOS

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

camera, flutter, permission_handler

More

Packages that depend on heart_rate