Created
June 18, 2022 05:19
-
-
Save koboolean/f86884b5bdf923ba493dac27d8cf9a12 to your computer and use it in GitHub Desktop.
flutter number Quiz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:dio/dio.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: HomePage(), | |
); | |
} | |
} | |
/// 홈 페이지 | |
class HomePage extends StatefulWidget { | |
const HomePage({Key? key}) : super(key: key); | |
@override | |
State<HomePage> createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
String quiz = ""; // 보여줄 퀴즈 | |
@override | |
void initState() { | |
super.initState(); | |
getQuiz(); | |
} | |
/// 퀴즈 가져오기 | |
void getQuiz() async { | |
String trivia = await getNumberTrivia(); | |
setState(() { | |
quiz = trivia; | |
}); | |
} | |
/// Numbers API 호출하기 | |
Future<String> getNumberTrivia() async { | |
// get 메소드로 URL 호출 | |
Response result = await Dio().get('http://numbersapi.com/random/trivia'); | |
String trivia = result.data; // 응답 결과 가져오기 | |
print(trivia); | |
return trivia; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.pinkAccent, | |
body: SafeArea( | |
child: Container( | |
padding: const EdgeInsets.all(16), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
// quiz | |
Expanded( | |
child: Center( | |
child: Text( | |
quiz, | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 28, | |
color: Colors.white, | |
), | |
), | |
), | |
), | |
// New Quiz 버튼 | |
SizedBox( | |
height: 42, | |
child: ElevatedButton( | |
child: Text( | |
"New Quiz", | |
style: TextStyle( | |
color: Colors.pinkAccent, | |
fontSize: 24, | |
), | |
), | |
style: ButtonStyle( | |
backgroundColor: MaterialStateProperty.all(Colors.white), | |
), | |
onPressed: () async { | |
getQuiz(); | |
}, | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment