위의 결과물처럼 메인페이지에서의 코인이 1초마다 +1씩 되도록 만드시오.
Icon(
FontAwesomeIcons.bitcoin,
size: 96.0,
color: Colors.yellow.shade700,
),
CoinController를 만들고, GetxController를 extends하여 위와 같은 결과물을 만들 수 있도록 하시오.
[상점으로 이동하기] 버튼을 누르면 상점 페이지로 이동한다.
GetxController를 활용하여 다음의 정보를 포함하고있는 “전역”컨트롤러를 만드시오
(위와 같은 프로젝트에 만들기)
bool | isSoundOn |
---|---|
bool | isNotificationOn |
String | appVersion |
String | appName |
String | appAuthor |
String | appPackageName |
DateTime? | lastUpdated |
```dart
Get.to(()=>페이지명());
```
import 'dart:async';
import 'package:get/get.dart';
class AppController extends GetxController {
RxInt count = 0.obs; // GetX의 상태 변수
startTimer() {
Timer.periodic(Duration(seconds: 1), (timer) {
// 타이머가 실행될 때마다 count 변수를 1씩 증가시킵니다.
count.value++;
});
}
void onInit() {
// TODO: implement onInit
super.onInit();
startTimer();
ever(
count,
(newValue) {
if (count % 10 == 0 && count != 0) {
return Get.snackbar('코인 ${count}개 돌파!', '축하합니다!');
}
},
);
}
}
import 'package:get/get.dart';
class AppSettingController extends GetxController {
RxBool isSoundOn;
RxBool isNotificationOn;
String appVersion;
String appName;
String appAuthor;
String appPackageName;
DateTime lastUpdated;
AppSettingController({
required this.isSoundOn,
required this.isNotificationOn,
required this.appVersion,
required this.appName,
required this.appAuthor,
required this.appPackageName,
required this.lastUpdated,
});
}
import 'package:day16_assignment/controller/app_setting_controller.dart';
import 'package:day16_assignment/pages/sub_page.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';
import '../controller/app_controller.dart';
class MainPage extends StatefulWidget {
MainPage({super.key});
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
Widget build(BuildContext context) {
final controller = Get.put(AppController());
final appSettingController = Get.put(
AppSettingController(
isSoundOn: RxBool(true),
isNotificationOn: RxBool(true),
appVersion: "1.0.0",
appName: '김건으로 탕후루 만들기',
appAuthor: '손세은',
appPackageName: 'thoothBreak',
lastUpdated: DateTime.now(),
),
);
return SafeArea(
child: Scaffold(
body: Align(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Align(
alignment: AlignmentDirectional.topStart,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Obx(() => Text(
'브금 상태: ${appSettingController.isSoundOn.value.toString()}',
style: TextStyle(fontSize: 24))),
TextButton(
onPressed: () {
appSettingController.isSoundOn.value =
!appSettingController.isSoundOn.value;
},
child: Text('브금 틀기')),
],
),
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Obx(() => Text('Count: ${controller.count.value}',
style: TextStyle(fontSize: 24))),
Icon(
FontAwesomeIcons.bitcoin,
size: 96.0,
color: Colors.yellow.shade700,
),
TextButton(
onPressed: () {
Get.to(() => SubPage());
},
child: Text('상점으로 이동하기')),
],
),
),
],
),
),
),
);
}
}
import 'package:day16_assignment/controller/app_setting_controller.dart';
import 'package:day16_assignment/pages/main_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller/app_controller.dart';
class SubPage extends StatelessWidget {
const SubPage({super.key});
Widget build(BuildContext context) {
var controller = Get.find<AppController>();
final appSettingController = Get.find<AppSettingController>();
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('상점', style: TextStyle(fontSize: 20)),
Obx(() => Text('현재 보유한 코인: ${controller.count.value}',
style: TextStyle(fontSize: 25))),
TextButton(
onPressed: () {
controller.count.value = 0;
},
child: Text('코인리셋')),
TextButton(
onPressed: () {
if (appSettingController.isNotificationOn.value) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('정말 돌아가시겠습니까?'),
actions: [
TextButton(
onPressed: () {
Get.to(() => MainPage());
},
child: Text('넵 돌아갈래유'),
),
],
);
},
);
}
},
child: Text('돌아가기'),
)
],
),
),
);
}
}
import 'package:flutter/material.dart';
import 'page/main_page.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: MainPage(),
);
}
}