Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- void main() {
- final Map<String, Map<String, bool>> questions = {
- "milk comes from what animal": {
- "horse": false,
- "monkey": false,
- "frog": false,
- "cow": true
- },
- "what colour is the sea?": {
- "red": false,
- "green": false,
- "blue": true,
- "yellow": false
- }
- };
- runApp(
- MaterialApp(title: 'Cats', home: QuestionScreen(questions: questions)));
- }
- class QuestionScreen extends StatefulWidget {
- final Map<String, Map<String, bool>> questions;
- const QuestionScreen({Key? key, required this.questions}) : super(key: key);
- @override
- _QuestionScreenState createState() => _QuestionScreenState();
- }
- class _QuestionScreenState extends State<QuestionScreen> {
- Map<String, String> _isChecked = {};
- @override
- void initState() {
- super.initState();
- widget.questions.keys.forEach((key) {
- _isChecked[key] = "";
- for (MapEntry entry in widget.questions[key]!.entries) {
- if (entry.value) _isChecked[key] = entry.key;
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(),
- body: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Container(
- child: Text("questions page"),
- ),
- Expanded(
- child: new ListView.builder(
- itemCount: widget.questions.keys.length,
- itemBuilder: (BuildContext ctxt, int questionTitleIndex) {
- return Padding(
- padding: const EdgeInsets.all(24.0),
- child: Container(
- height: MediaQuery.of(context).size.height * 0.45,
- decoration: BoxDecoration(
- color: Colors.deepPurple,
- borderRadius: BorderRadius.circular(25),
- ),
- child: Column(
- children: [
- Text(
- widget.questions.keys.elementAt(questionTitleIndex),
- style: TextStyle(
- color: Colors.white,
- fontSize: 24,
- fontWeight: FontWeight.w800),
- ),
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: ListView.builder(
- shrinkWrap: true,
- itemCount: widget.questions.values
- .elementAt(questionTitleIndex)
- .keys
- .length,
- itemBuilder:
- (BuildContext ctxt, int questionAnswersIndex) {
- return Container(
- decoration: BoxDecoration(border: Border.all()),
- child: CheckboxListTile(
- title: Text(
- "${questionAnswersIndex + 1}. ${widget.questions.values.elementAt(questionTitleIndex).keys.elementAt(questionAnswersIndex)}",
- style: TextStyle(
- color: Colors.white, fontSize: 16),
- ),
- value: _isChecked[widget.questions.keys
- .elementAt(questionTitleIndex)] ==
- widget.questions.values
- .elementAt(questionTitleIndex)
- .keys
- .elementAt(questionAnswersIndex),
- controlAffinity:
- ListTileControlAffinity.platform,
- onChanged: (bool? value) {
- setState(
- () {
- _isChecked[widget.questions.keys
- .elementAt(
- questionTitleIndex)] = widget
- .questions.values
- .elementAt(questionTitleIndex)
- .keys
- .elementAt(questionAnswersIndex);
- },
- );
- },
- activeColor: Colors.deepPurple,
- checkColor: Colors.white,
- ),
- );
- },
- ),
- )
- ],
- ),
- ),
- );
- },
- ),
- )
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement