带时间的计步器-颤振

时间:2020-06-01 11:07:18

标签: flutter dart flutter-dependencies pedometer

我需要计算用户执行的步骤以及每个步骤的时间!

我尝试使用Flutter的计步器,但是已经遇到了一些问题。我无法重新启动计步器,并且计数器似乎非常慢...我的意思是,我走了几步,只有在计数器更新一段时间后,我才希望每一步都马上开始。另一件事是,我需要获取每个步骤所花费的时间。您认为我可以用计步器吗?

示例: 步骤1-1300毫秒, 第2步-1340毫秒, 第3步-1240毫秒, 第4步-1500毫秒, 第5步-1330毫秒, ...

这是我的代码:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:pedometer/pedometer.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Pedometer _pedometer;
  StreamSubscription<int> _subscription;
  String _stepCountValue = '?';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    startListening();
  }

  void onData(int stepCountValue) {
    print(stepCountValue);
  }

  void startListening() {
    setState(() {
      _pedometer = new Pedometer();
      _subscription = _pedometer.pedometerStream.listen(_onData,
          onError: _onError, onDone: _onDone, cancelOnError: true);
    });
  }

// To restart the pedometer, but it doesn't work
  void change(){
    setState(() {
      _pedometer = new Pedometer();
      _stepCountValue = '?';
    },);
  }

  void stopListening() {
    _subscription.cancel();
  }

  void _onData(int newValue) async {
    print('New step count value: $newValue');
    setState(() => _stepCountValue = "$newValue");
  }

  void _onDone() => print("Finished pedometer tracking");

  void _onError(error) => print("Flutter Pedometer Error: $error");

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
          appBar: new AppBar(
            title: const Text('Pedometer example app'),
          ),
          body: Center(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Icon(
                Icons.directions_walk,
                size: 90,
              ),
              new Text(
                'Steps taken:',
                style: TextStyle(fontSize: 30),
              ),
              new Text(
                '$_stepCountValue',
                style: TextStyle(fontSize: 100, color: Colors.blue),
              ),
              RaisedButton(
                child: Text('Restart'),
                color: Theme.of(context).primaryColor,
                textColor: Theme.of(context).textTheme.button.color,
                onPressed: change,
              ),
            ],
          ))),
    );
  }
}

0 个答案:

没有答案