Home > Software engineering >  How do I handle screen parameters in themedata's routes?
How do I handle screen parameters in themedata's routes?

Time:07-18

I want to use ThemeData and set routes at

routes: {
                '/': (context) => const LoginScreen(),
                '/sign-up-screen': (context) => const SignUpScreen(),
                '/sign-in-screen': (context) => const SignInScreen(),
                '/map-screen': (context) => MapScreen()}

But I also have a screen with parameters, like this.

import 'dart:io';
import 'package:dash_mement/poststory/check_image.dart';
import 'package:dash_mement/providers/pushstory_provider.dart';
import 'package:dash_mement/style/mmnt_style.dart';
import 'package:dash_mement/style/story_textstyle.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';

class PostImage extends StatelessWidget {
   Widget _inform = Image.asset("assets/images/check_image.png");
   final ImagePicker _picker = ImagePicker();
   late Function _backButton;
   double? lat_y;
   double? lng_x;

   PostImage(this._backButton) {}

   PostImage.newPin({required double latitude_y, required double longitude_x}) {
      lat_y = latitude_y;
      lng_x = longitude_x;
   }
....
}

How can I add this screen at ThemeData's routes of main.dart

CodePudding user response:

Add it like any other route and access arguments in a widget something like this:

class PostImage extends StatelessWidget {
...
  @override
  Widget build(BuildContext context) {
    final args = ModalRoute.of(context)!.settings.arguments;

Or alternatively use onGenerateRoute to handle routes and extract arguments and pass to a widget.

See Pass arguments to a named route for details.

  • Related