web-dev-qa-db-fra.com

Minuterie de compte à rebours Flutter

Comment puis-je faire pour mettre la valeur passée dans la construction, pour faire un temporisateur qui arrondit à la première décimale et s'affiche dans le texte enfant de mon RaisedButton? J'ai essayé mais sans chance. Je parviens à faire fonctionner la fonction de rappel avec un simple Timer mais pas périodique et sans mise à jour de valeur en temps réel dans le texte ...

import 'package:flutter/material.Dart';
import 'Dart:ui';
import 'Dart:async';

class TimerButton extends StatefulWidget {
  final Duration timerTastoPremuto;


  TimerButton(this.timerTastoPremuto);

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

class _TimerButtonState extends State<TimerButton> {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(5.0),
      height: 135.0,
      width: 135.0,
      child: new RaisedButton(
        elevation: 100.0,
        color: Colors.white.withOpacity(.8),
        highlightElevation: 0.0,
        onPressed: () {
          int _start = widget.timerTastoPremuto.inMilliseconds;

          const oneDecimal = const Duration(milliseconds: 100);
          Timer _timer = new Timer.periodic(
              oneDecimal,
                  (Timer timer) =>
                  setState(() {
                    if (_start < 100) {
                      _timer.cancel();
                    } else {
                      _start = _start - 100;
                    }
                  }));

        },
        splashColor: Colors.red,
        highlightColor: Colors.red,
        //shape: RoundedRectangleBorder e tutto il resto uguale
        shape: BeveledRectangleBorder(
            side: BorderSide(color: Colors.black, width: 2.5),
            borderRadius: new BorderRadius.circular(15.0)),
        child: new Text(
          "$_start",
          style: new TextStyle(fontFamily: "Minim", fontSize: 50.0),
        ),
      ),
    );
  }
}
22
Floris Marpepa

ne répond pas directement à votre question. Mais utile pour ceux qui veulent commencer quelque chose après un certain temps.

Future.delayed(Duration(seconds: 1), () {
            print('yo hey');
          });
1
Soropromo