Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'dart:async' show Future;
- import 'package:flutter/services.dart' show rootBundle;
- import 'dart:convert';
- int qN = 0;
- bool qAnwered = true;
- int radioGroup;
- String button1 = 'Next Question';
- class EnCitTest extends StatefulWidget {
- @override
- State<StatefulWidget> createState() => _EnCitTestState();
- }
- class _EnCitTestState extends State<EnCitTest> {
- List data;
- @override
- void initState() {
- super.initState();
- }
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- backgroundColor: Colors.blueGrey,
- title: Text('Citizenship Test'),
- ),
- body: Column(
- children: <Widget>[
- Text(''),
- Align(
- alignment: Alignment.topCenter,
- child: Text('To start a new test select start.'),
- ),
- Text(''),
- Text(''),
- Text(''),
- Text(''),
- Text(''),
- Text(''),
- Center(
- child: RaisedButton(
- child: Text('Start', style: TextStyle(fontSize: 20)),
- onPressed: openTestViewerDialog,
- ),
- )
- ],
- ));
- }
- Future openTestViewerDialog() {
- Navigator.of(context).push(MaterialPageRoute<Null>(
- builder: (BuildContext context) {
- qN = 0;
- return TestViewerDialog();
- },
- fullscreenDialog: true));
- }
- }
- void nextQuestion(BuildContext context) {
- if (button1 == 'Next Question') {
- if (qAnwered == true) {
- radioGroup = 0;
- qN++;
- qAnwered = false;
- print(qN);
- if (qN == 2) {
- button1 = 'Finish Test';
- }
- }
- } else if (button1 == 'Finish Test')
- Navigator.of(context).pushNamed('/EnFinishScreen');
- }
- }
- class TestViewerDialog extends StatefulWidget {
- @override
- TestViewerDialogState createState() => TestViewerDialogState();
- }
- class TestViewerDialogState extends State<TestViewerDialog> {
- @override
- Widget build(BuildContext context) {
- return FutureBuilder(
- future: DefaultAssetBundle.of(context)
- .loadString('assets/data/en_citizen_test.json'),
- builder: (context, snapshot) {
- //Decode Json
- var mydata = json.decode(snapshot.data.toString());
- return Scaffold(
- appBar: AppBar(
- backgroundColor: Colors.blueGrey,
- title: Text(
- 'Question ' + mydata[qN]['id'],
- ),
- centerTitle: true),
- body: ListView.builder(
- itemBuilder: (BuildContext context, int index) {
- return Column(
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- Text(
- '',
- style: TextStyle(fontSize: 40),
- ),
- Text(mydata[qN]['question'] + "?",
- style: TextStyle(fontSize: 20)),
- Divider(),
- RadioListTile(
- value: 1,
- groupValue: radioGroup,
- onChanged: (e) => radioEventHandler(e),
- title: Text(mydata[qN]['A']),
- activeColor: Colors.black,
- ),
- RadioListTile(
- value: 2,
- groupValue: radioGroup,
- onChanged: (e) => radioEventHandler(e),
- title: Text(mydata[qN]['B']),
- activeColor: Colors.black,
- ),
- RadioListTile(
- value: 3,
- groupValue: radioGroup,
- onChanged: (e) => radioEventHandler(e),
- title: Text(mydata[qN]['C']),
- activeColor: Colors.black,
- ),
- RadioListTile(
- value: 4,
- groupValue: radioGroup,
- onChanged: (e) => radioEventHandler(e),
- title: Text(mydata[qN]['D']),
- activeColor: Colors.black,
- ),
- Align(
- alignment: Alignment.bottomCenter,
- child: RaisedButton(
- child: Text(button1),
- color: Colors.orangeAccent,
- onPressed: () => {
- setState(() {
- nextQuestion(context);
- })
- },
- ),
- )
- ]);
- },
- itemCount: mydata == null ? 0 : 1));
- });
- }
- void radioEventHandler(e) {
- setState(() {
- if (e == 1) {
- qAnwered = true;
- radioGroup = 1;
- } else if (e == 2) {
- qAnwered = true;
- radioGroup = 2;
- } else if (e == 3) {
- qAnwered = true;
- radioGroup = 3;
- } else if (e == 4) {
- qAnwered = true;
- radioGroup = 4;
- }
- });
- }
- }
- int qA1;
- int qA2;
- int qA3;
- int qA4;
- int qA5;
- int qA6;
- int qA7;
- int qA8;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement