Home > OS >  Flutter: press button not working to change page
Flutter: press button not working to change page

Time:12-06

Hello Everyone I Still learning about flutter why on my code press function for change page not working i was try to use routeName flow to change each page and here's my code

sign_in_screen.dart

    import 'package:flutter/material.dart';
    
    class SignInScreen extends StatelessWidget {
      static String routeName = "/sign_in";
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
        );
      }
    }

routs.dart

import 'package:accone/screens/splash/splash_screen.dart';
import 'package:accone/screens/sign_in/sign_in_screen.dart';
import 'package:flutter/widgets.dart';

final Map<String, WidgetBuilder> routes = {
  SplashScreen.routeName: (context) => SplashScreen(),
  SignInScreen.routeName: (context) => SignInScreen(),
};

and the button on body.dart

 @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: SizedBox(
            width: double.infinity,
            child: Column(
              children: <Widget>[
                Expanded(
                  flex: 3,
                  child: PageView.builder(
                      onPageChanged: (value) {
                        setState(() {
                          currentPage = value;
                        });
                      },
                      itemCount: splashData.length,
                      itemBuilder: (context, index) => SplashContent(
                          image: splashData[index]["image"] ?? '',
                          text: splashData[index]["text"] ?? '')),
                ),
                Expanded(
                  flex: 2,
                  child: Padding(
                    padding: EdgeInsets.symmetric(
                        horizontal: getProportionateScreenWidth(20)),
                    child: Column(
                      children: <Widget>[
                        Spacer(),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: List.generate(
                            splashData.length,
                            (index) => buildDot(index: index),
                          ),
                        ),
                        Spacer(flex: 3),
                        DefaultButton(
                          text: 'Join Now',
                          press: () {
                            Navigator.pushNamed(
                                context, SignInScreen.routeName);
                          },
                        ),
                        Spacer(),
                      ],
                    ),
                  ),
                )
              ],
            )));
  }

and on the body.dart i already import all package needed

import 'package:accone/components/default_button.dart';
import 'package:accone/constants.dart';
import 'package:accone/screens/sign_in/sign_in_screen.dart';
import 'package:accone/screens/splash/components/splash_content.dart';
import 'package:accone/size_config.dart';
import 'package:flutter/material.dart';

on the button i was trying to use Navigator.pushNamed ,Navigator.pushReplacementNamed(context, '/route') and Navigator.pushAndRemoveUntil but still can't change the pages on mobile

CodePudding user response:

Please add the routes to your MaterialApp Widget, you can do it by following these codes:

return MaterialApp(
            title: 'Your App',
            debugShowCheckedModeBanner: false,
            initialRoute: SplashScreen.routeName,
            routes: {
              SplashScreen.routeName: (context) => SplashScreen(),
              SignInScreen.routeName: (context) => SignInScreen(),
            },
          );

And you can find the MaterialApp widget inside the main.dart file.

CodePudding user response:

This worked, but I changed the button code to:

 ElevatedButton(
            onPressed: () {
                 Navigator.pushNamed(
                      context, SignInScreen.routeName);
                          },
                child: const Text('Join Now'),
               ),
  • Related