Advertisement
Nisanth-Reddy

Questions

May 20th, 2021
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() {
  4. final Map<String, Map<String, bool>> questions = {
  5. "milk comes from what animal": {
  6. "horse": false,
  7. "monkey": false,
  8. "frog": false,
  9. "cow": true
  10. },
  11. "what colour is the sea?": {
  12. "red": false,
  13. "green": false,
  14. "blue": true,
  15. "yellow": false
  16. }
  17. };
  18.  
  19. runApp(
  20. MaterialApp(title: 'Cats', home: QuestionScreen(questions: questions)));
  21. }
  22.  
  23. class QuestionScreen extends StatefulWidget {
  24. final Map<String, Map<String, bool>> questions;
  25.  
  26. const QuestionScreen({Key? key, required this.questions}) : super(key: key);
  27.  
  28. @override
  29. _QuestionScreenState createState() => _QuestionScreenState();
  30. }
  31.  
  32. class _QuestionScreenState extends State<QuestionScreen> {
  33. Map<String, String> _isChecked = {};
  34.  
  35. @override
  36. void initState() {
  37. super.initState();
  38. widget.questions.keys.forEach((key) {
  39. _isChecked[key] = "";
  40. for (MapEntry entry in widget.questions[key]!.entries) {
  41. if (entry.value) _isChecked[key] = entry.key;
  42. }
  43. });
  44. }
  45.  
  46. @override
  47. Widget build(BuildContext context) {
  48. return Scaffold(
  49. appBar: AppBar(),
  50. body: Column(
  51. mainAxisAlignment: MainAxisAlignment.center,
  52. children: [
  53. Container(
  54. child: Text("questions page"),
  55. ),
  56. Expanded(
  57. child: new ListView.builder(
  58. itemCount: widget.questions.keys.length,
  59. itemBuilder: (BuildContext ctxt, int questionTitleIndex) {
  60. return Padding(
  61. padding: const EdgeInsets.all(24.0),
  62. child: Container(
  63. height: MediaQuery.of(context).size.height * 0.45,
  64. decoration: BoxDecoration(
  65. color: Colors.deepPurple,
  66. borderRadius: BorderRadius.circular(25),
  67. ),
  68. child: Column(
  69. children: [
  70. Text(
  71. widget.questions.keys.elementAt(questionTitleIndex),
  72. style: TextStyle(
  73. color: Colors.white,
  74. fontSize: 24,
  75. fontWeight: FontWeight.w800),
  76. ),
  77. Padding(
  78. padding: const EdgeInsets.all(8.0),
  79. child: ListView.builder(
  80. shrinkWrap: true,
  81. itemCount: widget.questions.values
  82. .elementAt(questionTitleIndex)
  83. .keys
  84. .length,
  85. itemBuilder:
  86. (BuildContext ctxt, int questionAnswersIndex) {
  87. return Container(
  88. decoration: BoxDecoration(border: Border.all()),
  89. child: CheckboxListTile(
  90. title: Text(
  91. "${questionAnswersIndex + 1}. ${widget.questions.values.elementAt(questionTitleIndex).keys.elementAt(questionAnswersIndex)}",
  92. style: TextStyle(
  93. color: Colors.white, fontSize: 16),
  94. ),
  95. value: _isChecked[widget.questions.keys
  96. .elementAt(questionTitleIndex)] ==
  97. widget.questions.values
  98. .elementAt(questionTitleIndex)
  99. .keys
  100. .elementAt(questionAnswersIndex),
  101. controlAffinity:
  102. ListTileControlAffinity.platform,
  103. onChanged: (bool? value) {
  104. setState(
  105. () {
  106. _isChecked[widget.questions.keys
  107. .elementAt(
  108. questionTitleIndex)] = widget
  109. .questions.values
  110. .elementAt(questionTitleIndex)
  111. .keys
  112. .elementAt(questionAnswersIndex);
  113. },
  114. );
  115. },
  116. activeColor: Colors.deepPurple,
  117. checkColor: Colors.white,
  118. ),
  119. );
  120. },
  121. ),
  122. )
  123. ],
  124. ),
  125. ),
  126. );
  127. },
  128. ),
  129. )
  130. ],
  131. ),
  132. );
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement