Invalid value: Not in inclusive range 0..2: 3

mail@pastecode.io avatar
unknown
dart
2 years ago
5.4 kB
169
Indexable
Never
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:miit/screens/profile/widgets/custom_answer.dart';
import 'package:path_provider/path_provider.dart';

class CustomQuestionField extends StatefulWidget {
  @override
  State<CustomQuestionField> createState() => _CustomQuestionFieldState();
}

class _CustomQuestionFieldState extends State<CustomQuestionField> {
  @override
  void initState() {
    super.initState();
    read();
  }

  List<Data> data = [];
  final Map<String, String> answers = {};
  final Map<String, String> qst = {
    "Everyone should read...": "",
    "Two truths and a lie...": "",
    "I can quote every line from...": "",
    "If I didn't have to work I would...": "",
    "People think I am...": "",
    "Never have I ever...": "",
    "Believe it or not, I...": "",
    "I am amazing at...": "",
    "My life as a movie...": "",
    "My ultimate dinner party guest list...": "",
    "The dorkiest thing about me is...": "",
    "On the weekend you'll find me...": "",
  };

  Future<void> _write(Map<String, dynamic> map) async {
    final directory = await getApplicationDocumentsDirectory();

    final jsonStr = jsonEncode(map);

    final file = File('${directory.path}/answers.txt');

    await file.writeAsString(jsonStr);
  }

  Future<Map<String, dynamic>> read() async {
    final directory = await getApplicationDocumentsDirectory();

    final file = File('${directory.path}/answers.txt');
    final jsonStr = await file.readAsString();
    final raw = jsonDecode(jsonStr) as Map<String, dynamic>;
    raw.forEach((key, value) {
      data.add(Data(question: key, answer: value));
    });
    print(raw);
    return raw;
  }

  @override
  Widget build(
    BuildContext context,
  ) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        // for (var q in qst.keys) question(q),
        SizedBox(
          height: 500,
          child: ListView.builder(
              itemCount: qst.keys.length,
              itemBuilder: (context, index) {
                return question(qst.keys.elementAt(index), index);
              }),
        ),
        SizedBox(
          height: 50,
        ),
      ],
    );
  }

  Widget question(String question, int index) {
    var hint = "write something";
    final myController = TextEditingController();
    return Padding(
      padding: const EdgeInsets.only(top: 15),
      child: Stack(
        children: [
          Card(
            elevation: 0,
            shape: RoundedRectangleBorder(
                side: BorderSide(width: 0.5, color: Colors.black),
                borderRadius: BorderRadius.circular(
                  32,
                )),
            child: Padding(
              padding: const EdgeInsets.all(20.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Padding(
                    padding: const EdgeInsets.only(bottom: 15, left: 10),
                    child: Text(
                      question,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 18.0,
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                  TextField(
                    onChanged: (String ansr) {
                      answers[question] = ansr;
                    },
                    onSubmitted: (String ansr) {
                      _write(answers);
                    },
                    controller: myController,
                    decoration: InputDecoration(
                      enabledBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20),
                        borderSide: BorderSide(
                          color: Color.fromARGB(255, 128, 124, 124),
                        ),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20),
                        borderSide: BorderSide(
                          color: Color.fromARGB(255, 128, 124, 124),
                        ),
                      ),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20),
                        borderSide: BorderSide(
                          color: Color.fromARGB(255, 128, 124, 124),
                        ),
                      ),
                      hintText: data[index].answer.isEmpty
                          ? "Write something"
                          : data[index].answer
                    ),
                  ),
                ],
              ),
            ),
          ),
          Positioned(
            top: -10,
            right: -25,
            child: RawMaterialButton(
              onPressed: () {
                myController.clear();
              },
              elevation: 2.0,
              fillColor: Colors.white,
              child: Text(
                "X",
                style:
                    TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
              ),
              shape: CircleBorder(),
            ),
          ),
        ],
      ),
    );
  }
}