Source code

We have made the main source code publicly available. We hope it will be helpful as a reference for app development.

● Japanese ● ご注意 ● このページで公開しているのはDartコードのみです。アプリの動作には画像・音声・多言語テキストなどの追加データが必要なため、ここに掲載している情報だけではアプリを完全に再現することはできません。 ● アプリは継続的に機能拡張しているため、ストアで公開している最新版とコード内容が異なる場合があります。 ● コードはコピーして自由にご利用いただけますが、著作権は放棄していません。そのため、コード全体の再掲載はご遠慮ください。一部の引用や改変しての再掲載は問題ありません。個人利用・商用利用を問わず、アプリ開発の参考として自由にお使いください。 ● このコードは「お手本」として公開しているものではありません。ミニアプリとして作成しているため、変数名など細部に配慮していない箇所や誤りが含まれる可能性があります。参考程度にご覧ください。 ● 他の開発者の公開コードを参考にした部分も含まれています。アプリ開発の熟練者が書いたコードではない点もご了承ください。 ● ここはエンジニア向けの技術情報共有サービスではないため、詳細な説明は省略しています。 ● 本コードは無保証であり、動作や品質を保証するものではありません。
Copyright© エーオーシステム株式会社

● English ● Notice ● Only the Dart source code is provided on this page. Additional assets such as images, audio files, and multilingual text are required for the application to function. Therefore, the information here alone is not sufficient to fully reproduce the app. ● The app is continuously being improved, so the code published here may differ from the latest version available on the app stores. ● You are free to copy and use the code, but the copyright is not waived. For this reason, please refrain from redistributing (reposting) the entire code. Partial quotation or redistribution of modified portions is allowed. You may use the code freely for reference in your own app development, whether for personal or commercial use. ● This code is not intended to serve as a coding example or best practice. As this is a small sample app, some variable names and details may lack refinement, and there may be mistakes. Please use it only as a reference. ● Some parts of the code are based on publicly available examples from other developers. Please note that this code was not written by an expert in Android app development. ● This is not a technical knowledge-sharing service for engineers, so detailed explanations are omitted. ● The code is provided "as is" without any warranty of any kind.
Copyright© ao-system, Inc.

Last build date: 2026-05-19

name: luckybox
description: "LuckyBox"

publish_to: 'none'

version: 2.5.0+29

environment:
  sdk: ^3.11.5

dependencies:   #flutter pub upgrade --major-versions
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.8
  shared_preferences: ^2.0.17
  flutter_localizations:    # flutter gen-l10n
    sdk: flutter
  intl: ^0.20.2
  google_mobile_ads: ^8.0.0
  just_audio: ^0.10.4
  flutter_tts: ^4.2.3
  collection: ^1.19.1
  wakelock_plus: ^1.4.0
  in_app_review: ^2.0.11
  app_tracking_transparency: ^2.0.4
  app_settings: ^7.0.0

dev_dependencies:
  flutter_lints: ^6.0.0
  flutter_launcher_icons: ^0.14.4    #flutter pub run flutter_launcher_icons
  flutter_native_splash: ^2.3.6     #flutter pub run flutter_native_splash:create

flutter_launcher_icons:
  android: "launcher_icon"
  ios: true
  image_path: "assets/icon/icon.png"
  adaptive_icon_background: "assets/icon/icon_back.png"
  adaptive_icon_foreground: "assets/icon/icon_fore.png"

flutter_native_splash:
  color: '#ffcc00'
  image: 'assets/image/splash.png'
  color_dark: '#ffcc00'
  image_dark: 'assets/image/splash.png'
  fullscreen: true
  android_12:
    icon_background_color: '#ffcc00'
    image: 'assets/image/splash.png'
    icon_background_color_dark: '#ffcc00'
    image_dark: 'assets/image/splash.png'

flutter:
  generate: true
  uses-material-design: true

  assets:
    - assets/image/
    - assets/image/box/
    - assets/image/ticket/
    - assets/sound/

import 'package:flutter/cupertino.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'package:luckybox/ad_manager.dart';

class AdBannerWidget extends StatefulWidget {
  final AdManager adManager;
  const AdBannerWidget({super.key, required this.adManager});
  @override
  State<AdBannerWidget> createState() => _AdBannerWidgetState();
}

class _AdBannerWidgetState extends State<AdBannerWidget> {
  int _lastBannerWidthDp = 0;
  bool _isAdLoaded = false;
  bool _isLoading = false;
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: LayoutBuilder(
        builder: (context, constraints) {
          final int width = constraints.maxWidth.isFinite ? constraints.maxWidth.truncate() : MediaQuery.of(context).size.width.truncate();
          final bannerAd = widget.adManager.bannerAd;
          if (width > 0) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              if (mounted) {
                final bannerAd = widget.adManager.bannerAd;
                final bool widthChanged = _lastBannerWidthDp != width;
                final bool sizeMismatch = bannerAd == null || bannerAd.size.width != width;
                if ((widthChanged || !_isAdLoaded || sizeMismatch) && !_isLoading) {
                  _lastBannerWidthDp = width;
                  setState(() { _isAdLoaded = false; _isLoading = true; });
                  widget.adManager.loadAdaptiveBannerAd(width, () {
                    if (mounted) {
                      setState(() { _isAdLoaded = true; _isLoading = false; });
                    }
                  });
                }
              }
            });
          }
          if (_isAdLoaded && bannerAd != null) {
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(
                      width: bannerAd.size.width.toDouble(),
                      height: bannerAd.size.height.toDouble(),
                      child: AdWidget(ad: bannerAd),
                    ),
                  ],
                )
              ]
            );
          } else {
            return const SizedBox.shrink();
          }
        },
      ),
    );
  }
}
import 'dart:async';
import 'dart:io' show Platform;
import 'dart:ui';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:flutter/widgets.dart';

import 'package:luckybox/_secrets.dart';

class AdManager {
  static String get _adUnitId => Platform.isIOS ? Secrets.adUnitIdIos : Secrets.adUnitIdAndroid;
  BannerAd? _bannerAd;
  int _lastWidthPx = 0;
  VoidCallback? _onLoadedCb;
  Timer? _retryTimer;
  int _retryAttempt = 0;

  BannerAd? get bannerAd => _bannerAd;

  /// アプリ起動時の設定
  /// UMP(同意管理)を導入したため、手動のNPA設定は不要になった。
  static Future<void> initForNPA() async {
    if (kIsWeb) {
      return;
    }
    // UMP SDK が保存した同意情報を MobileAds SDK が自動で読み取るため、
    // ここで RequestConfiguration を使って NPA を強制する必要はない。
    await MobileAds.instance.updateRequestConfiguration(
      RequestConfiguration(
        tagForChildDirectedTreatment: TagForChildDirectedTreatment.unspecified,
        testDeviceIds: Secrets.umpConsentTestDeviceIds, //テストデバイスID:広告の誤クリック防止
      ),
    );
  }

  Future<void> loadAdaptiveBannerAd(int widthPx, VoidCallback onAdLoaded) async {
    if (kIsWeb) {
      return;
    }
    _onLoadedCb = onAdLoaded;
    _lastWidthPx = widthPx;
    _retryAttempt = 0;
    _retryTimer?.cancel();
    _startLoad(widthPx);
  }

  static AdRequest getAdRequest() {
    // ユーザーの同意状態(TCF信号)は、SDKによって自動的に付与される。
    // 手動で npa: 1 を送ると、UMPでのユーザーの選択と競合する可能性があるため、空で返す。
    // AdRequest(nonPersonalizedAds: true);にはしない
    return const AdRequest();
  }

  Future<void> _startLoad(int widthPx) async {
    if (kIsWeb) {
      return;
    }
    _bannerAd?.dispose();
    AnchoredAdaptiveBannerAdSize? adaptiveSize;
    try {
      adaptiveSize = await AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(widthPx);
    } catch (_) {
      adaptiveSize = null;
    }
    final AdSize size = adaptiveSize ?? AdSize.fullBanner;
    _bannerAd = BannerAd(
      adUnitId: _adUnitId,
      request: getAdRequest(),
      size: size,
      listener: BannerAdListener(
        onAdLoaded: (ad) {
          _retryTimer?.cancel();
          _retryAttempt = 0;
          final cb = _onLoadedCb;
          if (cb != null) {
            cb();
          }
        },
        onAdFailedToLoad: (ad, err) {
          ad.dispose();
          _scheduleRetry();
        },
      ),
    )..load();
  }

  void _scheduleRetry() {
    if (kIsWeb) return;
    _retryTimer?.cancel();
    _retryAttempt = (_retryAttempt + 1).clamp(1, 5);
    final seconds = _retryAttempt >= 4 ? 30 : (3 << (_retryAttempt - 1));
    _retryTimer = Timer(Duration(seconds: seconds), () {
      _startLoad(_lastWidthPx > 0 ? _lastWidthPx : 320);
    });
  }

  void dispose() {
    _bannerAd?.dispose();
    _retryTimer?.cancel();
  }
}
import 'dart:async';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:flutter/widgets.dart';

import 'package:luckybox/l10n/app_localizations.dart';
import 'package:luckybox/_secrets.dart';

/// UMP状態格納用
class AdUmpState {
  final PrivacyOptionsRequirementStatus privacyStatus;
  final ConsentStatus consentStatus;
  final bool privacyOptionsRequired;
  final bool isChecking;
  const AdUmpState({
    required this.privacyStatus,
    required this.consentStatus,
    required this.privacyOptionsRequired,
    required this.isChecking,
  });

  AdUmpState copyWith({
    PrivacyOptionsRequirementStatus? privacyStatus,
    ConsentStatus? consentStatus,
    bool? privacyOptionsRequired,
    bool? isChecking,
  }) {
    return AdUmpState(
      privacyStatus: privacyStatus ?? this.privacyStatus,
      consentStatus: consentStatus ?? this.consentStatus,
      privacyOptionsRequired:
      privacyOptionsRequired ?? this.privacyOptionsRequired,
      isChecking: isChecking ?? this.isChecking,
    );
  }

  static const initial = AdUmpState(
    privacyStatus: PrivacyOptionsRequirementStatus.unknown,
    consentStatus: ConsentStatus.unknown,
    privacyOptionsRequired: false,
    isChecking: false,
  );
}

//UMPコントローラ
class AdUmpConsentController {
  //デバッグ用:同意フォームの表示テスト:EEA地域を強制する(本番ではfalseにすること)
  final bool forceEeaForDebug = false;
  //デバッグ用:同意フォームの表示テスト:EEA地域を強制するテストデバイスID
  static final List<String> _testDeviceIds = Secrets.umpConsentTestDeviceIds;

  ConsentRequestParameters _buildParams() {
    if (forceEeaForDebug && _testDeviceIds.isNotEmpty) {
      return ConsentRequestParameters(
        consentDebugSettings: ConsentDebugSettings(
          debugGeography: DebugGeography.debugGeographyEea,
          testIdentifiers: _testDeviceIds,
        ),
      );
    }
    return ConsentRequestParameters();
  }

  //同意情報を更新して状態を返す
  Future<AdUmpState> updateConsentInfo({AdUmpState current = AdUmpState.initial}) async {
    if (kIsWeb) {
      return current;
    }
    var state = current.copyWith(isChecking: true);

    try {
      final params = _buildParams();
      final completer = Completer<AdUmpState>();

      ConsentInformation.instance.requestConsentInfoUpdate(
        params,
        () async {
          //同意フォームが必要なら表示する
          ConsentForm.loadAndShowConsentFormIfRequired((formError) async {
            final s = await ConsentInformation.instance.getPrivacyOptionsRequirementStatus();
            final c = await ConsentInformation.instance.getConsentStatus();
            completer.complete(
              state.copyWith(
                privacyStatus: s,
                consentStatus: c,
                privacyOptionsRequired: s == PrivacyOptionsRequirementStatus.required,
                isChecking: false,
              ),
            );
          });
        },
        (FormError e) {
          completer.complete(state.copyWith(isChecking: false));
        },
      );
      return await completer.future;
    } catch (_) {
      return state.copyWith(isChecking: false);
    }
  }

  //プライバシーオプションフォームを表示
  Future<FormError?> showPrivacyOptions() async {
    if (kIsWeb) return null;
    final completer = Completer<FormError?>();
    ConsentForm.showPrivacyOptionsForm((FormError? e) {
      completer.complete(e);
    });
    return completer.future;
  }
}

class AdUmpService {
  final AdUmpConsentController _adUmpConsentController = AdUmpConsentController();

  Future<AdUmpState> updateConsentInfo(AdUmpState current) async {
    return await _adUmpConsentController.updateConsentInfo(current: current);
  }

  Future<void> requestConsentInfoUpdate(ConsentRequestParameters params) async {
    final completer = Completer<void>();
    ConsentInformation.instance.requestConsentInfoUpdate(
      params,
          () => completer.complete(),
          (FormError error) => completer.completeError(error),
    );
    return completer.future;
  }

  Future<FormError?> showPrivacyOptions() async {
    return await _adUmpConsentController.showPrivacyOptions();
  }
}

extension ConsentStatusL10n on ConsentStatus {
  String localized(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    switch (this) {
      case ConsentStatus.obtained:
        return l.cmpConsentStatusObtained;
      case ConsentStatus.required:
        return l.cmpConsentStatusRequired;
      case ConsentStatus.notRequired:
        return l.cmpConsentStatusNotRequired;
      case ConsentStatus.unknown:
        return l.cmpConsentStatusUnknown;
    }
  }
}
///
/// @author akira ohmachi
/// @copyright ao-system, Inc.
/// @date 2023-01-27
///
library;

import 'package:just_audio/just_audio.dart';

import 'package:luckybox/const_value.dart';
import 'package:luckybox/model.dart';

class AudioPlay {
  //音を重ねて連続再生できるようにインスタンスを用意しておき、順繰りに使う。
  static final List<AudioPlayer> _player01 = [
    AudioPlayer(),
    AudioPlayer(),
    AudioPlayer(),
  ];
  static final List<AudioPlayer> _player02 = [
    AudioPlayer(),
    AudioPlayer(),
    AudioPlayer(),
  ];
  int _player01Ptr = 0;
  int _player02Ptr = 0;

  //constructor
  AudioPlay() {
    constructor();
  }
  void constructor() async {
    for (int i = 0; i < _player01.length; i++) {
      await _player01[i].setVolume(0);
      await _player01[i].setAsset(ConstValue.audioReady);
    }
    for (int i = 0; i < _player02.length; i++) {
      await _player02[i].setVolume(0);
      await _player02[i].setAsset(ConstValue.audioAction);
    }
    playZero();
  }
  void dispose() {
    for (int i = 0; i < _player01.length; i++) {
      _player01[i].dispose();
    }
    for (int i = 0; i < _player02.length; i++) {
      _player02[i].dispose();
    }
  }

  //最初に音が鳴らないのを回避する方法
  void playZero() async {
    AudioPlayer ap = AudioPlayer();
    await ap.setAsset(ConstValue.audioZero);
    await ap.load();
    await ap.play();
  }
  //
  void play01() async {
    if (Model.soundReadyVolume == 0) {
      return;
    }
    _player01Ptr += 1;
    if (_player01Ptr >= _player01.length) {
      _player01Ptr = 0;
    }
    await _player01[_player01Ptr].setVolume(Model.soundReadyVolume);
    await _player01[_player01Ptr].pause();
    await _player01[_player01Ptr].seek(const Duration(milliseconds: 100));
    await _player01[_player01Ptr].play();
  }
  //
  void play02() async {
    if (Model.soundStartVolume == 0) {
      return;
    }
    _player02Ptr += 1;
    if (_player02Ptr >= _player02.length) {
      _player02Ptr = 0;
    }
    await _player02[_player02Ptr].setVolume(Model.soundStartVolume);
    await _player02[_player02Ptr].pause();
    await _player02[_player02Ptr].seek(Duration.zero);
    await _player02[_player02Ptr].play();
  }
}
///
/// @author akira ohmachi
/// @copyright ao-system, Inc.
/// @date 2023-10-15
///
library;

class ConstValue {
  static const String prizeTextDefault = '1:car\n3:bicycle\n5:bag\n100:lose';
  //sound
  static const String audioZero = 'assets/sound/zero.wav'; //無音1秒
  static const String audioReady = 'assets/sound/switch.wav';
  static const String audioAction = 'assets/sound/slide.wav';
  //image
  static const List<String> imageBoxes = [
    'assets/image/box/box001.webp',
    'assets/image/box/box002.webp',
    'assets/image/box/box003.webp',
    'assets/image/box/box004.webp',
    'assets/image/box/box005.webp',
    'assets/image/box/box006.webp',
    'assets/image/box/box007.webp',
    'assets/image/box/box008.webp',
    'assets/image/box/box009.webp',
    'assets/image/box/box010.webp',
    'assets/image/box/box011.webp',
    'assets/image/box/box012.webp',
    'assets/image/box/box013.webp',
    'assets/image/box/box014.webp',
    'assets/image/box/box015.webp',
    'assets/image/box/box016.webp',
    'assets/image/box/box017.webp',
    'assets/image/box/box018.webp',
    'assets/image/box/box019.webp',
    'assets/image/box/box020.webp',
    'assets/image/box/box021.webp',
    'assets/image/box/box022.webp',
    'assets/image/box/box023.webp',
    'assets/image/box/box024.webp',
    'assets/image/box/box025.webp',
    'assets/image/box/box026.webp',
    'assets/image/box/box027.webp',
    'assets/image/box/box028.webp',
    'assets/image/box/box029.webp',
    'assets/image/box/box030.webp',
    'assets/image/box/box031.webp',
    'assets/image/box/box032.webp',
    'assets/image/box/box033.webp',
    'assets/image/box/box034.webp',
    'assets/image/box/box035.webp',
    'assets/image/box/box036.webp',
    'assets/image/box/box037.webp',
    'assets/image/box/box038.webp',
    'assets/image/box/box039.webp',
    'assets/image/box/box040.webp',
    'assets/image/box/box041.webp',
    'assets/image/box/box042.webp',
    'assets/image/box/box043.webp',
    'assets/image/box/box044.webp',
    'assets/image/box/box045.webp',
    'assets/image/box/box046.webp',
    'assets/image/box/box047.webp',
    'assets/image/box/box048.webp',
    'assets/image/box/box049.webp',
    'assets/image/box/box050.webp',
    'assets/image/box/box051.webp',
    'assets/image/box/box052.webp',
    'assets/image/box/box053.webp',
    'assets/image/box/box054.webp',
    'assets/image/box/box055.webp',
    'assets/image/box/box056.webp',
    'assets/image/box/box057.webp',
    'assets/image/box/box058.webp',
    'assets/image/box/box059.webp',
    'assets/image/box/box060.webp',
    'assets/image/box/box061.webp',
    'assets/image/box/box062.webp',
    'assets/image/box/box063.webp',
    'assets/image/box/box064.webp',
    'assets/image/box/box065.webp',
    'assets/image/box/box066.webp',
    'assets/image/box/box067.webp',
    'assets/image/box/box068.webp',
    'assets/image/box/box069.webp',
    'assets/image/box/box070.webp',
    'assets/image/box/box071.webp',
    'assets/image/box/box072.webp',
    'assets/image/box/box073.webp',
    'assets/image/box/box074.webp',
    'assets/image/box/box075.webp',
    'assets/image/box/box076.webp',
    'assets/image/box/box077.webp',
    'assets/image/box/box078.webp',
    'assets/image/box/box079.webp',
    'assets/image/box/box080.webp',
    'assets/image/box/box081.webp',
    'assets/image/box/box082.webp',
    'assets/image/box/box083.webp',
    'assets/image/box/box084.webp',
    'assets/image/box/box085.webp',
    'assets/image/box/box086.webp',
    'assets/image/box/box087.webp',
    'assets/image/box/box088.webp',
    'assets/image/box/box089.webp',
    'assets/image/box/box090.webp',
    'assets/image/box/box091.webp',
    'assets/image/box/box092.webp',
    'assets/image/box/box093.webp',
    'assets/image/box/box094.webp',
    'assets/image/box/box095.webp',
    'assets/image/box/box096.webp',
    'assets/image/box/box097.webp',
    'assets/image/box/box098.webp',
    'assets/image/box/box099.webp',
    'assets/image/box/box100.webp',
    'assets/image/box/box101.webp',
    'assets/image/box/box102.webp',
    'assets/image/box/box103.webp',
    'assets/image/box/box104.webp',
    'assets/image/box/box105.webp',
    'assets/image/box/box106.webp',
    'assets/image/box/box107.webp',
    'assets/image/box/box108.webp',
    'assets/image/box/box109.webp',
    'assets/image/box/box110.webp',
    'assets/image/box/box111.webp',
    'assets/image/box/box112.webp',
    'assets/image/box/box113.webp',
    'assets/image/box/box114.webp',
    'assets/image/box/box115.webp',
    'assets/image/box/box116.webp',
    'assets/image/box/box117.webp',
    'assets/image/box/box118.webp',
    'assets/image/box/box119.webp',
    'assets/image/box/box120.webp',
  ];
  static const List<String> imageTickets = [
    /*
    'assets/image/ticket/ticket070.webp',
    'assets/image/ticket/ticket071.webp',
    'assets/image/ticket/ticket072.webp',
    'assets/image/ticket/ticket073.webp',
    'assets/image/ticket/ticket074.webp',
    'assets/image/ticket/ticket075.webp',
    'assets/image/ticket/ticket076.webp',
    'assets/image/ticket/ticket077.webp',
    'assets/image/ticket/ticket078.webp',
    'assets/image/ticket/ticket079.webp',
     */
    'assets/image/ticket/ticket080.webp',
    'assets/image/ticket/ticket081.webp',
    'assets/image/ticket/ticket082.webp',
    'assets/image/ticket/ticket083.webp',
    'assets/image/ticket/ticket084.webp',
    'assets/image/ticket/ticket085.webp',
    'assets/image/ticket/ticket086.webp',
    'assets/image/ticket/ticket087.webp',
    'assets/image/ticket/ticket088.webp',
    'assets/image/ticket/ticket089.webp',
    /*
    'assets/image/ticket/ticket090.webp',
    'assets/image/ticket/ticket091.webp',
    'assets/image/ticket/ticket092.webp',
    'assets/image/ticket/ticket093.webp',
    'assets/image/ticket/ticket094.webp',
    'assets/image/ticket/ticket095.webp',
    'assets/image/ticket/ticket096.webp',
    'assets/image/ticket/ticket097.webp',
    'assets/image/ticket/ticket098.webp',
    'assets/image/ticket/ticket099.webp',
    'assets/image/ticket/ticket100.webp',
    'assets/image/ticket/ticket101.webp',
    'assets/image/ticket/ticket102.webp',
    'assets/image/ticket/ticket103.webp',
    'assets/image/ticket/ticket104.webp',
    'assets/image/ticket/ticket105.webp',
    'assets/image/ticket/ticket106.webp',
    'assets/image/ticket/ticket107.webp',
    'assets/image/ticket/ticket108.webp',
    'assets/image/ticket/ticket109.webp',
    'assets/image/ticket/ticket110.webp',
    'assets/image/ticket/ticket111.webp',
    'assets/image/ticket/ticket112.webp',
    'assets/image/ticket/ticket113.webp',
    'assets/image/ticket/ticket114.webp',
    'assets/image/ticket/ticket115.webp',
    'assets/image/ticket/ticket116.webp',

     */
  ];
  static const List<String> imageNumbers = [
    'assets/image/number_null.webp',
    'assets/image/number1.webp',
    'assets/image/number2.webp',
    'assets/image/number3.webp',
    'assets/image/number4.webp',
    'assets/image/number5.webp',
    'assets/image/number6.webp',
    'assets/image/number7.webp',
    'assets/image/number8.webp',
    'assets/image/number9.webp',
  ];

}
import 'dart:ui' as ui;
import 'package:flutter/cupertino.dart';

class FramePainter extends CustomPainter {
  final ui.Image frame;
  FramePainter(this.frame);
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint();
    // 1. フレームの短辺を基準に正方形にクロップ
    final side = frame.width < frame.height ? frame.width : frame.height;
    final src = Rect.fromLTWH(
      (frame.width - side) / 2.0,
      (frame.height - side) / 2.0,
      side.toDouble(),
      side.toDouble(),
    );
    // 2. 描画先も正方形に制限
    final length = size.shortestSide;
    final dx = (size.width - length) / 2.0;
    final dy = (size.height - length) / 2.0;
    final dst = Rect.fromLTWH(dx, dy, length, length);
    canvas.drawImageRect(frame, src, dst, paint);
  }
  @override
  bool shouldRepaint(covariant FramePainter oldDelegate) {
    return oldDelegate.frame != frame;
  }
}

import 'dart:async';
import 'dart:math';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'package:luckybox/l10n/app_localizations.dart';
import 'package:luckybox/const_value.dart';
import 'package:luckybox/theme_color.dart';
import 'package:luckybox/setting_page.dart';
import 'package:luckybox/ad_manager.dart';
import 'package:luckybox/ad_banner_widget.dart';
import 'package:luckybox/model.dart';
import 'package:luckybox/audio_play.dart';
import 'package:luckybox/text_to_speech.dart';
import 'package:luckybox/frame_painter.dart';
import 'package:luckybox/main.dart';
import 'package:luckybox/loading_screen.dart';

class MainHomePage extends StatefulWidget {
  const MainHomePage({super.key});
  @override
  State<MainHomePage> createState() => _MainHomePageState();
}

class _MainHomePageState extends State<MainHomePage> with SingleTickerProviderStateMixin, WidgetsBindingObserver {
  late List<ui.Image> _boxDecodedFrames = [];
  late List<ui.Image> _ticketDecodedFrames = [];
  final GlobalKey _aspectRatioKey = GlobalKey(); //_textAreaのサイズ取得用
  late AdManager _adManager;
  late ThemeColor _themeColor;
  final AudioPlay _audioPlay = AudioPlay(); //効果音
  bool _isReady = false;
  //lottery
  bool _isBusy = false; //動作中
  int _tickNumber = 0; //0~119で画像と結果表示を切り替え
  String _ticketText = ''; //結果 e.g. 'はずれ'
  double _ticketTextSize = 15.0; //結果の文字サイズ。_tickNumberで変化
  late Timer _timer; //カウントダウンと画像切り替えで使用
  //countdown
  int _countdownSubtraction = 0; //_countdownTimeが代入されて実際にカウントダウンされる
  String _imageCountdownNumber = ConstValue.imageNumbers[0]; //カウントダウン画像
  double _countdownScale = 3; //カウントダウン画像の拡大率
  double _countdownOpacity = 0; //カウントダウン画像の非透明度
  int _timerCount = 30; //Timerで処理される1秒間の数

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    _initState();
  }

  void _initState() async {
    _adManager = AdManager();
    _audioPlay.playZero();
    await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
    _boxDecodedFrames = await boxLoadAllFrames();
    _ticketDecodedFrames = await ticketLoadAllFrames();
    _wakelock();
    if (mounted) {
      setState(() {
        _isReady = true;
      });
    }
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    WakelockPlus.disable();
    _adManager.dispose();
    TextToSpeech.stop();
    _timer.cancel();
    super.dispose();
  }

  //need with WidgetsBindingObserver
  //WidgetsBinding.instance.addObserver(this);
  //WidgetsBinding.instance.removeObserver(this);
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        _wakelock();
        break;
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
        WakelockPlus.disable();
        break;
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
  }

  void _wakelock() {
    if (Model.wakelockEnabled) {
      WakelockPlus.enable();
    } else {
      WakelockPlus.disable();
    }
  }

  void _speakResult(String text) async {
    if (Model.ttsEnabled && Model.ttsVolume > 0.0) {
      await TextToSpeech.speak(text);
    }
  }

  Future<List<ui.Image>> boxLoadAllFrames() async {
    final List<ui.Image> decodedFrames = [];
    for (int i = 0; i < ConstValue.imageBoxes.length; i++) {
      final path = ConstValue.imageBoxes[i];
      final data = await rootBundle.load(path);
      final codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
      final frame = await codec.getNextFrame();
      decodedFrames.add(frame.image);
    }
    return decodedFrames;
  }

  Future<List<ui.Image>> ticketLoadAllFrames() async {
    final List<ui.Image> decodedFrames = [];
    for (int i = 0; i < ConstValue.imageTickets.length; i++) {
      final path = ConstValue.imageTickets[i];
      final data = await rootBundle.load(path);
      final codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
      final frame = await codec.getNextFrame();
      decodedFrames.add(frame.image);
    }
    return decodedFrames;
  }

  Future<void> _openSetting() async {
    final updated = await Navigator.push<bool>(
      context,
      MaterialPageRoute(builder: (_) => const SettingPage()),
    );
    if (mounted && updated == true) {
      MainApp.of(context).rebuildApp();
      _wakelock();
      await TextToSpeech.applyPreferences(Model.ttsVoiceId,Model.ttsVolume);
    }
    if (mounted) {
      setState(() {});
    }
  }

  //画面全体
  @override
  Widget build(BuildContext context) {
    if (_isReady == false) {
      return const LoadingScreen();
    }
    final l = AppLocalizations.of(context)!;
    return Container(
      decoration: _decoration(),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          centerTitle: false,
          title: Text(l.appTitle,
            style: TextStyle(color: Colors.white, fontSize: 15.0),
          ),
          //設定ボタン
          actions: <Widget>[
            Opacity(
              opacity: _isBusy ? 0.1 : 1,
              child: IconButton(
                icon: const Icon(Icons.settings,color: Colors.white),
                onPressed: _isBusy ? null : _openSetting,
              ),
            ),
            const SizedBox(width: 10),
          ],
        ),
        body: SafeArea(
          child: GestureDetector(
            onTap: () {
              _onClickStart();
            },
            child: Column(
              children: [
                Expanded(child: LayoutBuilder(builder: (context, c) {
                  final box = min(c.maxWidth, c.maxHeight);
                  final dpr = MediaQuery.of(context).devicePixelRatio;
                  final targetWidthPx = max(1, (box * dpr).round());
                  return Stack(
                    children: [
                      Center(child: _boxFrameImage(_tickNumber, targetWidthPx)),
                      Center(child: _preTextArea()),
                      Center(child: _textArea()),
                      Center(child: _ticketFrameImage(_tickNumber, targetWidthPx)),
                      Center(
                        child: Opacity(
                          opacity: _countdownOpacity,
                          child: Transform.scale(
                            scale: _countdownScale,
                            child: Image.asset(_imageCountdownNumber),
                          ),
                        ),
                      ),
                      SizedBox(
                        width: double.infinity,
                        child: Text(
                          AppLocalizations.of(context)!.start,
                          textAlign: TextAlign.center,
                          style: TextStyle(color: _themeColor.colorNote),
                        ),
                      ),
                    ],
                  );
                })),
              ],
            ),
          )
        ),
        bottomNavigationBar: AdBannerWidget(adManager: _adManager),
      ),
    );
  }

  Decoration _decoration() {
    return BoxDecoration(
      image: DecorationImage(
        image: AssetImage(_themeColor.backImage),
        fit: BoxFit.cover,
      ),
    );
  }

  //カウントダウンタイマー
  void _timerStart() {
    _timer = Timer.periodic(const Duration(milliseconds: (1000 ~/ 30)), (
        timer,
        ) {
      setState(() {
        _countdown();
      });
    });
  }

  //START
  void _onClickStart() {
    if (_isBusy) {
      return;
    }
    _isBusy = true;
    _countdownSubtraction = Model.countdownTime;
    if (_countdownSubtraction == 0) {
      //カウントダウンしない場合
      _tickAction();
    } else {
      _audioPlay.play01();
      _tickNumber = 0;
      _ticketTextSize = 0;
      _timerStart();
    }
  }

  //0~119まで変化。画像と結果表示を切り替え
  void _tickAction() {
    _audioPlay.play02();
    _tickNumber = 0;
    _ticketText = Model.nextPrizeText();
    _timer = Timer.periodic(const Duration(milliseconds: (1000 ~/ 30)), (
        timer,
        ) {
      setState(() {
        _tickNumber += 1;
        if (_tickNumber < 80) {
          _ticketTextSize = 0;
        } else {
          _ticketTextSize = (_tickNumber - 80) / 2 + 5.0;
        }
        if (_tickNumber >= 119) {
          _timer.cancel();
          _speakResult(_ticketText);
          _isBusy = false;
        }
      });
    });
  }

  //Timerで定期実行
  void _countdown() {
    //カウントダウン終了時
    if (_countdownSubtraction == 0) {
      return;
    }
    //数字画像を切り替え
    if (_timerCount == 30) {
      _imageCountdownNumber = ConstValue.imageNumbers[_countdownSubtraction];
    }
    _timerCount -= 1;
    if (_timerCount <= 0) {
      _timerCount = 30;
      _countdownSubtraction -= 1;
      if (_countdownSubtraction == 0) {
        _imageCountdownNumber = ConstValue.imageNumbers[0];
        _timer.cancel();
        _tickAction();
      }
    }
    _countdownScale = 1 + (0.1 * (_timerCount / 30));
    if (_timerCount >= 20) {
      _countdownOpacity = (30 - _timerCount) / 10;
    } else if (_timerCount <= 5) {
      _countdownOpacity = _timerCount / 5;
    } else {
      _countdownOpacity = 1;
    }
  }

  Widget _boxFrameImage(int idx, int targetWidthPx) {
    //念のための安全対策:配列が空、またはインデックスが範囲外なら何も表示しない
    if (_boxDecodedFrames.isEmpty || idx >= _boxDecodedFrames.length) {
      return const SizedBox.shrink();
    }
    return Center(
      child: SizedBox(
        width: targetWidthPx.toDouble(),
        height: targetWidthPx.toDouble(),
        child: CustomPaint(
          painter: FramePainter(_boxDecodedFrames[idx]),
        ),
      ),
    );
  }

  Widget _ticketFrameImage(int idx, int targetWidthPx) {
    if (idx < 80 || idx >= 90) {
      return SizedBox.shrink();
    }
    return Center(
      child: SizedBox(
        width: targetWidthPx.toDouble(),
        height: targetWidthPx.toDouble(),
        child: CustomPaint(
          painter: FramePainter(_ticketDecodedFrames[idx - 80]),
        ),
      ),
    );
  }

  //_textAreaのサイズ取得用
  Widget _preTextArea() {
    return AspectRatio(key: _aspectRatioKey, aspectRatio: 1);
  }

  //結果文字表示
  Widget _textArea() {
    late Size size;
    try {
      RenderBox renderBox =
      _aspectRatioKey.currentContext?.findRenderObject() as RenderBox;
      size = renderBox.size;
    } catch (_) {
      return Container();
    }
    return AspectRatio(
      aspectRatio: 1,
      child: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.fromLTRB(
          0,
          size.width * 0.25,
          size.width * 0.19,
          0,
        ),
        child: Text(_ticketText, style: TextStyle(color: Colors.black, fontSize: _ticketTextSize)),
      ),
    );
  }
}
import 'dart:math';
import 'package:flutter/material.dart';

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

  @override
  State<LoadingScreen> createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    final randomStart = Random().nextDouble();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 6),
      value: randomStart,
    )..repeat();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  Color _rainbowColor(double value) {
    final hue = _animationController.value * 360;
    return HSVColor.fromAHSV(1, hue, 1, value).toColor();
  }

  @override
  Widget build(BuildContext context) {
    final barHeight = MediaQuery.of(context).size.height * 0.4;
    return AnimatedBuilder(
      animation: _animationController,
      builder: (context, _) {
        final foreColor = _rainbowColor(1.0);
        final backColor = _rainbowColor(0.08);
        return Scaffold(
          backgroundColor: backColor,
          body: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(
                  height: barHeight,
                  child: RotatedBox(
                    quarterTurns: -1,
                    child: LinearProgressIndicator(
                      minHeight: 1,
                      valueColor: AlwaysStoppedAnimation(foreColor),
                      backgroundColor: Colors.transparent,
                    ),
                  ),
                ),
                const SizedBox(height: 5),
                Text(
                  'LOADING',
                  style: TextStyle(
                    color: foreColor,
                    fontSize: 18,
                    letterSpacing: 16,
                  ),
                ),
                const SizedBox(height: 5),
                SizedBox(
                  height: barHeight,
                  child: RotatedBox(
                    quarterTurns: 1,
                    child: LinearProgressIndicator(
                      minHeight: 1,
                      valueColor: AlwaysStoppedAnimation(foreColor),
                      backgroundColor: Colors.transparent,
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
import 'dart:io';
import 'package:app_tracking_transparency/app_tracking_transparency.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'package:luckybox/l10n/app_localizations.dart';
import 'package:luckybox/model.dart';
import 'package:luckybox/theme_mode_number.dart';
import 'package:luckybox/parse_locale_tag.dart';
import 'package:luckybox/home_page.dart';
import 'package:luckybox/loading_screen.dart';
import 'package:luckybox/ad_manager.dart';
import 'package:luckybox/ad_ump_status.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  //ATTを最優先で呼ぶ(広告SDKより前)
  if (!kIsWeb && Platform.isIOS) {
    final status = await AppTrackingTransparency.trackingAuthorizationStatus;
    if (status == TrackingStatus.notDetermined) {
      await Future.delayed(const Duration(milliseconds: 300));
      await AppTrackingTransparency.requestTrackingAuthorization();
    }
  }
  //UI設定
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      systemNavigationBarColor: Colors.transparent,
      statusBarColor: Colors.transparent,
      systemNavigationBarContrastEnforced: false,
      systemStatusBarContrastEnforced: false,
    ),
  );
  runApp(const MainApp());
}

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

  static MainAppState of(BuildContext context) {
    return context.findAncestorStateOfType<MainAppState>()!;
  }

  @override
  State<MainApp> createState() => MainAppState();
}

class MainAppState extends State<MainApp> {
  ThemeMode _themeMode = ThemeMode.system;
  Locale? _locale;
  bool _hasError = false;
  bool _isReady = false;

  @override
  void initState() {
    super.initState();
    _initState();
  }

  void _initState() async {
    try {
      //アプリの基本データ
      await Model.ensureReady();
      //UMP(ATTの後)
      final adUmpConsentController = AdUmpConsentController();
      await adUmpConsentController.updateConsentInfo();
      //Mobile Ads SDK(同意確定後)
      await MobileAds.instance.initialize();
      //自前の広告設定
      await AdManager.initForNPA();
      //UI更新
      if (mounted) {
        setState(() {
          _themeMode = ThemeModeNumber.numberToThemeMode(Model.themeNumber);
          _locale = parseLocaleTag(Model.languageCode);
          _isReady = true;
        });
      }
    } catch (e) {
      if (mounted) {
        setState(() {
          _hasError = true;
        });
      }
    }
  }

  void rebuildApp() {
    setState(() {
      _themeMode = ThemeModeNumber.numberToThemeMode(Model.themeNumber);
      _locale = parseLocaleTag(Model.languageCode);
    });
  }

  ThemeData _createTheme(Brightness brightness, Color seed) {
    final colorScheme = ColorScheme.fromSeed(seedColor: seed, brightness: brightness);
    return ThemeData(
      useMaterial3: true,
      colorScheme: colorScheme,
      appBarTheme: const AppBarTheme(backgroundColor: Colors.transparent),
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ElevatedButton.styleFrom(
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        ),
      ),
      sliderTheme: SliderThemeData(
        showValueIndicator: ShowValueIndicator.onDrag,
        valueIndicatorTextStyle: TextStyle(
          color: brightness == Brightness.light ? Colors.white : Colors.black,
        ),
      ),
      outlinedButtonTheme: OutlinedButtonThemeData(
        style: OutlinedButton.styleFrom(
          side: BorderSide(color: colorScheme.primary),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (_hasError) {
      return _buildErrorMessage();
    }
    Color seed = Colors.orange;
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      locale: _locale,
      themeMode: _themeMode,
      theme: _createTheme(Brightness.light, seed),
      darkTheme: _createTheme(Brightness.dark, seed),
      home: _isReady ? const MainHomePage() : const Scaffold(body: LoadingScreen()),
    );
  }

  Widget _buildErrorMessage() {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Padding(
            padding: EdgeInsets.all(24.0),
            child: Text(
              'Initialization failed. Please restart the app.',
              textAlign: TextAlign.center,
            ),
          ),
        ),
      ),
    );
  }

}
import 'dart:ui' as ui;
import 'package:shared_preferences/shared_preferences.dart';

import 'package:luckybox/const_value.dart';
import 'package:luckybox/l10n/app_localizations.dart';

class Model {
  Model._();

  static const String _prefPrizeText = 'prizeText';
  static const String _prefCountdownTime = 'countdownTime';
  static const String _prefSoundReadyVolume = 'soundReadyVolume';
  static const String _prefSoundStartVolume = 'soundStartVolume';
  static const String _prefTtsEnabled = 'ttsEnabled';
  static const String _prefTtsVoiceId = 'ttsVoiceId';
  static const String _prefTtsVolume = 'ttsVolume';
  static const String _prefWakelockEnabled = 'wakelockEnabled';
  static const String _prefThemeNumber = 'themeNumber';
  static const String _prefLanguageCode = 'languageCode';
  
  static bool _ready = false;
  static String _prizeText = '';
  static List<Map<String, dynamic>> _prizeList = [];
  static int _countdownTime = 0;
  static double _soundReadyVolume = 0.5;
  static double _soundStartVolume = 0.5;
  static bool _ttsEnabled = true;
  static double _ttsVolume = 1.0;
  static String _ttsVoiceId = '';
  static bool _wakelockEnabled = false;
  static int _themeNumber = 0;
  static String _languageCode = '';

  static String get prizeText => _prizeText;
  static List<Map<String, dynamic>> get prizeList => _prizeList;
  static int get countdownTime => _countdownTime;
  static double get soundReadyVolume => _soundReadyVolume;
  static double get soundStartVolume => _soundStartVolume;
  static bool get ttsEnabled => _ttsEnabled;
  static double get ttsVolume => _ttsVolume;
  static String get ttsVoiceId => _ttsVoiceId;
  static bool get wakelockEnabled => _wakelockEnabled;
  static int get themeNumber => _themeNumber;
  static String get languageCode => _languageCode;

  static Future<void> ensureReady() async {
    if (_ready) {
      return;
    }
    final prefs = await SharedPreferences.getInstance();
    //
    _prizeText = prefs.getString(_prefPrizeText) ?? '';
    _prizeList = _makePrizeList(_prizeText);
    if (_prizeText == '') {
      await setPrizeText(''); //空文字で初期値をセット
    }
    _countdownTime = (prefs.getInt(_prefCountdownTime) ?? 0).clamp(0, 9);
    _soundReadyVolume = (prefs.getDouble(_prefSoundReadyVolume) ?? 0.5).clamp(0.0, 1.0);
    _soundStartVolume = (prefs.getDouble(_prefSoundStartVolume) ?? 0.5).clamp(0.0, 1.0);
    _ttsEnabled = prefs.getBool(_prefTtsEnabled) ?? true;
    _ttsVolume = (prefs.getDouble(_prefTtsVolume) ?? 1.0).clamp(0.0, 1.0);
    _ttsVoiceId = prefs.getString(_prefTtsVoiceId) ?? '';
    _wakelockEnabled = prefs.getBool(_prefWakelockEnabled) ?? false;
    _themeNumber = (prefs.getInt(_prefThemeNumber) ?? 0).clamp(0, 2);
    _languageCode = prefs.getString(_prefLanguageCode) ?? ui.PlatformDispatcher.instance.locale.languageCode;
    _languageCode = _resolveLanguageCode(_languageCode);
    _ready = true;
  }

  static String _resolveLanguageCode(String code) {
    final supported = AppLocalizations.supportedLocales;
    if (supported.any((l) => l.languageCode == code)) {
      return code;
    } else {
      return '';
    }
  }

  //賞
  static Future<void> setPrizeText(String value) async {
    if (value == '') {
      value = ConstValue.prizeTextDefault;
    }
    _prizeText = _prizeFormat(value);
    _prizeList = _makePrizeList(_prizeText);
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefPrizeText, _prizeText);
  }

  static Future<void> setPrizeTextDefault() async {
    setPrizeText(ConstValue.prizeTextDefault);
  }

  //賞をListにする
  static List<Map<String, dynamic>> _makePrizeList(String value) {
    List<Map<String, dynamic>> mapList = [];
    if (value == '') {
      return mapList;
    }
    final List<String> lines = value.replaceAll('\r', '').split('\n');
    for (int i = 0; i < lines.length; i++) {
      final List<String> ary = lines[i].split(':');
      final int number = _parseStrToNumber(ary[0]);
      final Map<String, dynamic> mapOne = {'number': number, 'prize': ary[1]};
      mapList.add(mapOne);
    }
    return mapList;
  }

  //賞を1個取り出す
  static String nextPrizeText() {
    final List<int> candidates = [];
    int row = 0;
    for (Map<String, dynamic> prize in _prizeList) {
      for (int i = 0; i < prize['number']; i++) {
        candidates.add(row);
      }
      row += 1;
    }
    if (candidates.isEmpty) {
      return '* END *';
    }
    candidates.shuffle();
    final int choice = candidates.first;
    String result = '';
    for (int i = 0; i < _prizeList.length; i++) {
      if (i == choice) {
        _prizeList[i]['number'] -= 1;
        result = _prizeList[i]['prize'];
        break;
      }
    }
    List<String> prizeStringList = [];
    for (Map<String, dynamic> prize in _prizeList) {
      prizeStringList.add('${prize['number']}:${prize['prize']}');
    }
    setPrizeText(prizeStringList.join('\n'));
    return result;
  }

  //カウントダウン時間
  static Future<void> setCountdownTime(int value) async {
    _countdownTime = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefCountdownTime, value);
  }

  //効果音音量
  static Future<void> setSoundReadyVolume(double value) async {
    _soundReadyVolume = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefSoundReadyVolume, value);
  }

  //効果音音量
  static Future<void> setSoundStartVolume(double value) async {
    _soundStartVolume = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefSoundStartVolume, value);
  }

  static Future<void> setTtsEnabled(bool value) async {
    _ttsEnabled = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefTtsEnabled, value);
  }

  static Future<void> setTtsVolume(double value) async {
    _ttsVolume = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefTtsVolume, value);
  }

  static Future<void> setTtsVoiceId(String value) async {
    _ttsVoiceId = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefTtsVoiceId, value);
  }

  static Future<void> setWakelockEnabled(bool value) async {
    _wakelockEnabled = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefWakelockEnabled, value);
  }

  static Future<void> setThemeNumber(int value) async {
    _themeNumber = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefThemeNumber, value);
  }

  static Future<void> setLanguageCode(String value) async {
    _languageCode = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefLanguageCode, value);
  }

  //文字列を数値に変換
  static int _parseStrToNumber(String numString) {
    if (_isStringToIntParsable(numString)) {
      return int.parse(numString);
    }
    return 0;
  }

  //String を int に変換できるか
  static bool _isStringToIntParsable(String str) {
    try {
      int.parse(str);
      return true;
    } catch (e) {
      return false;
    }
  }

  //賞を整える。ユーザーの入力なので適宜調整する
  static String _prizeFormat(String str) {
    final List<String> lines = str.replaceAll('\r', '').split('\n');
    List<String> prizes = [];
    for (String str in lines) {
      str = str.replaceAll(':', ':');
      if (str.contains(':') == false) {
        continue;
      }
      List<String> ary = str.split(':');
      ary[0] = ary[0].replaceAll('0', '0');
      ary[0] = ary[0].replaceAll('1', '1');
      ary[0] = ary[0].replaceAll('2', '2');
      ary[0] = ary[0].replaceAll('3', '3');
      ary[0] = ary[0].replaceAll('4', '4');
      ary[0] = ary[0].replaceAll('5', '5');
      ary[0] = ary[0].replaceAll('6', '6');
      ary[0] = ary[0].replaceAll('7', '7');
      ary[0] = ary[0].replaceAll('8', '8');
      ary[0] = ary[0].replaceAll('9', '9');
      ary[0] = ary[0].replaceAll('、', ',');
      ary[0] = ary[0].replaceAll(',', ',');
      ary[0] = ary[0].replaceAll('ー', '-');
      ary[0] = ary[0].replaceAll('―', '-');
      ary[0] = ary[0].replaceAll(RegExp(r'[^0-9]'), '');
      prizes.add('${ary[0]}:${ary[1]}');
    }
    return prizes.join('\n');
  }

}
import 'dart:ui';

Locale? parseLocaleTag(String tag) {
  if (tag.isEmpty) {
    return null;
  }
  final parts = tag.split('-');
  final language = parts[0];
  String? script, country;
  if (parts.length >= 2) {
    parts[1].length == 4 ? script = parts[1] : country = parts[1];
  }
  if (parts.length >= 3) {
    parts[2].length == 4 ? script = parts[2] : country = parts[2];
  }
  return Locale.fromSubtags(
    languageCode: language,
    scriptCode: script,
    countryCode: country,
  );
}
import 'package:flutter/material.dart';

import 'package:luckybox/theme_color.dart';
import 'package:luckybox/model.dart';

/// 設定画面専用のカスタムCardウィジェット
class SettingCard extends StatelessWidget {
  final Widget child;
  final ShapeBorder shape;
  final EdgeInsetsGeometry margin;

  const SettingCard({
    super.key,
    required this.child,
    this.margin = const EdgeInsets.only(left: 0, top: 12, right: 0, bottom: 0),
  }) : shape = const RoundedRectangleBorder(
    borderRadius: BorderRadius.all(Radius.circular(12)),
  );

  const SettingCard.top({
    super.key,
    required this.child,
    this.margin = const EdgeInsets.only(left: 0, top: 12, right: 0, bottom: 0),
  }) : shape = const RoundedRectangleBorder(
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(12),
      topRight: Radius.circular(12),
      bottomLeft: Radius.circular(0),
      bottomRight: Radius.circular(0),
    ),
  );

  const SettingCard.flat({
    super.key,
    required this.child,
    this.margin = const EdgeInsets.only(left: 0, top: 2, right: 0, bottom: 0),
  }) : shape = const RoundedRectangleBorder(
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(0),
      topRight: Radius.circular(0),
      bottomLeft: Radius.circular(0),
      bottomRight: Radius.circular(0),
    ),
  );

  const SettingCard.bottom({
    super.key,
    required this.child,
    this.margin = const EdgeInsets.only(left: 0, top: 2, right: 0, bottom: 0),
  }) : shape = const RoundedRectangleBorder(
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(0),
      topRight: Radius.circular(0),
      bottomLeft: Radius.circular(12),
      bottomRight: Radius.circular(12),
    ),
  );

  @override
  Widget build(BuildContext context) {
    final themeColor = ThemeColor(
      themeNumber: Model.themeNumber,
      context: context,
    );
    return SizedBox(
      width: double.infinity,
      child: Card(
        elevation: 0,
        margin: margin,
        surfaceTintColor: Colors.transparent,
        shadowColor: Colors.transparent,
        color: themeColor.cardColor,
        shape: shape,
        child: child,
      ),
    );
  }
}
import "dart:async";
import "dart:io";
import "package:app_settings/app_settings.dart";
import "package:app_tracking_transparency/app_tracking_transparency.dart";
import 'package:flutter/material.dart';
import "package:flutter/foundation.dart";
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:in_app_review/in_app_review.dart';

import "package:luckybox/setting_card.dart";
import 'package:luckybox/l10n/app_localizations.dart';
import 'package:luckybox/model.dart';
import 'package:luckybox/text_to_speech.dart';
import 'package:luckybox/theme_color.dart';
import 'package:luckybox/ad_manager.dart';
import 'package:luckybox/ad_banner_widget.dart';
import 'package:luckybox/ad_ump_status.dart';
import 'package:luckybox/_secrets.dart';


class SettingPage extends StatefulWidget {
  const SettingPage({super.key});
  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  late final AdManager _adManager;
  AdUmpState _adUmpState = AdUmpState.initial;
  late final AdUmpService _adUmpService;
  late ThemeColor _themeColor;
  final _inAppReview = InAppReview.instance;
  final TextEditingController _controllerPrizeText = TextEditingController();
  bool _prizeInitialFlag = false;
  int _countdownTime = 0;
  double _soundReadyVolume = 0.5;
  double _soundStartVolume = 0.5;
  late List<TtsOption> _ttsVoices;
  bool _ttsEnabled = true;
  String _ttsVoiceId = '';
  double _ttsVolume = 1.0;
  bool _wakelockEnabled = true;
  int _themeNumber = 0;
  String _languageCode = '';
  bool _isReady = false;


  @override
  void initState() {
    super.initState();
    _initState();
  }

  @override
  void dispose() {
    _adManager.dispose();
    _controllerPrizeText.dispose();
    super.dispose();
  }

  void _initState() async {
    //ump
    _adUmpService = AdUmpService();
    _refreshConsentInfo();
    //ad
    _adManager = AdManager();
    //model
    _controllerPrizeText.text = Model.prizeText;
    _countdownTime = Model.countdownTime;
    _soundReadyVolume = Model.soundReadyVolume;
    _soundStartVolume = Model.soundStartVolume;
    _ttsEnabled = Model.ttsEnabled;
    _ttsVoiceId = Model.ttsVoiceId;
    _ttsVolume = Model.ttsVolume;
    _wakelockEnabled = Model.wakelockEnabled;
    _themeNumber = Model.themeNumber;
    _languageCode = Model.languageCode;
    //speech
    await TextToSpeech.getInstance();
    _ttsVoices = TextToSpeech.ttsVoices;
    TextToSpeech.setVolume(_ttsVolume);
    TextToSpeech.setTtsVoiceId(_ttsVoiceId);
    //
    setState(() {
      _isReady = true;
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
  }

  Future<void> _refreshConsentInfo() async {
    final AdUmpState newState = await _adUmpService.updateConsentInfo(_adUmpState);
    if (mounted) {
      setState(() { _adUmpState = newState; });
    }
  }

  void _apply() async {
    FocusScope.of(context).unfocus();
    if (_prizeInitialFlag) {
      await Model.setPrizeTextDefault();
    } else {
      await Model.setPrizeText(_controllerPrizeText.text);
    }
    await Model.setCountdownTime(_countdownTime);
    await Model.setSoundReadyVolume(_soundReadyVolume);
    await Model.setSoundStartVolume(_soundStartVolume);
    await Model.setTtsEnabled(_ttsEnabled);
    await Model.setTtsVoiceId(_ttsVoiceId);
    await Model.setTtsVolume(_ttsVolume);
    await Model.setWakelockEnabled(_wakelockEnabled);
    await Model.setThemeNumber(_themeNumber);
    await Model.setLanguageCode(_languageCode);
    if (!mounted) {
      return;
    }
    Navigator.of(context).pop(true);
  }

  //ページ描画
  @override
  Widget build(BuildContext context) {
    if (!_isReady) {
      return _buildLoadingView();
    }
    final l = AppLocalizations.of(context)!;
    final TextTheme t = Theme.of(context).textTheme;
    return Scaffold(
      backgroundColor: _themeColor.backColor,
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Colors.transparent,
        leading: IconButton(
          icon: const Icon(Icons.close),
          onPressed: () => Navigator.of(context).pop(),
          style: IconButton.styleFrom(foregroundColor: _themeColor.appBarForegroundColor),
        ),
        actions: <Widget>[
          Padding(
            padding: const EdgeInsets.only(right: 10),
            child: IconButton(
              icon: const Icon(Icons.check),
              onPressed: _apply,
              style: IconButton.styleFrom(foregroundColor: _themeColor.appBarForegroundColor),
            ),
          ),
        ],
      ),
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: GestureDetector(
                onTap: () => FocusScope.of(context).unfocus(), //背景タップでキーボードを仕舞う
                child: SingleChildScrollView(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 12, right: 12, top: 0, bottom: 100),
                    child: Column(
                      children: [
                        _buildPrize(l, t),
                        _buildCountdown(l, t),
                        _buildSoundVolume(l, t),
                        _buildSpeechSettings(l, t),
                        _buildWakelockEnabled(l, t),
                        _buildTheme(l, t),
                        _buildLanguage(l, t),
                        _buildReview(l, t),
                        _buildCmp(l, t),
                        _buildAtt(l, t),
                        _buildUsage(l, t),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ],
        )
      ),
      bottomNavigationBar: AdBannerWidget(adManager: _adManager),
    );
  }

  Widget _buildLoadingView() {
    return Container(
      color: Colors.yellow[700],
      child: Center(
        child: CircularProgressIndicator(
          valueColor: const AlwaysStoppedAnimation<Color>(Colors.white),
          backgroundColor: Colors.yellow[300],
        ),
      ),
    );
  }

  Widget _buildPrize(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: Column(
        children: [
          ListTile(
            contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            title: Text(l.prize, style: t.bodyMedium),
            trailing: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(l.initial, style: t.bodyMedium),
                Switch(
                  value: _prizeInitialFlag,
                  onChanged: (value) {
                    setState(() {
                      _prizeInitialFlag = value;
                    });
                  },
                  activeThumbColor: Colors.red,
                ),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
            child: TextField(
              controller: _controllerPrizeText,
              maxLines: null,
              decoration: const InputDecoration(
                border: OutlineInputBorder(),
              ),
            ),
          ),
        ],
      ),
    );
  }


  Widget _buildCountdown(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        title: Text(l.countdownTime, style: t.bodyMedium),
        subtitle: Row(
          children: [
            Text(_countdownTime.toString()),
            Expanded(
              child: Slider(
                value: _countdownTime.toDouble(),
                min: 0,
                max: 9,
                divisions: 9,
                label: _countdownTime.toString(),
                onChanged: (value) {
                  setState(() {
                    _countdownTime = value.toInt();
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSoundVolume(AppLocalizations l, TextTheme t) {
    return Column(children:[
      SettingCard.top(
        child: Column(
          children: [
            ListTile(
              contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
              title: Text(l.soundReadyVolume, style: t.bodyMedium),
              subtitle: Row(
                children: [
                  Text(_soundReadyVolume.toStringAsFixed(1)),
                  Expanded(
                    child: Slider(
                      value: _soundReadyVolume,
                      min: 0.0,
                      max: 1.0,
                      divisions: 10,
                      label: _soundReadyVolume.toStringAsFixed(1),
                      onChanged: (value) {
                        setState(() {
                          _soundReadyVolume = value;
                        });
                      },
                    ),
                  ),
                ],
              ),
            )
          ]
        )
      ),
      SettingCard.bottom(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          title: Text(l.soundStartVolume, style: t.bodyMedium),
          subtitle: Row(
            children: [
              Text(_soundStartVolume.toStringAsFixed(1)),
              Expanded(
                child: Slider(
                  value: _soundStartVolume,
                  min: 0.0,
                  max: 1.0,
                  divisions: 10,
                  label: _soundStartVolume.toStringAsFixed(1),
                  onChanged: (value) {
                    setState(() {
                      _soundStartVolume = value;
                    });
                  },
                ),
              ),
            ],
          ),
        )
      ),
    ]);
  }

  Widget _buildSpeechSettings(AppLocalizations l, TextTheme t) {
    if (_ttsVoices.isEmpty) {
      return const SizedBox.shrink();
    }
    return Column(children:[
      SettingCard.top(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(l.ttsEnabled, style: t.bodyMedium),
          trailing: Switch(
            value: _ttsEnabled,
            onChanged: (value) {
              setState(() {
                _ttsEnabled = value;
              });
            },
          ),
        ),
      ),
      SettingCard.flat(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(l.ttsVolume, style: t.bodyMedium),
          subtitle: Row(
            children: [
              Text(_ttsVolume.toStringAsFixed(1)),
              Expanded(
                child: Slider(
                  value: _ttsVolume,
                  min: 0.0,
                  max: 1.0,
                  divisions: 10,
                  label: _ttsVolume.toStringAsFixed(1),
                  onChanged: _ttsEnabled
                      ? (value) {
                    setState(() {
                      _ttsVolume = double.parse(value.toStringAsFixed(1));
                    });
                  }
                      : null,
                ),
              ),
            ],
          ),
        ),
      ),
      SettingCard.bottom(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(l.ttsVoiceId, style: t.bodyMedium),
          trailing: DropdownButton<String>(
            dropdownColor: _themeColor.dropdownColor,
            value: () {
              if (_ttsVoiceId.isNotEmpty &&
                  _ttsVoices.any((o) => o.id == _ttsVoiceId)) {
                return _ttsVoiceId;
              }
              return _ttsVoices.first.id;
            }(),
            items: _ttsVoices
                .map((o) => DropdownMenuItem<String>(
              value: o.id,
              child: Text(o.label),
            ))
                .toList(),
            onChanged: (v) {
              if (v == null) return;
              setState(() => _ttsVoiceId = v);
            },
          ),
        ),
      ),
    ]);
  }

  Widget _buildWakelockEnabled(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.wakelockEnabled, style: t.bodyMedium),
        trailing: Switch(
          value: _wakelockEnabled,
          onChanged: (value) {
            setState(() {
              _wakelockEnabled = value;
            });
          },
        ),
      ),
    );
  }

  Widget _buildTheme(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        minVerticalPadding: 0,
        title: Text(l.theme, style: t.bodyMedium),
        trailing: DropdownButton<int>(
          value: _themeNumber,
          items: [
            DropdownMenuItem(value: 0, child: Text(l.systemSetting)),
            DropdownMenuItem(value: 1, child: Text(l.lightTheme)),
            DropdownMenuItem(value: 2, child: Text(l.darkTheme)),
          ],
          onChanged: (value) {
            if (value != null) {
              setState(() {
                _themeNumber = value;
              });
            }
          },
        ),
      ),
    );
  }

  Widget _buildLanguage(AppLocalizations l, TextTheme t) {
    final Map<String,String> languageNames = {
      'af': 'af: Afrikaans',
      'ar': 'ar: العربية',
      'bg': 'bg: Български',
      'bn': 'bn: বাংলা',
      'bs': 'bs: Bosanski',
      'ca': 'ca: Català',
      'cs': 'cs: Čeština',
      'da': 'da: Dansk',
      'de': 'de: Deutsch',
      'el': 'el: Ελληνικά',
      'en': 'en: English',
      'es': 'es: Español',
      'et': 'et: Eesti',
      'fa': 'fa: فارسی',
      'fi': 'fi: Suomi',
      'fil': 'fil: Filipino',
      'fr': 'fr: Français',
      'gu': 'gu: ગુજરાતી',
      'he': 'he: עברית',
      'hi': 'hi: हिन्दी',
      'hr': 'hr: Hrvatski',
      'hu': 'hu: Magyar',
      'id': 'id: Bahasa Indonesia',
      'it': 'it: Italiano',
      'ja': 'ja: 日本語',
      //'jv': 'jv: Basa Jawa',    //flutterのサポート外
      'km': 'km: ខ្មែរ',
      'kn': 'kn: ಕನ್ನಡ',
      'ko': 'ko: 한국어',
      'lt': 'lt: Lietuvių',
      'lv': 'lv: Latviešu',
      'ml': 'ml: മലയാളം',
      'mr': 'mr: मराठी',
      'ms': 'ms: Bahasa Melayu',
      'my': 'my: မြန်မာ',
      'ne': 'ne: नेपाली',
      'nl': 'nl: Nederlands',
      'or': 'or: ଓଡ଼ିଆ',
      'pa': 'pa: ਪੰਜਾਬੀ',
      'pl': 'pl: Polski',
      'pt': 'pt: Português',
      'ro': 'ro: Română',
      'ru': 'ru: Русский',
      'si': 'si: සිංහල',
      'sk': 'sk: Slovenčina',
      'sr': 'sr: Српски',
      'sv': 'sv: Svenska',
      'sw': 'sw: Kiswahili',
      'ta': 'ta: தமிழ்',
      'te': 'te: తెలుగు',
      'th': 'th: ไทย',
      'tl': 'tl: Tagalog',
      'tr': 'tr: Türkçe',
      'uk': 'uk: Українська',
      'ur': 'ur: اردو',
      'uz': 'uz: Oʻzbekcha',
      'vi': 'vi: Tiếng Việt',
      'zh': 'zh: 中文',
      'zu': 'zu: isiZulu',
    };
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        minVerticalPadding: 0,
        title: Text(l.language, style: t.bodyMedium),
        trailing: DropdownButton<String?>(
          value: _languageCode,
          items: [
            DropdownMenuItem(value: '', child: Text('Default')),
            ...languageNames.entries.map((entry) => DropdownMenuItem<String?>(
              value: entry.key,
              child: Text(entry.value),
            )),
          ],
          onChanged: (String? value) {
            setState(() {
              _languageCode = value ?? '';
            });
          },
        ),
      ),
    );
  }

  Widget _buildReview(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.reviewApp, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SizedBox(height: 8),
            OutlinedButton.icon(
              icon: const Icon(Icons.open_in_new, size: 16),
              label: Text(l.reviewStore, style: t.bodySmall),
              onPressed: () async {
                await _inAppReview.openStoreListing(
                  appStoreId: Secrets.appStoreId,
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildCmp(AppLocalizations l, TextTheme t) {
    final showButton = _adUmpState.privacyStatus == PrivacyOptionsRequirementStatus.required;
    String statusLabel = l.cmpCheckingRegion;
    IconData statusIcon = Icons.help_outline;
    switch (_adUmpState.privacyStatus) {
      case PrivacyOptionsRequirementStatus.required:
        statusLabel = l.cmpRegionRequiresSettings;
        statusIcon = Icons.privacy_tip_outlined;
        break;
      case PrivacyOptionsRequirementStatus.notRequired:
        statusLabel = l.cmpRegionNoSettingsRequired;
        statusIcon = Icons.check_circle_outline;
        break;
      case PrivacyOptionsRequirementStatus.unknown:
        statusLabel = l.cmpRegionCheckFailed;
        statusIcon = Icons.error_outline;
        break;
    }
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.cmpSettingsTitle, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 8),
            Text(l.cmpConsentDescription, style: t.bodySmall),
            const SizedBox(height: 16),
            Center(
              child: Column(
                children: [
                  Chip(
                    avatar: Icon(statusIcon, size: 18),
                    label: Text(statusLabel),
                  ),
                  const SizedBox(height: 6),
                  Text(
                    '${l.cmpConsentStatusLabel} ${_adUmpState.consentStatus.localized(context)}',
                    style: t.bodySmall,
                  ),
                  if (_adUmpState.consentStatus == ConsentStatus.obtained) ...[
                    const SizedBox(height: 6),
                    Text(l.cmpConsentStatusObtainedNote, style: t.bodySmall),
                  ],
                  if (showButton) ...[
                    const SizedBox(height: 8),
                    ElevatedButton.icon(
                      onPressed: _adUmpState.isChecking
                          ? null
                          : () async {
                        try {
                          await _adUmpService.showPrivacyOptions();
                        } catch (e) {
                          //debugPrint('Privacy options error ignored: $e');
                        }
                        await _refreshConsentInfo();
                      },
                      icon: const Icon(Icons.settings),
                      label: Text(
                        _adUmpState.isChecking
                            ? l.cmpConsentStatusChecking
                            : l.cmpOpenConsentSettings,
                      ),
                    ),
                    const SizedBox(height: 8),
                    OutlinedButton.icon(
                      onPressed: _adUmpState.isChecking ? null : _refreshConsentInfo,
                      icon: const Icon(Icons.refresh),
                      label: Text(l.cmpRefreshStatus),
                    ),
                    const SizedBox(height: 8),
                    OutlinedButton.icon(
                      onPressed: () async {
                        final messenger = ScaffoldMessenger.of(context);
                        final message = l.cmpResetStatusDone;
                        await ConsentInformation.instance.reset();
                        if (!mounted) {
                          return;
                        }
                        setState(() {
                          _adUmpState = _adUmpState.copyWith(
                            consentStatus: ConsentStatus.unknown,
                          );
                        });
                        messenger.showSnackBar(SnackBar(content: Text(message)));
                      },
                      icon: const Icon(Icons.delete_sweep_outlined),
                      label: Text(l.cmpResetStatus),
                    ),
                  ],
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildAtt(AppLocalizations l, TextTheme t) {
    if (kIsWeb || !Platform.isIOS) {
      return const SizedBox.shrink();
    }
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(l.attSettingsTitle, style: t.bodyMedium),
        subtitle: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 8),
            Text(l.attDescription, style: t.bodySmall),
            const SizedBox(height: 8),
            FutureBuilder<TrackingStatus>(
              future: AppTrackingTransparency.trackingAuthorizationStatus,
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Column(
                    children: [
                      Chip(
                        avatar: const Icon(Icons.hourglass_empty),
                        label: Text(l.attStatusChecking),
                      ),
                      const SizedBox(height: 8),
                      OutlinedButton.icon(
                        onPressed: null,
                        icon: const Icon(Icons.open_in_new),
                        label: Text(l.attOpenSettings),
                      ),
                    ],
                  );
                }
                final status = snapshot.data;
                final label = status != null
                    ? status.toString().split('.').last
                    : l.attStatusUnknown;
                return Center(
                  child: Column(
                    children: [
                      Chip(
                        avatar: const Icon(Icons.track_changes),
                        label: Text('${l.attStatusLabel} $label'),
                      ),
                      const SizedBox(height: 8),
                      OutlinedButton.icon(
                        onPressed: () => AppSettings.openAppSettings(),
                        icon: const Icon(Icons.open_in_new, size: 16),
                        label: Text(l.attOpenSettings, style: t.bodySmall),
                      ),
                    ],
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildUsage(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(l.usage1, style: t.bodySmall),
            const SizedBox(height: 16),
            Text(l.usage2, style: t.bodySmall),
            const SizedBox(height: 16),
            Text(l.usage3, style: t.bodySmall),
            const SizedBox(height: 16),
            Text(l.usage4, style: t.bodySmall),
          ],
        ),
      ),
    );
  }

}
/*
void _initState() async {
  await TextToSpeech.applyPreferences(Preferences.ttsVoiceId,Preferences.ttsVolume);
}
@override
void dispose() {
  TextToSpeech.stop();
  super.dispose();
}

void any() {
  await TextToSpeech.speak(text);
}

void _onClickSetting() async {
  final updatedSettings = await Navigator.push(
    context,MaterialPageRoute(builder: (context) => SettingPage()),
  );
  if (updatedSettings != null) {
    await TextToSpeech.applyPreferences(Preferences.ttsVoiceId,Preferences.ttsVolume);
  }
}
*/

import 'package:flutter_tts/flutter_tts.dart';
import 'dart:io' show Platform;
import 'package:collection/collection.dart';

class TtsOption {
  final String locale;
  final String name;
  const TtsOption(this.locale, this.name);
  String get id => '$locale|$name';
  String get label => '$locale $name';
}

class TextToSpeech {
  static late FlutterTts _tts;
  static final List<TtsOption> ttsVoices = [];
  static String ttsVoiceId = '';

  static TextToSpeech? _instance;
  static bool _initialized = false;

  TextToSpeech._internal();

  static Future<TextToSpeech> getInstance() async {
    _instance ??= TextToSpeech._internal();
    if (!_initialized) {
      await _instance!._initial();
      _initialized = true;
    }
    return _instance!;
  }

  //声リスト作成
  Future<void> _initial() async {
    _tts = FlutterTts();
    try {
      List<dynamic>? vs;
      for (int i = 0; i < 10; i++) {
        vs = await _tts.getVoices;
        if (vs != null) {
          break;
        }
        await Future.delayed(Duration(seconds: 1));
      }
      if (vs is List) {
        ttsVoices.clear();
        for (final v in vs) {
          if (v is Map && v['name'] is String && v['locale'] is String) {
            ttsVoices.add(TtsOption(v['locale']!, v['name']!));
          }
        }
      }
      ttsVoices.sort((a, b) => a.label.compareTo(b.label));
      ttsVoices.insert(0, TtsOption("Default", ""));
      ttsVoiceId = ttsVoices.first.id;
      await _tts.awaitSpeakCompletion(true);
    } catch (_) {}
  }

  //ttsVoiceIdを登録
  static Future<void> setTtsVoiceId(String newTtsVoiceId) async {
    final exists = ttsVoices.any((o) => o.id == newTtsVoiceId);
    if (exists) {
      ttsVoiceId = newTtsVoiceId;
    } else {
      ttsVoiceId = ttsVoices.first.id;
    }
    await _setSpeechVoiceFromId();
  }

  //ttsVoiceIdの声を用意
  static Future<void> _setSpeechVoiceFromId() async {
    if (ttsVoices.isEmpty || ttsVoiceId.isEmpty) {
      return;
    }
    final idx = ttsVoiceId.indexOf('|');
    String selLocale = '';
    String selName = ttsVoiceId;
    if (idx >= 0) {
      selLocale = ttsVoiceId.substring(0, idx);
      selName = ttsVoiceId.substring(idx + 1);
    }
    TtsOption? match;
    if (selLocale.isNotEmpty) {
      match = ttsVoices.firstWhereOrNull(
        (e) => e.name == selName && e.locale == selLocale,
      );
    }
    match ??= ttsVoices.firstWhereOrNull((e) => e.name == selName);
    if (match != null) {
      final locale = match.locale;
      final name = match.name;
      try {
        if (Platform.isAndroid) {
          // Prefer Google TTS if available; ignore errors if not installed
          try {
            await _tts.setEngine('com.google.android.tts');
          } catch (_) {}
          if (locale.isNotEmpty) {
            await _tts.setLanguage(locale);
          }
          await _tts.setVoice({'name': name, 'locale': locale});
        } else if (Platform.isIOS) {
          // On iOS, setting voice is sufficient; avoid setLanguage overriding the voice
          await _tts.setVoice({'name': name, 'locale': locale});
        } else {
          // Fallback for other platforms
          if (locale.isNotEmpty) {
            await _tts.setLanguage(locale);
          }
          await _tts.setVoice({'name': name, 'locale': locale});
        }
      } catch (_) {}
    }
  }

  //外部から呼び出し。インスタンス生成と設定を同時に行う。
  static Future<void> applyPreferences(String ttsVoiceId, double ttsVolume) async {
    await TextToSpeech.getInstance();
    await TextToSpeech.setTtsVoiceId(ttsVoiceId);
    await TextToSpeech.setVolume(ttsVolume);
  }

  //文字列を音声再生
  static Future<void> speak(String text) async {
    try {
      await _tts.stop();
      await _tts.speak(text);
    } catch (_) {}
  }

  //音声再生を停止
  static Future<void> stop() async {
    try {
      await _tts.stop();
    } catch (_) {}
  }

  //音声再生の速度
  static Future<void> setVolume(double volume) async {
    try {
      await _tts.setVolume(volume);
    } catch (_) {}
  }

  //音声の高さ
  static Future<void> setPitch(double pitch) async {
    try {
      await _tts.setPitch(pitch);
    } catch (_) {}
  }

  //音声の速度
  static Future<void> setSpeechRate(double speechRate) async {
    try {
      await _tts.setSpeechRate(speechRate);
    } catch (_) {}
  }

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

class ThemeColor {
  final int? themeNumber;
  final BuildContext context;

  ThemeColor({this.themeNumber, required this.context});

  Brightness get _effectiveBrightness {
    switch (themeNumber) {
      case 1:
        return Brightness.light;
      case 2:
        return Brightness.dark;
      default:
        return Theme.of(context).brightness;
    }
  }

  bool get _isLight => _effectiveBrightness == Brightness.light;

  //main page
  Color get colorNote => _isLight ? Color.fromRGBO(255, 0, 0, 0.4) : Color.fromRGBO(255, 100, 100, 0.8);
  Color get colorBack => _isLight ? Color.fromRGBO(255, 204, 0, 1) : Color.fromRGBO(
      117, 90, 0, 1.0);
  Color get colorSettingHeader => _isLight ? Color.fromRGBO(255, 204, 0, 1) : Color.fromRGBO(
      124, 101, 0, 1.0) ;
  String get backImage => _isLight ? 'assets/image/back.png' : 'assets/image/back_dark.png';
  //setting page
  Color get backColor => _isLight ? Colors.grey[300]! : Colors.grey[900]!;
  Color get cardColor => _isLight ? Colors.white : Colors.grey[800]!;
  Color get appBarForegroundColor => _isLight ? Colors.grey[700]! : Colors.white70;
  Color get dropdownColor => cardColor;
  Color get borderColor => _isLight ? Colors.grey[300]! : Colors.grey[700]!;
  Color get inputFillColor => _isLight ? Colors.grey[50]! : Colors.grey[900]!;
}
import 'package:flutter/material.dart';

class ThemeModeNumber {
  static ThemeMode numberToThemeMode(int value) {
    switch (value) {
      case 1:
        return ThemeMode.light;
      case 2:
        return ThemeMode.dark;
      default:
        return ThemeMode.system;
    }
  }
}