Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.66 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:async' show Future;
  3. import 'package:flutter/services.dart' show rootBundle;
  4. import 'dart:convert';
  5.  
  6. int qN = 0;
  7. bool qAnwered = true;
  8. int radioGroup;
  9. String button1 = 'Next Question';
  10.  
  11. class EnCitTest extends StatefulWidget {
  12.   @override
  13.   State<StatefulWidget> createState() => _EnCitTestState();
  14. }
  15.  
  16. class _EnCitTestState extends State<EnCitTest> {
  17.   List data;
  18.   @override
  19.   void initState() {
  20.     super.initState();
  21.   }
  22.  
  23.   Widget build(BuildContext context) {
  24.     return Scaffold(
  25.         appBar: AppBar(
  26.           backgroundColor: Colors.blueGrey,
  27.           title: Text('Citizenship Test'),
  28.         ),
  29.         body: Column(
  30.           children: <Widget>[
  31.             Text(''),
  32.             Align(
  33.               alignment: Alignment.topCenter,
  34.               child: Text('To start a new test select start.'),
  35.             ),
  36.             Text(''),
  37.             Text(''),
  38.             Text(''),
  39.             Text(''),
  40.             Text(''),
  41.             Text(''),
  42.             Center(
  43.               child: RaisedButton(
  44.                 child: Text('Start', style: TextStyle(fontSize: 20)),
  45.                 onPressed: openTestViewerDialog,
  46.               ),
  47.             )
  48.           ],
  49.         ));
  50.   }
  51.  
  52.   Future openTestViewerDialog() {
  53.     Navigator.of(context).push(MaterialPageRoute<Null>(
  54.         builder: (BuildContext context) {
  55.           qN = 0;
  56.           return TestViewerDialog();
  57.         },
  58.         fullscreenDialog: true));
  59.   }
  60. }
  61.  
  62. void nextQuestion(BuildContext context) {
  63.   if (button1 == 'Next Question') {
  64.     if (qAnwered == true) {
  65.       radioGroup = 0;
  66.       qN++;
  67.       qAnwered = false;
  68.       print(qN);
  69.       if (qN == 2) {
  70.         button1 = 'Finish Test';
  71.       }
  72.     }
  73.   } else if (button1 == 'Finish Test')
  74.     Navigator.of(context).pushNamed('/EnFinishScreen');
  75.   }
  76. }
  77.  
  78. class TestViewerDialog extends StatefulWidget {
  79.   @override
  80.   TestViewerDialogState createState() => TestViewerDialogState();
  81. }
  82.  
  83. class TestViewerDialogState extends State<TestViewerDialog> {
  84.   @override
  85.   Widget build(BuildContext context) {
  86.     return FutureBuilder(
  87.         future: DefaultAssetBundle.of(context)
  88.             .loadString('assets/data/en_citizen_test.json'),
  89.         builder: (context, snapshot) {
  90.           //Decode Json
  91.           var mydata = json.decode(snapshot.data.toString());
  92.  
  93.           return Scaffold(
  94.               appBar: AppBar(
  95.                   backgroundColor: Colors.blueGrey,
  96.                   title: Text(
  97.                     'Question ' + mydata[qN]['id'],
  98.                   ),
  99.                   centerTitle: true),
  100.               body: ListView.builder(
  101.                   itemBuilder: (BuildContext context, int index) {
  102.                     return Column(
  103.                         mainAxisSize: MainAxisSize.max,
  104.                         children: <Widget>[
  105.                           Text(
  106.                             '',
  107.                             style: TextStyle(fontSize: 40),
  108.                           ),
  109.                           Text(mydata[qN]['question'] + "?",
  110.                               style: TextStyle(fontSize: 20)),
  111.                           Divider(),
  112.                           RadioListTile(
  113.                             value: 1,
  114.                             groupValue: radioGroup,
  115.                             onChanged: (e) => radioEventHandler(e),
  116.                             title: Text(mydata[qN]['A']),
  117.                             activeColor: Colors.black,
  118.                           ),
  119.                           RadioListTile(
  120.                             value: 2,
  121.                             groupValue: radioGroup,
  122.                             onChanged: (e) => radioEventHandler(e),
  123.                             title: Text(mydata[qN]['B']),
  124.                             activeColor: Colors.black,
  125.                           ),
  126.                           RadioListTile(
  127.                             value: 3,
  128.                             groupValue: radioGroup,
  129.                             onChanged: (e) => radioEventHandler(e),
  130.                             title: Text(mydata[qN]['C']),
  131.                             activeColor: Colors.black,
  132.                           ),
  133.                           RadioListTile(
  134.                             value: 4,
  135.                             groupValue: radioGroup,
  136.                             onChanged: (e) => radioEventHandler(e),
  137.                             title: Text(mydata[qN]['D']),
  138.                             activeColor: Colors.black,
  139.                           ),
  140.                           Align(
  141.                             alignment: Alignment.bottomCenter,
  142.                             child: RaisedButton(
  143.                               child: Text(button1),
  144.                               color: Colors.orangeAccent,
  145.                               onPressed: () => {
  146.                                     setState(() {
  147.                                       nextQuestion(context);
  148.                                     })
  149.                                   },
  150.                             ),
  151.                           )
  152.                         ]);
  153.                   },
  154.                   itemCount: mydata == null ? 0 : 1));
  155.         });
  156.   }
  157.  
  158.   void radioEventHandler(e) {
  159.     setState(() {
  160.       if (e == 1) {
  161.         qAnwered = true;
  162.         radioGroup = 1;
  163.       } else if (e == 2) {
  164.         qAnwered = true;
  165.         radioGroup = 2;
  166.       } else if (e == 3) {
  167.         qAnwered = true;
  168.         radioGroup = 3;
  169.       } else if (e == 4) {
  170.         qAnwered = true;
  171.         radioGroup = 4;
  172.       }
  173.     });
  174.   }
  175. }
  176.  
  177. int qA1;
  178. int qA2;
  179. int qA3;
  180. int qA4;
  181. int qA5;
  182. int qA6;
  183. int qA7;
  184. int qA8;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement