I'm learning to program mobile apps in flutter following this video tutorial " https://www.youtube.com/watch?v=DAroZ2-OAaY&t=472s&ab_channel=Dero " of a flutter application, and within the modifications I made to the video code I managed to make the containers where the carousels are work as buttons, however, the way I did it works badly since I cannot assign a new screen to each button but they all redirect to the same screen.
So I wonder if it is possible with the way I have written the code to be able to assign each button a different screen or is it necessary to structure the code in another way.
Below I leave the code extracts that I modified from the video where I added the gesture detector
import 'package:hielder2/screens/avances.dart';
import 'package:hielder2/screens/mainscreen.dart';
import 'package:hielder2/models/tool.dart';
import 'package:flutter/material.dart';
import 'package:hielder2/screens/mainscreen.dart';
class CompactItemJob extends StatelessWidget {
Tool tool;
CompactItemJob(this.tool);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainScreen())
);
},
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 7.0),
child: Container(
decoration: _boxDecoration(),
height: 125.0,
width: 100.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
_companyLogo(),
_infoJobTexts(context),
],
),
],
),
),
),
);
}
Some tips:
Surely no one will watch a video longer than 30 minutes. Probably not a minute, so if you want to link to the video, Ok, but it's probably unnecessary.
In addition, I would recommend you to put the part related to the incidence. In this case, you could put the widget that acts as items for the slider, which I suppose is
CompactItemJob
, and the slider widget itself. The imports are not really necessary. In addition, indicating what each thing is will help a lot to better understand the problem.With that said, let's get down to business.
There are several ways to do what you ask, but in the end what you need is to have a variable argument that indicates how or where to navigate.
You can achieve this by passing it as a parameter to the widget; just like you do with the
tool
.In Flutter there is the
typedef VoidCallback
whose signature is:typedef VoidCallback = void Function();
. You can use this type as an attribute within your widget or create your own. Here is a link if you want to learn more: ExploreTypeDef .So your widget could look like:
Finally, where you instantiate the widget you will have to pass it a function to navigate. Each widget will need to be passed a different function, so you can change the screen each widget navigates to. It would be something like this:
Remember to mark my answer as the correct one, if it has helped you and you think there is no better one; that way, other users can benefit from your question.