I highly recommend you to use PageView in this case, Just create new file and paste my code to see how it's work.
The code do what you want exactly, but off course doesn't implements any styling or your app mechanism. I just used buttons for simplicity. Implement it with your desired way:
import 'package:flutter/material.dart';
class TestPage extends StatelessWidget {
TestPage({Key? key}) : super(key: key);
final PageController controller = PageController();
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: PageView(
controller: controller,
children: List.generate(
6, (index) => QuestionPage(questionNumber: index)),
),
),
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: () {
controller.previousPage(
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut);
},
child: const Text('Back'),
),
),
const SizedBox(width: 20),
Expanded(
child: ElevatedButton(
onPressed: () {
controller.nextPage(
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut);
},
child: const Text('Next'),
),
),
],
)
],
);
}
}
class QuestionPage extends StatelessWidget {
const QuestionPage({Key? key, required this.questionNumber})
: super(key: key);
final int questionNumber;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Question $questionNumber',
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {},
child: const Text('Answer 1'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {},
child: const Text('Answer 2'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {},
child: const Text('Answer 3'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {},
child: const Text('Answer 4'),
),
],
),
);
}
}
