Skip to content

Instantly share code, notes, and snippets.

@hongsw
Created October 2, 2024 03:25
Show Gist options
  • Select an option

  • Save hongsw/a237c9815c179bc8d4ad57daf68dff3e to your computer and use it in GitHub Desktop.

Select an option

Save hongsw/a237c9815c179bc8d4ad57daf68dff3e to your computer and use it in GitHub Desktop.
my_flutter1
import 'package:flutter/material.dart';
void main() async {
runApp(MaterialApp(
initialRoute: '/',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => FirstScreen(),
// When navigating to the "/second" route, build the SecondScreen widget.
'/second': (context) => QuizPage(),
},
onGenerateRoute: (RouteSettings settings) {
// https://sharegpt.com/c/aPDwZ64
if (settings.name == '/result') {
final Map<String, dynamic> arguments =
settings.arguments as Map<String, dynamic>;
return MaterialPageRoute(
builder: (context) => ResultPage(
correctAnswers: arguments['correctAnswers'] as int,
totalQuestions: arguments['totalQuestions'] as int,
),
);
}
},
));
}
class FirstScreen extends StatelessWidget {
const FirstScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Language learning app")), // ์ƒ๋‹จ ์•ฑ๋ฐ”
body: SingleChildScrollView(
// ์Šคํฌ๋กค์ด ๊ฐ€๋Šฅํ•œ ์œ„์ ฏ
child: Column(children: [
const Placeholder(
fallbackHeight: 200, // Placeholder์˜ ๋†’์ด๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
), // ๋†’์ด๊ฐ€ 200์ธ ๋นˆ ๋ฐ•์Šค
SizedBox(
height: 300, //
child: SingleChildScrollView(
// ์Šคํฌ๋กค์ด ๊ฐ€๋Šฅํ•œ ์œ„์ ฏ
child: ListView.builder(
shrinkWrap: true, // ๋ฆฌ์ŠคํŠธ๋ทฐ์˜ ํฌ๊ธฐ๋ฅผ ์ž์‹ ์œ„์ ฏ์— ๋งž์ถฅ๋‹ˆ๋‹ค.
physics:
const NeverScrollableScrollPhysics(), // ๋ฆฌ์ŠคํŠธ๋ทฐ์—์„œ ์Šคํฌ๋กค ๋™์ž‘์„ ๋ง‰์Šต๋‹ˆ๋‹ค.
itemCount: 17, // ์•„์ดํ…œ์˜ ๊ฐœ์ˆ˜
itemBuilder: (BuildContext context, int index) {
// ๋ฆฌ์ŠคํŠธ๋ทฐ์˜ ์•„์ดํ…œ์„ ๊ตฌ์„ฑํ•˜๋Š” ์œ„์ ฏ
return Card(
// ์นด๋“œ ์œ„์ ฏ
child: ListTile(
// ๋ฆฌ์ŠคํŠธ ํƒ€์ผ ์œ„์ ฏ
leading:
FlutterLogo(size: 72.0), // ์นด๋“œ์˜ ์™ผ์ชฝ์— ์ด๋ฏธ์ง€ ์•„์ด์ฝ˜์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
title: Text('Three-line ListTile'), // ๋ฆฌ์ŠคํŠธ ํƒ€์ผ์˜ ์ œ๋ชฉ
subtitle: Text(
'A sufficiently long subtitle warrants three lines. $index'), // ๋ฆฌ์ŠคํŠธ ํƒ€์ผ์˜ ๋ถ€์ œ๋ชฉ
trailing:
Icon(Icons.more_vert), // ๋ฆฌ์ŠคํŠธ ํƒ€์ผ์˜ ์˜ค๋ฅธ์ชฝ์— ํ‘œ์‹œ๋˜๋Š” ์ถ”๊ฐ€ ์ •๋ณด ์•„์ด์ฝ˜
isThreeLine: true, // ๋ฆฌ์ŠคํŠธ ํƒ€์ผ์ด ์„ธ ์ค„๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.
onTap: () {
Navigator.pushNamed(context,
'/second'); // ๋ฆฌ์ŠคํŠธ ํƒ€์ผ์„ ํƒญํ•˜๋ฉด /second ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
}, // Handle your onTap here.
),
);
},
))),
const Placeholder(
fallbackHeight: 200, // Placeholder์˜ ๋†’์ด๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
), // ๋†’์ด๊ฐ€ 200์ธ ๋นˆ ๋ฐ•์Šค
])));
}
}
// https://sharegpt.com/c/B3lxchS
class QuizPage extends StatefulWidget {
@override
_QuizPageState createState() => _QuizPageState();
}
class _QuizPageState extends State<QuizPage> {
int _correctAnswers = 0;
int _wrongAnswers = 0;
int _currentIndex = 0;
final List<Map<String, dynamic>> _questions = [
{
'question': 'apple',
"answers": ['์‚ฌ๊ณผ', 'ํฌ๋„', '๋ฐฐ'],
'correctIndex': 0,
},
{
'question': 'banana',
'answers': ['๋ฐ”๋‚˜๋‚˜', 'ํ‚ค์œ„', '๋”ธ๊ธฐ'],
'correctIndex': 0,
},
{
'question': 'cherry',
'answers': ['์ฒด๋ฆฌ', '์ˆ˜๋ฐ•', 'ํฌ๋„'],
'correctIndex': 0,
},
];
void _onAnswerSelected(int selectedIndex) {
setState(() {
if (_questions[_currentIndex]['correctIndex'] == selectedIndex) {
_correctAnswers++;
} else {
_wrongAnswers++;
}
if (_currentIndex == _questions.length - 1) {
Navigator.pushNamed(
context,
'/result',
arguments: {
'correctAnswers': _correctAnswers,
'totalQuestions': _questions.length
},
);
} else {
_currentIndex++;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ํ€ด์ฆˆ'),
),
body: Column(
children: [
SizedBox(height: 16),
LinearProgressIndicator(
value: (_currentIndex + 1) / _questions.length,
backgroundColor: Colors.grey[300],
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
),
SizedBox(height: 16),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
'๋ฌธ์ œ ${_currentIndex + 1}/${_questions.length}',
style: TextStyle(fontSize: 16),
),
),
SizedBox(height: 16),
Expanded(
child: Card(
margin: const EdgeInsets.symmetric(horizontal: 5),
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
_questions[_currentIndex]['question'],
style: TextStyle(fontSize: 28),
),
...(_questions[_currentIndex]['answers'] as List<String>)
.asMap()
.entries
.map(
(entry) => ElevatedButton(
onPressed: () => _onAnswerSelected(entry.key),
child: Text(
entry.value,
style: TextStyle(fontSize: 20),
),
),
)
.toList(),
],
),
),
),
),
],
),
);
}
}
// https://sharegpt.com/c/aPDwZ64
class ResultPage extends StatelessWidget {
final int correctAnswers;
final int totalQuestions;
ResultPage({required this.correctAnswers, required this.totalQuestions});
String get resultPhrase {
double percentage = correctAnswers / totalQuestions;
if (percentage >= 0.9) {
return '๋Œ€๋‹จํ•ด์š”!';
} else if (percentage >= 0.7) {
return '์ž˜ํ–ˆ์–ด์š”!';
} else if (percentage >= 0.5) {
return '์ข€ ๋” ๋…ธ๋ ฅํ•ด์ฃผ์„ธ์š”';
} else {
return '๋ถ„๋ฐœํ•˜์„ธ์š”!';
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('๊ฒฐ๊ณผ'),
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
resultPhrase,
style: TextStyle(fontSize: 24),
textAlign: TextAlign.center,
),
SizedBox(height: 16),
Text(
'๋งž์€ ๊ฐœ์ˆ˜: $correctAnswers/$totalQuestions',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () {
Navigator.pushNamedAndRemoveUntil(
context,
'/',
(route) => false,
);
},
child: Text('ํ™ˆ์œผ๋กœ'),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment