skip to Main Content

I’m new on learning flutter and I’m trying do build my app, but I’m stuck on a simpley function. The gesture detecion does not work. I get no errors, but when i click on the button, nothing happens, I would like to move from the intro_page to home.
Can someone find the error?

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

class IntroPage extends StatelessWidget {
  const IntroPage({super.key});

  get navigator => null;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue,
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(25),
          child: Column(
            children: [
              // Abstand
              const SizedBox(height: 180),

              // title
              const Text('Trade App',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 32,
                    color: Colors.white,
                  )),

              //subtitele
              const Text('Verkaufen und Kaufen',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 18,
                    color: Color.fromARGB(255, 255, 255, 255),
                  )),

              //Abstand
              const SizedBox(height: 50),
              // start button
              GestureDetector(
                onTap: () => navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const Home(),
                  ),
                ),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(30),
                  ),
                  padding: const EdgeInsets.all(20),
                  child: const Text('Loslegen'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

I tried to read in several forums and watch videos, but I don’t know how to solve it

2

Answers


  1. Yes, it’s expected !

    what does this getter function do in your code : get navigator => null;

    and, in your callback you call this navigator getter which returns null:

    onTap: () => navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const Home(),
                      ),
                    ),
    

    TO FIX

    You have 2 options, you can either remove this getter and use Navigator.of(context), or return this navigator from your getter as the following:

    get navigator=>Navigator.of(context);
    

    But, how could you reach the context !, so i recommend to remove this getter.

    Login or Signup to reply.
  2. I don’t understand why you created a null navigator object , instead of this you can try just use Navigator class like below

    Navigator.push(context,MaterialPageRoute( builder: (context) => const Home(),),),

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search