简体   繁体   中英

Flutter How to navigate from page to page without any users Interaction

I'm writing a flutter application and i want to change the page without any users interaction, i wrote a timer methode and i want to change the page when the timer is 0 Timer _timer; int _start = 5;

    void startTimer() {
   const oneSec = const Duration(seconds: 1);
  _timer = new Timer.periodic(
  oneSec,
  (Timer timer) => setState(
    () {
      if (_start < 1) {
        timer.cancel();
      } else {
        _start = _start - 1;
      }
    },
  ),
);
   }
   @override
  void initState() {
     super.initState();
         startTimer();
     }

the timer wille be from 5 to 0 i want to change the page when start wil be 0 And thankyou

It is a better approach to go with Visibility widget rather route.
You can replace your any widget with the Text widget in demo.
check out demo code below:

// main.dart
import 'dart:async';

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CountDownDemo(),
    );
  }
}

class CountDownDemo extends StatefulWidget {
  @override
  _CountDownDemoState createState() => _CountDownDemoState();
}

class _CountDownDemoState extends State<CountDownDemo> {
  int _start = 5;

  void startTimer() {
    const oneSec = const Duration(seconds: 1);
    Timer.periodic(
      oneSec,
      (Timer timer) => setState(
        () {
          if (_start < 1) {
            timer.cancel();
          } else {
            _start = _start - 1;
          }
        },
      ),
    );
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Countdown Demo"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Visibility(
              child: Text("Text 5"),
              visible: _start == 5 ? true : false,
            ),
            Visibility(
              child: Text("Text 4"),
              visible: _start == 4 ? true : false,
            ),
            Visibility(
              child: Text("Text 3"),
              visible: _start == 3 ? true : false,
            ),
            Visibility(
              child: Text("Text 2"),
              visible: _start == 2 ? true : false,
            ),
            Visibility(
              child: Text("Text 1"),
              visible: _start == 1 ? true : false,
            ),
            Visibility(
              child: Text("Timer is complete"),
              visible: _start == 0 ? true : false,
            ),
          ],
        ),
      ),
    );
  }
}

Output:
带有可见性小部件和计时器的演示输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM