How to Access Custom Hook functions from another class

Viewed 20

i have a custom hook timer_hook.dart The way this hooks works is whenever the user open's up the page, the counter begins to count down, so i am trying to access the method "restartTimer()" from the main class where i am using the hook. but i dont know how to achieve that, sorry i am still learning flutter. and i don't want to use the stateful widget.

import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

Duration useCountDownTimer() {
  return use(const CountdownTimer());
}

class CountdownTimer extends Hook<Duration> {
  const CountdownTimer();

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

class _CountdownTimerState extends HookState<Duration, CountdownTimer> {
  Timer? _timer;
  Duration _duration = const Duration(seconds: 6);

  void countDownTime() {
    const reduceSeconds = 1;
    setState(() {
      final seconds = _duration.inSeconds - reduceSeconds;
      _duration = Duration(seconds: seconds);

      if (_duration.inSeconds == 0) {
        _timer?.cancel();
      }
    });
  }

  void restartTimer() {
    setState(() {
      _duration = const Duration(seconds: 90);
      _timer =
          Timer.periodic(const Duration(seconds: 1), (_) => countDownTime());
    });
  }

  @override
  void initHook() {
    super.initHook();
    _timer = Timer.periodic(const Duration(seconds: 1), (_) => countDownTime());
  }

  @override
  Duration build(BuildContext context) {
    return _duration;
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }
}
0 Answers
Related