inflearn logo
강의

Course

Instructor

Create a Quiz App/Server in 1 Hour with Flutter and Django [Full Stack]

Create a quiz model and create a quiz solving screen

정답 선택지 보기에 데이터가 불러지지가않습니다..ㅠㅠ

569

20141025059502

5 asked

0

많은 질문 드려서 죄송합니다.ㅠㅠㅠ
최대한 제가 해결해보려고 노력 중인데 잘 모르겠어서 질문드립니다.

Q.질문데이터는 잘 물러와지는데,

4가지 개관식 보기 데이터들이 제대로 화면에 뜨지않습니다. 데이터가 불러와지지 않는 대표적인 이유를 알 수 있을까요.??

 

widget_candidate.dart 파일입니다.

import 'package:flutter/material.dart';

class CandWidget extends StatefulWidget {
  VoidCallback tap;
  String text;
  int index;
  double width;
  bool answerState;

  CandWidget({this.tap, this.text, this.index, this.width, this.answerState});
  _CandWidgetState createState() => _CandWidgetState();
}

class _CandWidgetState extends State<CandWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width * 0.8,
      height: widget.width * 0.1,
      padding: EdgeInsets.fromLTRB(
        widget.width * 0.848,
        widget.width * 0.024,
        widget.width * 0.848,
        widget.width * 0.024,
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20),
        border: Border.all(color: Colors.deepPurple),
        color: widget.answerState ? Colors.deepPurple : Colors.white,
      ),
      child: InkWell(
        child: Text(
          widget.text,
          style: TextStyle(
            fontSize: widget.width * 0.035,
            color: widget.answerState ? Colors.white : Colors.black,
          ),
        ),
        onTap: () {
          setState(() {
            widget.tap();
            widget.answerState = !widget.answerState;
          });
        },
      ),
    );
  }
}

screen_quiz.dart 파일입니다.

import 'package:auto_size_text/auto_size_text.dart';
import 'package:first/model/model_quiz.dart';
import 'package:first/screen/screen_result.dart';
import 'package:first/widget/widget_candidate.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class QuizScreen extends StatefulWidget {
  final List<Quiz> quizs;
  QuizScreen({this.quizs});

  @override
  _QuizScreenState createState() => _QuizScreenState();
}

class _QuizScreenState extends State<QuizScreen> {
  List<int> _answers = [-1, -1, -1];
  List<bool> _answerState = [falsefalsefalsefalse];
  int _currentindex = 0;
  SwiperController _controller = SwiperController();

  @override
  Widget build(BuildContext context) {
    Size screenSize = MediaQuery.of(context).size;
    double width = screenSize.width;
    double height = screenSize.height;

    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.deepPurple,
        body: Center(
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20),
              border: Border.all(color: Colors.deepPurple),
            ),
            width: width * 0.85,
            height: height * 0.7,
            child: Swiper(
              controller: _controller,
              physics: NeverScrollableScrollPhysics(),
              loop: false,
              itemCount: widget.quizs.length,
              itemBuilder: (BuildContext context, int index) {
                return _buildQuizCard(widget.quizs[index], width, height);
              },
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildQuizCard(Quiz quiz, double width, double height) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20),
        border: Border.all(color: Colors.white),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Container(
            padding: EdgeInsets.fromLTRB(0, width * 0.0240, width * 0.024),
            child: Text(
              'Q' + (_currentindex + 1).toString() + '.',
              style: TextStyle(
                fontSize: width * 0.06,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          Container(
            width: width * 0.8,
            padding: EdgeInsets.only(top: width * 0.012),
            child: AutoSizeText(
              quiz.title,
              textAlign: TextAlign.center,
              maxLines: 2,
              style: TextStyle(
                fontSize: width * 0.048,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          Expanded(
            child: Container(),
          ),
          Column(
            children: _buildCandidates(width, quiz),
          ),
          Container(
            padding: EdgeInsets.all(width * 0.024),
            child: Center(
              child: ButtonTheme(
                minWidth: width * 0.5,
                height: height * 0.05,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
                child: RaisedButton(
                  child: _currentindex == widget.quizs.length - 1
                      ? Text('결과보기')
                      : Text('다음문제'),
                  textColor: Colors.white,
                  color: Colors.deepPurple,
                  onPressed: _answers[_currentindex] == -1
                      ? null
                      : () {
                          if (_currentindex == widget.quizs.length - 1) {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => ResultScreen(
                                          answers: _answers,
                                          quizs: widget.quizs,
                                        )));
                          } else {
                            _answerState = [falsefalsefalsefalse];
                            _currentindex += 1;
                            _controller.next();
                          }
                        },
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  List<Widget_buildCandidates(double width, Quiz quiz) {
    List<Widget> _children = [];
    for (int i = 0; i < 4; i++) {
      _children.add(
        CandWidget(
          index: i,
          text: quiz.candidates[i],
          width: width,
          answerState: _answerState[i],
          tap: () {
            setState(() {
              for (int j = 0; j < 4; j++) {
                if (j == i) {
                  _answerState[j] = true;
                  _answers[_currentindex] = j;
                } else {
                  _answerState[j] = false;
                }
              }
            });
          },
        ),
      );
      _children.add(
        Padding(
          padding: EdgeInsets.all(width * 0.024),
        ),
      );
    }
    return _children;
  }
}

django Flutter

Answer 2

0

dkyou78425

저도 같은 문제로 고민이 많았습니다.

정답은

widget.width * 0.048,
widget.width * 0.024,
widget.width * 0.048,
widget.width * 0.024,

이부분인데요. 코드 올리신 것 보면 0.848로 되어있습니다. 이부분을 수정하면 될 것 같아요!

0

taebbong

안녕하세요! 해당 문제는 백엔드 데이터를 못 가져온다고 하기엔 질문을 잘 가져오고 있어서 아마 처리하는 과정에서 문제가 있었을 것 같습니다. 

우선 선택지가 담기는 변수를 출력해서 데이터가 잘 들어오는지 확인해보고, 만약 그렇다면 글씨가 흰 색이라 안보이는 건지 아니면 데이터가 변수로 넘어오지 않는건지 등 체크해볼 수 있을 것 같습니다.

emulator 실행오류

0

116

0

24년 8월 기준 flutter_swiper import 관련

4

196

2

heroku 유료 결제 관련

0

324

1

vscode 포매팅 하는 방법이 궁금합니다

0

240

1

heroku 푸시 에러

0

263

1

heroku run python manage.py createsuperuser / auth_user 오류

0

350

2

사진 파일

0

630

2

혹시 강의 따라하시려는 분들

6

729

1

github에서 파일을 가지고 와서 복사붙여넣기 했더니 자꾸 오류가 뜹니다.

0

752

0

python: can't open file 'manage.py': [Errno 2] No such file or directory

0

1825

1

*윈도우에서 개발하시는 분들 가상환경 만드실 때

2

1403

0

오른쪽 휴대폰 화면

0

567

0

Quiz.fromMap 작성이유가 궁금합니다.

0

395

0

버튼 부분을 잘 모르겠습니다.

0

328

0

1강 구현 중 질문이 있습니다

1

783

1

반응형 UI인데 오버플로우가 발생하네요.

3

348

0

A RenderFlex overflowed by 41 pixels on the bottom

0

475

1

fetch quizs에서 데이터가 불러와지지 않습니다

0

278

0

헤로쿠 H10 에러 발생하시는분 없나요??

0

498

2

vscode에서 자동으로 코드 정렬

0

1042

1

이부분이 안됩니다 ㅠㅠ

0

515

2

혹시 이건 어떻게 풀어야 할까요?

0

314

1

에러 문의 드립니다

0

556

3

http.get 타입 문제

0

326

2