I created a button and i need that when the user pressesit, to send him from the level he was to the next level, my app has 300 levels, if the user is on level 1, and he presses that button it will send him to the 2nd level. Here is the screen for all the buttons for the levels ][1], here is the image for the the level button[][2], so this button only has to send the user to the next level.
child: GridView.count(
crossAxisCount: 4,
children: List.generate(data!.length, (index) {
return InkWell(
splashColor: Colors.blue.withAlpha(20),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
RenditFjaletButton(
QuizList: data[index].word,
)));
},
child: Card(
elevation: 3.0,
margin: EdgeInsets.all(7.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
child: Container(
child: Align(
alignment: Alignment.center,
child: Container(
child: Text(
'${index 1}',
And here is the Code for the button
child: FlatButton(
onPressed: () {
null;
},
child: Image.asset(
'assets/next_small.png',
height: 80,
width: 80,
),
),
CodePudding user response:
You could use Navigator.push()
. Have a look at the Flutter Docs on Navigation to add these features to your app.
CodePudding user response:
Increase the index by one on push
RenditFjaletButton( QuizList: data[index 1].word, )))
For last index, show game over screen.
You can make QuizList
nullable by adding ?
on RenditFjaletButton
class and removing required or create another bool variable like isGameOver
RenditFjaletButton( QuizList: data[index 1].word,
isGameOver: data[index 1].word>data.length )))
Here is a test widget to increase index, based on second screen.
class _TState extends State<T> {
bool _gotoNextLevel = false;
// on new page it will set to false again
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
floatingActionButton: ElevatedButton(
child: Text("L1"),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: () {
setState(() {
_gotoNextLevel = true;
});
Navigator.of(context).pop();
},
child: Text("1")),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(" cancel")),
],
),
);
},
);
}),
body: Center(
child: Text("Current level ${1 (_gotoNextLevel ? 1 : 0)}"),
));
}
}
CodePudding user response:
You can simply use this
Navigator.push(context, new MaterialPageRoute(
builder: (context) => Page2()
));
In place of page2 you can place the name of the page where you want to go navigate.