Home > database >  How do I pass the class as a parameter in flutter?
How do I pass the class as a parameter in flutter?

Time:01-27

I am developing an Android application on flutter, where I want to pass class as a parameter. I am a page where all the food recipes are shown and the data comes from database. When any of the recipe is clicked than new screen will appear and user will see the details of that recipe Now what is want is that when normal user click the recipe the user will move to normal screen where edit/delete option is not present and when Admin click the screen than he will move to the screen where edit/delete option is present

class RecipeCard extends StatelessWidget {
  final String title;
  final String rating;
  final String cookTime;
  final String thumbnailUrl;

  const RecipeCard({
    super.key,
    required this.title,
    required this.cookTime,
    required this.rating,
    required this.thumbnailUrl,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        Navigator.push(
          context,
          PageRouteBuilder(
            transitionDuration: const Duration(seconds: 1),
            transitionsBuilder: (context, animation, animationTime, child) {
              animation = CurvedAnimation(
                  parent: animation, curve: Curves.fastLinearToSlowEaseIn);
              return ScaleTransition(
                scale: animation,
                alignment: Alignment.center,
                child: child,
              );
            },
            pageBuilder: (context, animation, animationTime) {
              return UpdateOrDeleteRecipe(
                title: title,
                cookTime: cookTime,
                rating: rating,
                thumbnailUrl: thumbnailUrl,
              );
            },
          ),
        );
      },
      child: Container(...),
    );
  }
}

Now in the above code, in return InkWell( onTap: () {I want to pass the class name so that when the card is clicked it will move to the class I want it to move.

How can I do that? Remember when I move to the next page the following argument will also move to the next page.

return UpdateOrDeleteRecipe(
  title: title,
  cookTime: cookTime,
  rating: rating,
  thumbnailUrl: thumbnailUrl,
);

CodePudding user response:

If you have two separate screens for Admin and User you can put a condition like isAdmin ? UpdateOrDeleteRecipe(...) : Detail() in pageBuilder, Or you can make a single screen for both Admin & User and put bool isAdmin argument on the screen arguments, when Admin enters that screen the isAdmin will be true and accordingly, you can show edit/delete options and is isAdmin is false don't show them.

  • Related