Search code examples
flutterdartcountdown

Flutter : How to automatically change the page after passing a certain time?


Thank you for reading my question and taking your time. I am trying to add a function that makes you go back to the home screen after a certain time passes in Flutter. I have struggled with this problem for 3 weeks so I came here.

There are 4 pages.

  • HomeScreen()
  • SecondRoute()
  • ThirdRoute()
  • ForthRoute()

When you go to the next page or SecondRoute(), a timer starts. If you go to the next page or ThirdRoute(), the timer restarts. I succeeded in adding this timer function, but the page transition never happened after reaching 0.



1. My codes


Here are my codes below.

mian.dart



import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:question/fourth_route.dart';
import 'package:question/second_route.dart';
import 'package:question/third_route.dart';
import 'package:question/timer_store.dart';

import 'home_screen.dart';

void main() {
  runApp(ChangeNotifierProvider(
    create: (context) => TimerStore(),
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Question',
      theme: ThemeData.dark(),
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/second_route': (context) => SecondRoute(),
        '/third_route': (context) => ThirdRoute(),
        '/fourth_route': (context) => FourthRoute(),
      },
    );
  }
}

time_store.dart

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

import 'home_screen.dart';

class TimerStore with ChangeNotifier {
  static const COUNTER_VALUE = 10; // TODO: ユーザーが設定できるように
  var count = COUNTER_VALUE;
  static const sec = const Duration(seconds: 1);
  static const ms = const Duration(milliseconds: 1);
  Timer _timer;

  startTimer() {
    if (_timer != null) {
      _timer.cancel();
    }

    count = COUNTER_VALUE;
    _timer = Timer.periodic(sec, (timer) {
      updateCounter();
    });
  }

  void updateCounter() {
    if (count > 0) {
      count--;
      print(count);
    } else {
      _timer.cancel();
      updateCounter2();
      updateCounter4;
    }
    notifyListeners();
  }

//追加
  void updateCounter2() {
    print('page trangition');
  }

  void updateCounter3(BuildContext context) {
    Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => HomeScreen(),
    ));
  }

  void updateCounter4(BuildContext context){
    Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => HomeScreen()));
  }



  @override
  void dispose() {
    super.dispose();
    _timer.cancel();
  }

//追加 ---
  void stopTimer() {
    _timer.cancel();
  }

  void restartTimer() {
    startTimer();
  }
}




2. Problems


In order to see if the function is working, I used "print" and checked the output in the console. I could see the count down and "page transition" word of updateCounter2().

enter image description here

  void updateCounter() {
    if (count > 0) {
      count--;
      print(count);
    } else {
      _timer.cancel();
      updateCounter2();
      updateCounter4;
    }
    notifyListeners();
  }

//追加
  void updateCounter2() {
    print('page trangition');
  }

However, updateCounter4 did not work. I know this looks strange because it should have () like updateCounter4(). But if I do that I got an error saying,

1 positional argument(s) expected, but 0 found. Try adding the missing arguments.

enter image description here

I know updateCounter4 needs arguments which is context. But I am not sure what exactly should be added in the() of updateCounter4...

I put "updateCounter4(context)" but it did not work...



Hope you understand my question and provide me advice. If it is not clear enough, happy to explain the details.
Thank you so much.


Solution

  • you can use Timer for that

     @override
    void initState() {
     Timer(Duration(seconds: 3), (){
    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context)=> 
     HomeScreen()));
    });
     super.initState();
    }
    

    this will route you to your home screen after 3 seconds

    and make sure to import dart:async at the top of your file

    import 'dart:async';