name27 / flutter

0 stars 0 forks source link

GetxController 활용 전역 컨트롤러 #101

Open name27 opened 1 year ago

name27 commented 1 year ago

image image

main.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:use_get_x_app/controller/app_setting_controller.dart';
import 'package:use_get_x_app/page/main_page.dart';

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

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

  @override
  Widget build(BuildContext context) {
    var controller = Get.put(AppSettingController(
        isSoundOn: true,
        isNotificationOn: false,
        appAuthor: 'Author',
        appName: 'App',
        appVersion: '0.1',
        appPackageName: 'App Package Name'));
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData.dark(),
      home: MainPage(),
    );
  }
}

app_setting_controller.dart

import 'package:get/get.dart';

class AppSettingController extends GetxController {
  bool isSoundOn;
  bool isNotificationOn;
  String appVersion;
  String appName;
  String appAuthor;
  String appPackageName;
  DateTime? lastUpdated;

  AppSettingController(
      {required this.isSoundOn,
      required this.isNotificationOn,
      required this.appAuthor,
      required this.appName,
      required this.appVersion,
      required this.appPackageName,
      this.lastUpdated});
}

main_page.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:use_get_x_app/controller/app_setting_controller.dart';
import 'package:use_get_x_app/page/sub_page.dart';

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

  @override
  Widget build(BuildContext context) {
    var controller = Get.find<AppSettingController>();
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [Text(controller.appName), Text(controller.appVersion)],
        ),
        actions: [
          controller.isSoundOn
              ? IconButton(onPressed: () {}, icon: Icon(Icons.volume_up))
              : IconButton(onPressed: () {}, icon: Icon(Icons.volume_off)),
          controller.isNotificationOn
              ? IconButton(
                  onPressed: () {}, icon: Icon(Icons.notifications_active))
              : IconButton(
                  onPressed: () {}, icon: Icon(Icons.notifications_off)),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [Text('App Author : ${controller.appAuthor}')],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Get.to(SubPage());
        },
        child: Icon(Icons.navigate_next),
      ),
    );
  }
}

sub_page.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controller/app_setting_controller.dart';

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

  @override
  Widget build(BuildContext context) {
    var controller = Get.find<AppSettingController>();
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [Text(controller.appName), Text(controller.appVersion)],
        ),
      ),
      body: Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text('Sub Page'),
        ],
      )),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Get.back();
        },
        child: Icon(Icons.navigate_before),
      ),
    );
  }
}