クイズ:次の問題
CoddyのFlutterジャーニー「プロジェクト」セクションの一部。レッスン 12/36。
質問が1つだけではクイズとは言えません。回答した後、「次へ」ボタンで次の質問に進めるようにし、上部の行に現在の位置を表示しましょう。
次へ進む処理は、1つのsetState内で2つの変更を行います。インデックスを増やし、回答をリセットすることで、新しい質問は色が付いていない状態で始まります。
void _next() {
setState(() {
_index++;
_picked = null;
});
}ボタンが意味を持つのは回答後だけです。また、次の質問がない最後の質問では表示しません(結果画面は後で追加します)。コレクションのifを使って、その場合にのみ追加します。if (_answered && !_isLast) FilledButton(...)。
人は1から数えますが、リストは0から数えます。ラベルには_index + 1を表示します。'Question ${_index + 1} of ${questions.length}'。${...}形式を使えば、名前だけでなく式全体を文字列に埋め込めます。
チャレンジ
中級質問を進める:
- 最後の質問で
trueになるゲッター_isLastと、理論で説明した_nextメソッドを追加します - 質問の上に、テーマの
labelLargeスタイルでQuestion 1 of 5(実際の数値を使います)を表示し、その下に24ピクセルの余白を設けます - 回答の下に、回答済みで、かつ最後の質問ではない場合、
_nextを呼び出すNextとラベルの付いたFilledButtonを表示します
自分で試してみよう
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;
Question get _question => questions[_index];
bool get _answered => _picked != null;
void _pick(int answer) {
if (_answered) return;
setState(() {
_picked = answer;
});
}
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')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
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]),
),
),
],
),
),
);
}
}
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
プロジェクトのすべてのレッスン
自分で練習してみよう: Flutterオンラインコンパイラ