Home > Enterprise >  How to pass a screen as a parameter to another screen in flutter?
How to pass a screen as a parameter to another screen in flutter?

Time:05-27

this is my first time making a full flutter app and i am stuck. I have currently passed a screen as a widget to another class as i have a dashboard screen where multiples buttons within it go to the same screen (course_screen.dart) and then from their move away to different screens depending on what option they had chosen in the dashboard screen. however in the course_screen.dart file depending on the course chosen the number and all available quizzes of that course will be shown on the UI. How do i achieve this flutter?

course_screen.dart

   import 'package:lms_app/Screens/student_homescreen.dart';
   import 'package:lms_app/providers/courses_provider.dart';
   import 'package:provider/provider.dart';

   class CoursesScreen extends StatelessWidget {
      Widget nextScreen;
      CoursesScreen({required this.nextScreen, super.key});

      @override
      Widget build(BuildContext context) {
        final course_listner = Provider.of<CoursesProvider>(context).subjects;
        return Scaffold(
          body: Stack(
            children: [
              Container(
                color: Colors.grey[200],
                child: Align(
                  alignment: Alignment.bottomCenter,
                  child: Container(
                    width: double.infinity,
                    height: 700,
                    // color: Colors.red,
                    child: ListView.builder(
                        itemCount: course_listner.length,
                        itemBuilder: (context, index) =>
                            Container(
                                padding: EdgeInsets.symmetric(horizontal: 12),
                                width: 300,
                                height: 100,
                                margin: EdgeInsets.all(5),
                                child: GestureDetector(
                                  onTap: () => Navigator.of(context).push(
                                      MaterialPageRoute(
                                          builder: (context) => nextScreen, )),
                                  child: Card(
                                    color: Colors.deepPurple[400],
                                    elevation: 5,
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(20),
                                    ),
                                    child: Padding(
                                     padding: const EdgeInsets.symmetric(horizontal: 15),
                                      child: ListTile(
                                        title: Padding(
                                          padding: EdgeInsets.symmetric(vertical: 10),
                                          child: Text(
                                            course_listner[index].subjectName,
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 25,
                                                color: Colors.white),
                                         ),
                                        ),
                                        subtitle: Text(
                                          course_listner[index].teacherName,
                                          style: TextStyle(color: Colors.white),
                                        ),
                                      ),
                                    ),
                                   ),
                                ),
                              ),
                            ),
                  ),
                ),
              ),
              Container(
                height: 120,
                width: double.infinity,
                // color: Colors.red,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(25),
                        bottomRight: Radius.circular(25)),
                    image: DecorationImage(
                       image: AssetImage('assets/background_img.png'),
                       fit: BoxFit.cover)),
                 child: Column(children: [
                  SizedBox(
                    height: 15,
                  ),
                 Padding(
                    padding: const EdgeInsets.symmetric(vertical: 15),
                    child: Row(
                     children: [
                        IconButton(
                            onPressed: () {
                              Navigator.of(context).push(MaterialPageRoute(
                                  builder: (context) => StudentHomeScreen()));
                            },
                            icon: Icon(
                              Icons.arrow_back_ios_new,
                              color: Colors.white,
                            )),
                        SizedBox(
                          width: 90,
                        ),
                        Text(
                          'Courses',
                           style: TextStyle(color: Colors.white, fontSize: 30),
                         )
                      ],
                    ),
                  ),
                ]),
              )
            ],
          ),
        );
      }
    }```

student_quiz_screen.dart

 ```import 'package:flutter/material.dart';
    import 'package:lms_app/models/courses_model.dart';
    import 'package:provider/provider.dart';

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

      @override
       Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            children: [
              Container(
               color: Colors.grey[200],
               // child: Center(child: Text(listener.subjectName)),
             ),
              Container(
                width: double.infinity,
                height: 200,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(25),
                        bottomRight: Radius.circular(25)),
                    image: DecorationImage(
                        image: AssetImage('assets/background_img.png'),
                        fit: BoxFit.cover)),
                child: Padding(
                  padding: const EdgeInsets.symmetric(vertical: 20),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          IconButton(
                            onPressed: (() {}),
                            icon: Icon(Icons.arrow_back_ios_new),
                            color: Colors.white,
                          )
                        ],
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        );
     }
   }```


[enter image description here][1]


[enter image description here][2]

[enter image description here][3]


  [1]: https://i.stack.imgur.com/JVRxF.jpg
  [2]: https://i.stack.imgur.com/wu7DA.jpg
  [3]: https://i.stack.imgur.com/jGMtC.jpg

CodePudding user response:

Here are a few options.

  1. Use state management packages (ex. bloc, provider, riverpod, getx, ...etc)

  2. Passing additional parameters to the constructor

class StudentQuizPage extends StatelessWidget {
      const StudentQuizPage({super.key, int somedata, String somedata2});

CodePudding user response:

 *onPressed: () { nextScreenArgs args =
                                nextScreenArgs(
                                    dashboardCubit
                                        .selectedcoursevalue!
                                        .toString(),
                                    name: dashboardCubit.name); 
                                dashboardCubit.nextscreen();
                              }),*

-------------------------------------------------------------
 *int? dashboard;
String? name;

dashboardCubit(this.authenticationCubit, nextScreenArgs args)
      : super(dashboardScreenInitialState()) {
    dashboard= args.value as int;
    name= args.name;
}*
----------------------------------------------------------------
 *static Widget builddashboard(RouteSettings settings) {
    final NextscreenArgs args = settings.arguments! as NextscreenArgs ;
    return CProvider(
        create: (context) {
          final AuthenticationCubit authenticationCubit =
              BlocProvider.of<AuthenticationCubit>(context);
          return dashboardCubit(authenticationCubit, args)..init();
        },
        builder: (context) => DashboardScreen());
  }*

*class nextScreenArgs {
 int? selectedcoursevalue;
  final String? routeName;
   
  nextScreenArgs (this.selectedcoursevalue, {this.routeName});
}*
  • Related