check this

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

import 'package:flutter/material.dart';
import 'package:miit/models/questions/answer.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

class _CustomQuestionFieldState extends State<CustomQuestionField> {
  final Map<String, String> answers = {};
  final Map<String, String> qna = {
    "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...": "",
  };

  @override
  Widget build(
    BuildContext context,
  ) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        for (var q in qna.keys) question(q),
        RaisedButton(
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
          color: Colors.purple,
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: Text(
              "Submit",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
          ),
          onPressed: () {
            print('normal adi');
            print(answers);
            _read() async {
              try {
                final directory = await getApplicationDocumentsDirectory();
                final file = File('${directory.path}/answers.txt');
                String text = await file.readAsString();
                print(text);
              } catch (e) {
                print("Couldn't read file");
              }
            }

            _save() async {
              final directory = await getApplicationDocumentsDirectory();
              final file = File('${directory.path}/answers.txt');
              await file.writeAsString(answers.toString());
              print('saved');
            }

            _save();
            _read();
          },
        ),
      ],
    );
  }

  Widget question(String question) {
    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) {
                    //   answers[question] = ansr;
                    // },
                    controller: myController,
                    decoration: InputDecoration(
                      // filled: true,
                      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),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          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(),
            ),
          ),
        ],
      ),
    );
  }
}