Home > Mobile >  How to show a page in flutter at a specific date and time
How to show a page in flutter at a specific date and time

Time:09-03

how can I show a page in flutter at a specific date and time so I have an events app when the user sign in it will navigate him to a page that has a countdown to the date this I made but I want to know how to navigate him to the page when the time comes

CodePudding user response:

I think the solution is to display the said page while the return account will be terminated. To do so, you would have to set a condition like this

if event.date == DateTime.now() show page contents, else show countdown

CodePudding user response:

You can try this widget,


class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  DateTime specificDate = DateTime.now()
      .add(const Duration(seconds: 3)); //change based on your date.

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

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _todayIsTheDay();
    });
  }

  _todayIsTheDay() {
    Timer.periodic(const Duration(seconds: 1), (timer) {
      if (DateTime.now().difference(specificDate).inSeconds ==
          Duration.zero.inSeconds) {
        timer.cancel();
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) => const SpecificPage(),
          ),
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Home Page"),
      ),
    );
  }
}

class SpecificPage extends StatelessWidget {
  const SpecificPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Specific Page"),
      ),
    );
  }
}

I think this can be improved

  • Related