quiz_bank 1.0.0
quiz_bank: ^1.0.0 copied to clipboard
A comprehensive Flutter package providing true/false quiz questions with categories, difficulty levels, and easy customization. Perfect for educational apps, trivia games, and learning platforms.
import 'package:flutter/material.dart';
import 'package:quiz_bank/quiz_bank.dart';
void main() {
runApp(const QuizApp());
}
class QuizApp extends StatelessWidget {
const QuizApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Simple Quiz App',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.light,
),
appBarTheme: const AppBarTheme(
centerTitle: true,
elevation: 0,
backgroundColor: Colors.transparent,
foregroundColor: Colors.black87,
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
cardTheme: CardThemeData(
elevation: 4,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
),
),
home: const QuizScreen(),
);
}
}
class QuizScreen extends StatefulWidget {
const QuizScreen({super.key});
@override
State<QuizScreen> createState() => _QuizScreenState();
}
class _QuizScreenState extends State<QuizScreen> {
late QuizBrain quiz;
int score = 0;
bool? userAnswer;
bool showResult = false;
@override
void initState() {
super.initState();
quiz = QuizBrain();
}
void _answerQuestion(bool answer) {
if (showResult) return;
setState(() {
userAnswer = answer;
if (quiz.checkAnswer(answer)) {
score++;
}
showResult = true;
});
}
void _nextQuestion() {
if (quiz.hasNextQuestion) {
setState(() {
quiz.nextQuestion();
userAnswer = null;
showResult = false;
});
} else {
_showScore();
}
}
void _showScore() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Quiz Complete!'),
content: Text('Your Score: $score / ${quiz.totalQuestions}'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
_resetQuiz();
},
child: const Text('Try Again'),
),
],
),
);
}
void _resetQuiz() {
setState(() {
quiz.reset();
score = 0;
userAnswer = null;
showResult = false;
});
}
@override
Widget build(BuildContext context) {
final progress = quiz.currentQuestionNumber / quiz.totalQuestions;
return Scaffold(
backgroundColor: Colors.grey.shade50,
appBar: AppBar(
title: const Text(
'Quiz Master',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 24,
),
),
backgroundColor: Colors.transparent,
elevation: 0,
),
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.blue.shade50,
Colors.white,
],
),
),
child: SafeArea(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: MediaQuery.of(context).size.height -
MediaQuery.of(context).padding.top -
kToolbarHeight - 48, // Account for padding
),
child: Column(
children: [
// Progress Section
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 10,
offset: const Offset(0, 2),
),
],
),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Question ${quiz.currentQuestionNumber}',
style: TextStyle(
fontSize: 16,
color: Colors.grey.shade600,
fontWeight: FontWeight.w500,
),
),
Text(
'${quiz.totalQuestions}',
style: TextStyle(
fontSize: 16,
color: Colors.grey.shade600,
fontWeight: FontWeight.w500,
),
),
],
),
const SizedBox(height: 12),
LinearProgressIndicator(
value: progress,
backgroundColor: Colors.grey.shade200,
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue.shade400),
minHeight: 8,
borderRadius: BorderRadius.circular(4),
),
],
),
),
const SizedBox(height: 24),
// Question Card
Card(
elevation: 8,
shadowColor: Colors.black.withOpacity(0.1),
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Colors.white,
Colors.blue.shade50,
],
),
),
child: Center(
child: Text(
quiz.questionText,
style: const TextStyle(
fontSize: 22,
fontWeight: FontWeight.w600,
height: 1.4,
),
textAlign: TextAlign.center,
),
),
),
),
const SizedBox(height: 24),
// Answer Buttons or Result
AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: !showResult ? _buildAnswerButtons() : _buildResultCard(),
),
const SizedBox(height: 20),
// Score Display
Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
decoration: BoxDecoration(
color: Colors.blue.shade100,
borderRadius: BorderRadius.circular(25),
),
child: Text(
'Score: $score',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.blue.shade800,
),
),
),
const SizedBox(height: 20),
],
),
),
),
),
),
),
);
}
Widget _buildAnswerButtons() {
return Column(
children: [
SizedBox(
width: double.infinity,
height: 60,
child: ElevatedButton(
onPressed: () => _answerQuestion(true),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green.shade500,
foregroundColor: Colors.white,
minimumSize: const Size(double.infinity, 60),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
elevation: 4,
),
child: const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.check_circle, size: 24),
SizedBox(width: 12),
Text(
'TRUE',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
letterSpacing: 1.2,
),
),
],
),
),
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
height: 60,
child: ElevatedButton(
onPressed: () => _answerQuestion(false),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red.shade500,
foregroundColor: Colors.white,
minimumSize: const Size(double.infinity, 60),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
elevation: 4,
),
child: const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.cancel, size: 24),
SizedBox(width: 12),
Text(
'FALSE',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
letterSpacing: 1.2,
),
),
],
),
),
),
],
);
}
Widget _buildResultCard() {
final isCorrect = quiz.checkAnswer(userAnswer!);
return Column(
children: [
Card(
elevation: 8,
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: isCorrect
? [Colors.green.shade50, Colors.green.shade100]
: [Colors.red.shade50, Colors.red.shade100],
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedContainer(
duration: const Duration(milliseconds: 500),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: isCorrect ? Colors.green.shade500 : Colors.red.shade500,
shape: BoxShape.circle,
),
child: Icon(
isCorrect ? Icons.check : Icons.close,
size: 40,
color: Colors.white,
),
),
const SizedBox(height: 16),
Text(
isCorrect ? 'Correct!' : 'Wrong!',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: isCorrect ? Colors.green.shade700 : Colors.red.shade700,
),
),
const SizedBox(height: 12),
Text(
'Answer: ${quiz.correctAnswer ? 'TRUE' : 'FALSE'}',
style: TextStyle(
fontSize: 18,
color: Colors.grey.shade700,
fontWeight: FontWeight.w500,
),
),
],
),
),
),
const SizedBox(height: 20),
SizedBox(
width: double.infinity,
height: 60,
child: ElevatedButton(
onPressed: _nextQuestion,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue.shade600,
foregroundColor: Colors.white,
minimumSize: const Size(double.infinity, 60),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
elevation: 4,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(quiz.isFinished ? Icons.emoji_events : Icons.arrow_forward),
const SizedBox(width: 12),
Text(
quiz.isFinished ? 'See Score' : 'Next Question',
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
],
);
}
}