name27 / flutter

0 stars 0 forks source link

퀴즈 앱 0.1 - Map #63

Open name27 opened 1 year ago

name27 commented 1 year ago

image

main.dart

import 'package:flutter/material.dart';
import 'package:quiz_app/main_page.dart';

void main() {
  runApp(const QuizApp());
}

class QuizApp extends StatelessWidget {
  const QuizApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
    debugShowCheckedModeBanner: false,
      home: MainPage(),
    );
  }
}

main_page.dart

import 'package:flutter/material.dart';
import 'package:quiz_app/QuizCard.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  var _pageController = PageController();
  List<Map<String, dynamic>> quizs = [
    {
      "question": "의학적으로 얼굴과 머리를 구분하는 기준은 어디일까요?",
      "answer": 2,
      "options": ["코", "눈썹", "귀", "머리카락"]
    },
    {
      "question": "다음 중 바다가 아닌 곳은?",
      "answer": 3,
      "options": ["카리브해", "오호츠크해", "사해", "지중해"]
    },
    {
      "question": "심청이의 아버지 심봉사의 이름은?",
      "answer": 2,
      "options": ["심전도", "심학규", "심한길", "심은하"]
    },
    {
      "question": "심청전에서 심청이가 빠진 곳은 어디일까요?",
      "answer": 4,
      "options": ["정단수", "육각수", "해모수", "인당수"]
    },
    {
      "question": "택시 번호판의 바탕색은?",
      "answer": 3,
      "options": ["녹색", "흰색", "노란색", "파란색"]
    }
  ];
  List<bool> correctList = [];
  onIncorrect() {}
  onCorrect() {}
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading:
            IconButton(onPressed: () {}, icon: Icon(Icons.navigate_before)),
        centerTitle: true,
        title: SizedBox(
          height: 30,
          width: 150,
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: correctList.length,
            itemBuilder: (context, index) => correctList[index]
                ? Padding(
                  padding: const EdgeInsets.all(3.0),
                  child: Icon(Icons.circle_outlined),
                )
                : Padding(
                  padding: const EdgeInsets.all(3.0),
                  child: Icon(Icons.close),
                ),
          ),
        ),
        actions: [
          IconButton(onPressed: () {}, icon: Icon(Icons.navigate_next))
        ],
      ),
      body: Container(
        decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
                colors: [Colors.pinkAccent, Colors.blue])),
        child: PageView.builder(
          controller: _pageController,
          scrollDirection: Axis.horizontal,
          itemCount: quizs.length,
          itemBuilder: (context, index) => Padding(
            padding: const EdgeInsets.all(20.0),
            child: UnconstrainedBox(
              child: Container(
                width: 280,
                height: 480,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(15),
                    color: Colors.white),
                child: Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Center(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          quizs[index]["question"],
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        SizedBox(
                          width: 280,
                          child: ElevatedButton(
                              onPressed: () {
                                if ((quizs[index]["answer"]) == 1) {
                                  correctList.insert(index, true);
                                } else {
                                  correctList.insert(index, false);
                                }
                                print((quizs[index]["answer"]));
                                setState(() {});
                                _pageController.nextPage(duration: Duration(seconds: 1), curve: Curves.easeIn);
                              },
                              child: Text(quizs[index]["options"][0])),
                        ),
                        SizedBox(
                          width: 280,
                          child: ElevatedButton(
                              onPressed: () {
                                if ((quizs[index]["answer"]) == 2) {
                                  correctList.insert(index, true);
                                } else {
                                  correctList.insert(index, false);
                                }
                                setState(() {});
                                _pageController.nextPage(duration: Duration(seconds: 1), curve: Curves.easeIn);
                              },
                              child: Text(quizs[index]["options"][1])),
                        ),
                        SizedBox(
                          width: 280,
                          child: ElevatedButton(
                              onPressed: () {
                                if ((quizs[index]["answer"]) == 3) {
                                  correctList.insert(index, true);
                                } else {
                                  correctList.insert(index, false);
                                }
                                setState(() {});
                                _pageController.nextPage(duration: Duration(seconds: 1), curve: Curves.easeIn);
                              },
                              child: Text(quizs[index]["options"][2])),
                        ),
                        SizedBox(
                          width: 280,
                          child: ElevatedButton(
                              onPressed: () {
                                if ((quizs[index]["answer"]) == 4) {
                                  correctList.insert(index, true);
                                } else {
                                  correctList.insert(index, false);
                                }
                                setState(() {});
                                _pageController.nextPage(duration: Duration(seconds: 1), curve: Curves.easeIn);
                              },
                              child: Text(quizs[index]["options"][3])),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.white,
        foregroundColor: Colors.black,
        onPressed: () {
          correctList.clear();
          _pageController.animateTo(1, duration: Duration(seconds: 1), curve: Curves.easeIn);
          setState(() {});
        },
        child: Icon(Icons.refresh),
      ),
    );
  }
}