如何每次都停止构建

时间:2020-10-15 11:36:37

标签: flutter dart flutter-layout

嗨,我有一个带有倒计时计时器的颤动应用程序,我从这部视频https://www.youtube.com/watch?v=Rkkb8P9LsPw&list=PL26uY6-lIzqmfQO2iK5vTpmp5Iz_rHyr6&index=13中获得了它的效果,但是工作正常,但是当它计数时,它会导致小部件每秒重建一次,因此它会初始化为init状态,所以我不这样做不知道如何停止不必要的重建,只在显示计时器的地方重建,这将是自定义计时器代码

class TimeLeft {
  List<String> timeLeft(DateTime due) {
    List<String> retVal = List(2);

    Duration _timeUntilDue = due.difference(DateTime.now());

    //int _monthsUntil;
    int _daysUntil = _timeUntilDue.inDays;
    int _hoursUntil = _timeUntilDue.inHours - (_daysUntil * 24);
    int _minUntil =
        _timeUntilDue.inMinutes - (_daysUntil * 24 * 60) - (_hoursUntil * 60);
    int _secUntil = _timeUntilDue.inSeconds -
        (_daysUntil * 24 * 60 * 60) -
        (_hoursUntil * 60 * 60) -
        (_minUntil * 60);
    if (_daysUntil > 0) {
      retVal[0] =
          //_daysUntil > 30 ? '${double.parse('${_daysUntil/30}').toStringAsFixed(0)}' + ' month ' : '' +
          _daysUntil.toString() +
              " day" +
              '${_daysUntil > 1 ? 's' : ''}' +
              ' : ' +
              _hoursUntil.toString() +
              " hours : " +
              _minUntil.toString() +
              " mins : " +
              _secUntil.toString() +
              ' sec';
    } else if (_hoursUntil > 0) {
      retVal[0] = _hoursUntil.toString() +
          " hours : " +
          _minUntil.toString() +
          "mins : " +
          _secUntil.toString() +
          'sec';
    } else if (_minUntil > 0) {
      retVal[0] =
          _minUntil.toString() + " mins : " + _secUntil.toString() + 'sec';
    } else if (_secUntil > 0) {
      retVal[0] = _secUntil.toString() + ' : sec';
    } else {
      retVal[0] = 'Done';
    }

    Duration _timeUntilReveal =
        due.add(Duration(hours: 1)).difference(DateTime.now());
    int _daysUntilReveal = _timeUntilReveal.inDays;
    int _hoursUntilReveal = _timeUntilReveal.inHours - (_daysUntilReveal * 24);
    int _minUntilReveal = _timeUntilReveal.inMinutes -
        (_daysUntilReveal * 24 * 60) -
        (_hoursUntilReveal * 60);
    int _secUntilReveal = _timeUntilReveal.inSeconds -
        (_daysUntilReveal * 24 * 60 * 60) -
        (_hoursUntilReveal * 60 * 60) -
        (_minUntilReveal * 60);
    if (_daysUntilReveal > 0) {
      retVal[1] = _daysUntilReveal.toString() +
          " day" +
          '${_daysUntilReveal > 1 ? 's' : ''}';
      //_hoursUntilReveal.toString() +" hours";
      //_minUntilReveal.toString() +" mins" ;
      //_secUntilReveal.toString() +': sec';
    } else if (_hoursUntilReveal > 0) {
      retVal[1] = _hoursUntilReveal.toString() +
          " hours : " +
          _minUntilReveal.toString() +
          "mins ";
      //_secUntilReveal.toString() +'sec';
    } else if (_minUntilReveal > 0) {
      retVal[1] = _minUntilReveal.toString() +
          " mins : " +
          _secUntilReveal.toString() +
          'sec';
    } else if (_secUntilReveal > 0) {
      retVal[1] = _secUntilReveal.toString() + ' : sec';
    } else {
      retVal[1] = "Done";
    }
    return retVal;
  }
}

这是我使用它的页面

class _InProgressPageState extends State<InProgressPage> {
  Timer _timer;

  var value;

  List<String> _timeUntil = List(2);

  void _startTimer(DateTime time) {
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (this.mounted)
        return setState(() {
          _timeUntil =
              TimeLeft().timeLeft(DateTime.parse("2020-10-01 00:00:00Z"));
        });
    });
  }

  @override
  void initState() {
    _startTimer(DateTime.now());
    super.initState();
  }
    @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('Build InProgress');
    ThemeManagment themeManagment = Provider.of<ThemeManagment>(context);
    DataNotifier dataNotifier = Provider.of<DataNotifier>(context,listen: false);
    double size = MediaQuery.of(context).size.height;
    return Scaffold(
      backgroundColor: themeManagment.theme == ThemeType.Dark
          ? Color(0xFF161A26)
          : Colors.white
              .withOpacity(0.15),
      body: dataNotifier.dataList.length == 0
          ? Center(
              child: Text(
                'You Have No Inprogress Data',
                style: GoogleFonts.lato(
                  color: themeManagment.theme == ThemeType.Dark
                      ? Colors.white
                      : Color(0xFF161A26),
                  fontSize: 15,
                ),
              ),
            )
          : Padding(
              padding: const EdgeInsets.only(top: 8.0, bottom: 1),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                      child: ListView.builder(
                          itemCount: dataNotifier.dataList.length,
                          itemBuilder: (context, int i) => Padding(
                                padding: const EdgeInsets.only(
                                    bottom: 10, right: 10, left: 10, top: 5),
                                child: Material(
                                  borderRadius: BorderRadius.circular(20),
                                  elevation: 10,
                                  color: Colors.transparent,
                                  //shadowColor: Colors.lightBlueAccent,
                                  child: Container(
                                    decoration: BoxDecoration(
                                      color: themeManagment.theme ==
                                              ThemeType.Dark
                                          ? Color(0xFF161A26)
                                          :Colors.white.withOpacity(
                                                  0.85),
                                      borderRadius: BorderRadius.circular(20),
                                      border: Border.all(
                                        width: 1,
                                        color: themeManagment.theme ==
                                                ThemeType.Dark
                                            ? Colors.grey.withOpacity(0.1)
                                            : Colors.blueGrey.withOpacity(0.2),
                                      ),
                                    ),
                                    child: Padding(
                                      padding: const EdgeInsets.all(10),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceEvenly,
                                        children: [
                                         
                                          Column(
                                            children: [
                                              Padding(
                                                padding:
                                                    const EdgeInsets.all(8.0),
                                                child: Text(
                                                  'Time Left To Join',
                                                  style: GoogleFonts.lato(
                                                    fontSize: size / 54,
                                                    color:
                                                        themeManagment.theme ==
                                                                ThemeType.Dark
                                                            ? Colors.white
                                                            : Color(0xFF161A26),
                                                  ),
                                                ),
                                              ),
                                              Text(
                                                _timeUntil[0] ?? 'loading...',
                                                style: GoogleFonts.lato(
                                                  color: Color(
                                                      0xFF18DEBA), //Color(0xFFFF9A44),
                                                  fontSize: size / 40,
                                                ),
                                              ),
                                            ],
                                          ),

0 个答案:

没有答案