퀴즈: 피드백
Coddy Flutter 여정의 프로젝트 섹션에 포함된 레슨. 36개 중 14번째.
색상은 정답인지 오답인지 알려 줍니다. 짧은 문장으로 알려 주면 더 명확하고, 오답일 때는 정답을 표시할 수도 있습니다. 예: "정답입니다!" 또는 "정답은 Column입니다".
답을 제출한 다음, 다음 버튼과 함께 표시하세요. 컬렉션 if는 스프레드 ...[ ]를 사용해 여러 위젯을 한 번에 추가할 수 있습니다. 조건이 충족될 때만 해당 목록의 모든 항목이 표시됩니다.
if (_answered) ...[
Text(_picked == question.correct ? 'Correct!' : 'The answer is ...'),
if (!_isLast) FilledButton(...),
],정답의 텍스트는 question.answers[question.correct]입니다. 답 목록에서 정답이 있는 위치의 항목입니다. 여기에 _picked를 사용하면 대신 탭한 답을 표시하게 됩니다.
챌린지
중급결과를 알려 주세요:
- 답변을 선택하면 답변 아래에 12픽셀의 간격과 문구를 표시하세요. 정답이면
Correct!를, 오답이면The answer is와 정답을 표시하세요 - 가운데 정렬하고 테마의
titleMedium을 사용하세요 - 그다음 16픽셀의 간격을 두고, 질문이 마지막 질문이 아닐 때만 Next 버튼을 표시하세요
직접 해보기
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const QuizScreen(),
);
}
}
class Question {
const Question(this.text, this.answers, this.correct);
final String text;
final List<String> answers;
final int correct;
}
const List<Question> questions = [
Question('Which widget stacks its children from top to bottom?', ['Row', 'Column', 'Stack', 'Wrap'], 1),
Question('Which language do you write Flutter apps in?', ['Kotlin', 'Swift', 'Dart', 'JavaScript'], 2),
Question('What does setState do?', ['Rebuilds the widget', 'Saves the app', 'Closes the screen', 'Plays a sound'], 0),
Question('Which widget shows a list you can scroll?', ['Container', 'SizedBox', 'Padding', 'ListView'], 3),
Question('Which widget adds space around its child?', ['Padding', 'Center', 'Icon', 'Text'], 0),
];
class QuizScreen extends StatefulWidget {
const QuizScreen({super.key});
@override
State<QuizScreen> createState() => _QuizScreenState();
}
class _QuizScreenState extends State<QuizScreen> {
int _index = 0;
int? _picked;
int _score = 0;
Question get _question => questions[_index];
bool get _answered => _picked != null;
bool get _isLast => _index == questions.length - 1;
void _pick(int answer) {
if (_answered) return;
setState(() {
_picked = answer;
if (answer == _question.correct) {
_score++;
}
});
}
void _next() {
setState(() {
_index++;
_picked = null;
});
}
Color? _colorFor(int answer) {
if (!_answered) return null;
if (answer == _question.correct) return Colors.green;
if (answer == _picked) return Colors.red;
return null;
}
@override
Widget build(BuildContext context) {
final question = _question;
final textTheme = Theme.of(context).textTheme;
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Quiz'),
actions: [
Padding(
padding: const EdgeInsets.only(right: 16),
child: Center(
child: Text('Score: $_score', style: textTheme.titleMedium),
),
),
],
),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Question ${_index + 1} of ${questions.length}',
style: textTheme.labelLarge,
),
const SizedBox(height: 24),
Text(question.text, style: textTheme.headlineSmall),
const SizedBox(height: 24),
for (int i = 0; i < question.answers.length; i++)
Padding(
padding: const EdgeInsets.only(bottom: 12),
child: FilledButton.tonal(
style: FilledButton.styleFrom(
backgroundColor: _colorFor(i),
foregroundColor: _colorFor(i) == null ? null : Colors.white,
padding: const EdgeInsets.all(16),
),
onPressed: () => _pick(i),
child: Text(question.answers[i]),
),
),
if (_answered && !_isLast)
FilledButton(
onPressed: _next,
child: const Text('Next'),
),
],
),
),
);
}
}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
프로젝트의 모든 레슨
직접 연습해 보세요: 온라인 Flutter 컴파일러