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-27

name: barcodereader
description: "barcodereader"

publish_to: 'none'

version: 2.20.3+69

environment:
  sdk: ^3.11.5

dependencies:     # flutter pub upgrade --major-versions
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  mobile_scanner: ^7.0.1
  shared_preferences: ^2.0.15
  flutter_toastr: ^1.0.3
  url_launcher: ^6.1.5
  share_plus: ^13.1.0
  google_mobile_ads: ^8.0.0
  provider: ^6.0.3
  clipboard: ^3.0.8
  intl: ^0.20.2
  audioplayers: ^6.0.0
  image_picker: ^1.1.2
  wakelock_plus: ^1.4.0
  in_app_review: ^2.0.11
  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.5     #flutter pub run flutter_native_splash:create

flutter_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: '#9da9f5'
  image: 'assets/image/splash.png'
  color_dark: '#9da9f5'
  image_dark: 'assets/image/splash.png'
  fullscreen: true
  android_12:
    icon_background_color: '#9da9f5'
    image: 'assets/image/splash.png'
    icon_background_color_dark: '#9da9f5'
    image_dark: 'assets/image/splash.png'

flutter:
  generate: true
  uses-material-design: true
  config:
    enable-swift-package-manager: true

  assets:
    - assets/image/
    - assets/sound/
/// Copyright© ao-system, Inc.

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

import 'package:barcodereader/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();
          }
        },
      ),
    );
  }
}
/// Copyright© ao-system, Inc.

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:barcodereader/_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();
    _retryTimer = null;
    _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();
          _retryTimer = null;
          _retryAttempt = 0;
          final cb = _onLoadedCb;
          if (cb != null) {
            cb();
          }
        },
        onAdFailedToLoad: (ad, err) {
          ad.dispose();
          _scheduleRetry();
        },
      ),
    )..load();
  }

  void _scheduleRetry() {
    if (kIsWeb) return;
    _retryTimer?.cancel();
    _retryTimer = null;
    _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();
    _retryTimer = null;
  }
}
/// Copyright© ao-system, Inc.

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:barcodereader/l10n/app_localizations.dart';
import 'package:barcodereader/_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;
    }
  }
}
/// Copyright© ao-system, Inc.

import 'package:flutter/services.dart';

///App Tracking Transparency サービス
///iOS 14以降で広告トラッキングの許可をリクエストする
class AttService {
  //チャンネル名をiOS側と一致させる
  static const _channel = MethodChannel('aosystem.att');

  static final AttService _instance = AttService._internal();
  factory AttService() => _instance;
  AttService._internal();

  ///トラッキング許可をリクエスト
  ///戻り値: AttStatus (enum)
  Future<AttStatus> requestTracking() async {
    try {
      //iOS側からInt値を受け取る
      final result = await _channel.invokeMethod<int>('requestTracking');
      //enumに変換して返却
      return parseAttStatus(result);
    } on MissingPluginException catch (_) {
      //ハンドラ未登録(iOS以外/旧バイナリ等)はアプリ全体を落とさない
      return AttStatus.unknown;
    } on PlatformException catch (_) {
      return AttStatus.unknown;
    }
  }

  ///現在のトラッキング許可状態を取得
  ///戻り値:AttStatus(enum)
  Future<AttStatus> getTrackingStatus() async {
    try {
      //iOS側からInt値を受け取る
      final result = await _channel.invokeMethod<int>('getTrackingStatus');
      //enumに変換して返却
      return parseAttStatus(result);
    } on MissingPluginException catch (_) {
      return AttStatus.unknown;
    } on PlatformException catch (_) {
      return AttStatus.unknown;
    }
  }

  ///トラッキングが許可されているか
  Future<bool> isTrackingAuthorized() async {
    final status = await getTrackingStatus();
    return status == AttStatus.authorized;
  }
}

//一緒に利用する enum とヘルパー関数

enum AttStatus {
  notDetermined, // 0
  restricted,    // 1
  denied,        // 2
  authorized,    // 3
  unknown,       // 4
}

AttStatus parseAttStatus(int? value) {
  switch (value) {
    case 0:
      return AttStatus.notDetermined;
    case 1:
      return AttStatus.restricted;
    case 2:
      return AttStatus.denied;
    case 3:
      return AttStatus.authorized;
    default:
      return AttStatus.unknown;
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:share_plus/share_plus.dart';

import 'package:barcodereader/l10n/app_localizations.dart';
import 'package:barcodereader/scan_provider.dart';
import 'package:barcodereader/ad_manager.dart';
import 'package:barcodereader/ad_banner_widget.dart';

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

class _HistoryPageState extends State<HistoryPage> {
  final ScrollController _scrollController = ScrollController();
  late AdManager _adManager;

  @override
  void initState() {
    super.initState();
    _adManager = AdManager();
    // buildが完了した直後に実行
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // 遅延させてからスクロールを実行
      Timer(const Duration(milliseconds: 50), _scrollToBottom);
    });
  }

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

  void _scrollToBottom() {
    // scrollControllerがListViewに紐付いていて、スクロール可能な場合
    if (_scrollController.hasClients) {
      _scrollController.animateTo(
        _scrollController.position.maxScrollExtent,
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeOut,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context)!;
    return Consumer<ScanProvider>(
      builder: (context, scanProvider, child) {
        // スキャン結果が空の場合の表示
        if (scanProvider.scanResults.isEmpty) {
          return Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.transparent,
              title: Text(l.history),
              centerTitle: true,
            ),
            body: Center(
              child: Text(l.noData),
            ),
            bottomNavigationBar: AdBannerWidget(adManager: _adManager),
          );
        }
        // スキャン結果をリスト表示
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            title: Text(l.history),
            centerTitle: true,
          ),
          body: SafeArea(
            child: ListView.builder(
              controller: _scrollController, // コントローラーを紐付け
              padding: const EdgeInsets.all(8.0),
              itemCount: scanProvider.scanResults.length,
              itemBuilder: (context, index) {
                // 古いものが上、新しいものが下になるようにそのままのindexを使用
                final result = scanProvider.scanResults[index];
                final parts = result.split(',');
                // データのパース
                final timestamp =
                    parts.isNotEmpty
                        ? parts[0]
                        : l.noDate;
                final format = parts.length > 1
                    ? parts[1].split('.').last
                    : l.unknownFormat;
                final content = parts.length > 2
                    ? parts.sublist(2).join(',')
                    : l.noContent;

                // デフォルトのテキストスタイルを取得
                final textTheme = Theme.of(context).textTheme;
                final titleStyle = textTheme.titleMedium;

                return Card(
                  margin: const EdgeInsets.symmetric(vertical: 4.0),
                  child: ListTile(
                    title: Text('$timestamp - $format'),
                    subtitle: Text(content, style: titleStyle),
                    onTap: () {
                      showDialog(
                        context: context,
                        builder: (context) => AlertDialog(
                          title: Text(content),
                          actions: <Widget>[
                            TextButton(
                              child: Text(l.sendAction),
                              onPressed: () {
                                Navigator.of(context).pop();
                                SharePlus.instance.share(ShareParams(text: content));
                              },
                            ),
                            TextButton(
                              child: Text(l.deleteAction),
                              onPressed: () {
                                Navigator.of(context).pop();
                                scanProvider.deleteScanResult(index);
                              },
                            ),
                            TextButton(
                              child: Text(l.cancel),
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                );
              },
            ),
          ),
          bottomNavigationBar: AdBannerWidget(adManager: _adManager),
        );
      },
    );
  }

}
/// Copyright© ao-system, Inc.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

import 'package:barcodereader/l10n/app_localizations.dart';
import 'package:barcodereader/ad_banner_widget.dart';
import 'package:barcodereader/info_page.dart';
import 'package:barcodereader/history_page.dart';
import 'package:barcodereader/scan_provider.dart';
import 'package:barcodereader/scanner_page.dart';
import 'package:barcodereader/image_scanner_page.dart';
import 'package:barcodereader/setting_page.dart';
import 'package:barcodereader/model.dart';
import 'package:barcodereader/loading_screen.dart';
import 'package:barcodereader/main.dart';
import 'package:barcodereader/theme_color.dart';

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

  @override
  State<MainHomePage> createState() => _MainHomePageState();
}

class _MainHomePageState extends State<MainHomePage> with WidgetsBindingObserver, WidgetsBindingObserver {
  late ThemeColor _themeColor;
  bool _isReady = false;
  //
  final _textController = TextEditingController();
  final ScrollController _scrollController = ScrollController();
  late ScanProvider _scanProvider;
  VoidCallback? _settingsListener;
  bool _hasAutoStartedScan = false;
  //
  static const double _buttonHeight1 = 100.0;
  static const double _buttonHeight2 = 80.0;

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

  void _initState() async {
    _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
    _scanProvider = context.read<ScanProvider>();
    _scanProvider.loadScanResults().then((_) {
      _updateTextField();
    });
    _scanProvider.addListener(_updateTextField);
    _settingsListener = _handleSettingsChanged;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _maybeStartScanOnLaunch();
    });
    _wakelock();
    if (mounted) {
      setState(() {
        _isReady = true;
      });
    }
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    WakelockPlus.disable();
    _scanProvider.removeListener(_updateTextField);
    _textController.dispose();
    _scrollController.dispose();
    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;
    }
  }

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

  Future<void> _scanBarcode() async {
    await Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) => ScannerPage(
          onScan: (result) async {
            await _scanProvider.addScanResult(result);
          },
        ),
      ),
    );
  }

  void _handleSettingsChanged() {
    _maybeStartScanOnLaunch();
  }

  void _maybeStartScanOnLaunch() {
    if (_hasAutoStartedScan) {
      return;
    }
    if (!Model.startScanOnLaunch) {
      return;
    }
    _hasAutoStartedScan = true;
    if (_settingsListener != null) {
      _settingsListener = null;
    }
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) {
        unawaited(_scanBarcode());
      }
    });
  }

  void _updateTextField() {
    _textController.text = _scanProvider.getFormattedResults();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (_scrollController.hasClients) {
        _scrollController.animateTo(
          _scrollController.position.maxScrollExtent,
          duration: const Duration(milliseconds: 300),
          curve: Curves.easeOut,
        );
      }
    });
  }

  void _copyToClipboard(String text) {
    if (text.isEmpty) {
      return;
    }
    final l = AppLocalizations.of(context)!;
    Clipboard.setData(ClipboardData(text: text));
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(l.copiedToClipboard),
        duration: const Duration(seconds: 1),
      ),
    );
  }

  void _shareText(String text) {
    if (text.isEmpty) {
      return;
    }
    //SharePlus.instance.share(ShareParams(text: text));	//iPadで動作しない
    final box = context.findRenderObject() as RenderBox?;
    SharePlus.instance.share(
      ShareParams(
        text: text,
        sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
      ),
    );
  }

  void _launchUrlOrShare(String text) async {
    if (text.isEmpty) {
      return;
    }
    final l = AppLocalizations.of(context)!;
    Uri uri;
    final parsedUri = Uri.tryParse(text);
    if (parsedUri != null &&
        (parsedUri.scheme == 'http' || parsedUri.scheme == 'https')) {
      uri = parsedUri;
    } else {
      uri = Uri.parse(
        'https://www.google.com/search?q=${Uri.encodeComponent(text)}',
      );
    }
    try {
      if (!await launchUrl(uri)) {
        if (!mounted) return;
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(l.couldNotLaunchBrowserFor(uri.toString())),
          ),
        );
      }
    } catch (e) {
      if (!mounted) return;
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(
        SnackBar(
          content: Text(l.errorLaunchingBrowser(e.toString())),
        ),
      );
    }
  }

  Future<bool> _showConfirmDialog(String title) async {
    final l = AppLocalizations.of(context)!;
    return await showDialog<bool>(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(title),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: Text(l.cancel),
          ),
          TextButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: Text(l.ok),
          ),
        ],
      ),
    ) ??
    false;
  }

  Future<void> _showMessageDialog({String? title, required List<String> messages}) async {
    final l = AppLocalizations.of(context)!;
    await showDialog<void>(
      context: context,
      builder: (context) => AlertDialog(
        title: title != null ? Text(title) : null,
        content: messages.length == 1
          ? Text(messages.first)
          : Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              for (var i = 0; i < messages.length; i++)
                Padding(
                  padding: EdgeInsets.only(bottom: i == messages.length - 1 ? 0 : 8.0),
                  child: Text(messages[i]),
                ),
            ],
          ),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: Text(l.ok),
          ),
        ],
      ),
    );
  }

  Future<void> _handleDeleteDuplicates() async {
    final l = AppLocalizations.of(context)!;
    if (!await _showConfirmDialog(l.deleteDuplicatesConfirmation)) {
      return;
    }
    final results = await _scanProvider.removeDuplicateScanResults();
    if (!mounted) {
      return;
    }
    if (results.isEmpty) {
      await _showMessageDialog(
        title: l.deleteDuplicates,
        messages: [l.noDuplicateDataFound],
      );
      return;
    }
    await _showMessageDialog(
      title: l.duplicateRemovalResultTitle,
      messages: results
        .map(
          (entry) => l.duplicateRemovalEntry(
            entry.removedCount,
            entry.data,
          ),
        )
        .toList(),
    );
  }

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

  @override
  Widget build(BuildContext context) {
    if (!_isReady) {
      return const LoadingScreen();
    }
    final l = AppLocalizations.of(context)!;
    return Scaffold(
      backgroundColor: _themeColor.mainButtonColor2,
      appBar: AppBar(
        backgroundColor: _themeColor.mainButtonColor2,
        elevation: 0,
        actions: [
          IconButton(
            icon: const Icon(Icons.image_search_outlined),
            tooltip: l.imageScan,
            onPressed: () => Navigator.of(context).push(
              MaterialPageRoute(
                builder: (context) => ImageScannerPage(
                  onScan: (result) async {
                    await _scanProvider.addScanResult(result);
                  },
                ),
              ),
            ),
          ),
          IconButton(
            icon: const Icon(Icons.info_outline),
            onPressed: () => Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const InfoPage()),
            ),
          ),
          IconButton(
            icon: const Icon(Icons.list),
            onPressed: () => Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const HistoryPage()),
            ),
          ),
          IconButton(
            icon: const Icon(Icons.settings),
            color: _themeColor.mainForeColor,
            tooltip: l.setting,
            onPressed: _onOpenSetting,
          ),
        ],
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              _buildFullWidthButton(
                l.scan,
                _scanBarcode,
                backColor: _themeColor.mainButtonColor1,
                foreColor: _themeColor.mainForeColor,
                height: _buttonHeight1,
              ),
              Row(children:[
                _buildHalfWidthButton(
                  l.copy,
                  () async {
                    if (await _showConfirmDialog(l.copyAll)) {
                      _copyToClipboard(_scanProvider.toCsv());
                    }
                  },
                  backColor: _themeColor.mainButtonColor2,
                  foreColor: _themeColor.mainForeColor,
                  height: _buttonHeight2,
                ),
                _buildHalfWidthButton(
                  l.send,
                  () async {
                    if (await _showConfirmDialog(l.sendAll)) {
                      _shareText(_scanProvider.toCsv());
                    }
                  },
                  backColor: _themeColor.mainButtonColor2,
                  foreColor: _themeColor.mainForeColor,
                  height: _buttonHeight2,
                ),
              ]),
              Row(children:[
                _buildHalfWidthButton(
                  l.deleteDuplicates,
                  () {
                    unawaited(_handleDeleteDuplicates());
                  },
                  backColor: _themeColor.mainButtonColor2,
                  foreColor: _themeColor.mainForeColor,
                  height: _buttonHeight2,
                ),
                _buildHalfWidthButton(
                  l.deleteAll,
                  () async {
                    if (await _showConfirmDialog(l.deleteAll)) {
                      _scanProvider.deleteAllScanResults();
                    }
                  },
                  backColor: _themeColor.mainButtonColor2,
                  foreColor: _themeColor.mainForeColor,
                  height: _buttonHeight2,
                ),
              ]),
              Row(children:[
                _buildHalfWidthButton(
                  l.copyLast,
                  () => _copyToClipboard(_scanProvider.getLastScanContent()),
                  backColor: _themeColor.mainButtonColor3,
                  foreColor: _themeColor.mainForeColor,
                  height: _buttonHeight2,
                ),
                _buildHalfWidthButton(
                  l.sendLast,
                  () => _shareText(_scanProvider.getLastScanContent()),
                  backColor: _themeColor.mainButtonColor3,
                  foreColor: _themeColor.mainForeColor,
                  height: _buttonHeight2,
                ),
              ]),
              Row(children:[
                _buildHalfWidthButton(
                  l.browserLast,
                  () => _launchUrlOrShare(_scanProvider.getLastScanContent()),
                  backColor: _themeColor.mainButtonColor3,
                  foreColor: _themeColor.mainForeColor,
                  height: _buttonHeight2,
                ),
                _buildHalfWidthButton(
                  l.deleteLast,
                  () async {
                    if (await _showConfirmDialog(l.deleteLastConfirmation)) {
                      _scanProvider.deleteLastScanResult();
                    }
                  },
                  backColor: _themeColor.mainButtonColor3,
                  foreColor: _themeColor.mainForeColor,
                  height: _buttonHeight2,
                ),
              ]),
              TextField(
                controller: _textController,
                scrollController: _scrollController,
                readOnly: true,
                maxLines: null,
                textAlignVertical: TextAlignVertical.top,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  isDense: true,
                  filled: true,
                  contentPadding: const EdgeInsets.only(left: 8, right: 8, top: 8, bottom: 20),
                  fillColor: _themeColor.mainResultBackColor,
                ),
                style: const TextStyle(fontSize: 12.0),
              )
            ],
          )
        )
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _buildFullWidthButton(
    String text,
    VoidCallback onPressed, {
      required Color backColor,
      required Color foreColor,
      required double height,
    }) {
    return Container(
      height: height,
      decoration: BoxDecoration(color: backColor),
      child: SizedBox.expand(
        child: TextButton(
          onPressed: onPressed,
          child: Text(text, style: TextStyle(color: foreColor))
        ),
      ),
    );
  }

  Widget _buildHalfWidthButton(
      String text,
      VoidCallback onPressed, {
        required Color backColor,
        required Color foreColor,
        required double height,
      }) {
    return Expanded(
      child: SizedBox(
        height: height,
        child: Container(
          decoration: BoxDecoration(color: backColor),
          child: TextButton(
            onPressed: onPressed,
            child: Text(text, style: TextStyle(color: foreColor)),
          ),
        ),
      )
    );
  }

}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

import 'package:barcodereader/l10n/app_localizations.dart';
import 'package:barcodereader/model.dart';
import 'package:barcodereader/ad_manager.dart';
import 'package:barcodereader/ad_banner_widget.dart';
import 'package:barcodereader/theme_color.dart';

class ImageScannerPage extends StatefulWidget {
  const ImageScannerPage({super.key, required this.onScan});

  final FutureOr<void> Function(String result) onScan;

  @override
  State<ImageScannerPage> createState() => _ImageScannerPageState();
}

class _ImageScannerPageState extends State<ImageScannerPage> {
  late final MobileScannerController _scannerController;
  late final TransformationController _transformationController;
  late final AdManager _adManager;
  final GlobalKey _imageBoundaryKey = GlobalKey();
  XFile? _selectedImage;
  String? _statusMessage;
  bool _isProcessing = false;
  double _contrast = 1.0;
  late ThemeColor _themeColor;
  bool _isFirst = true;

  @override
  void initState() {
    super.initState();
    _scannerController = _createScannerController();
    _transformationController = TransformationController();
    _adManager = AdManager();
  }

  MobileScannerController _createScannerController() {
    final BarcodeFormat? selectedFormat = _selectedBarcodeFormat();
    final List<BarcodeFormat> formats = selectedFormat == null
        ? const <BarcodeFormat>[]
        : <BarcodeFormat>[selectedFormat];
    return MobileScannerController(formats: formats);
  }

  BarcodeFormat? _selectedBarcodeFormat() {
    final selection = Model.selectedBarcodeFormat;
    if (selection == 'all') {
      return null;
    }
    try {
      return BarcodeFormat.values.firstWhere((format) => format.name == selection);
    } catch (_) {
      return null;
    }
  }

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

  Future<void> _pickImage() async {
    if (kIsWeb) {
      return;
    }
    final picker = ImagePicker();
    final XFile? image = await picker.pickImage(source: ImageSource.gallery);
    if (image == null) {
      return;
    }
    _transformationController.value = Matrix4.identity();
    setState(() {
      _selectedImage = image;
      _statusMessage = null;
      _contrast = 1.0;
    });
  }

  Future<void> _scanSelectedImage() async {
    if (kIsWeb || _selectedImage == null) {
      return;
    }
    String? tempPath;
    setState(() {
      _statusMessage = null;
      _isProcessing = true;
    });
    try {
      final String imagePath = await _prepareImagePathForScan();
      if (imagePath != _selectedImage!.path) {
        tempPath = imagePath;
      }
      final BarcodeCapture? capture = await _scannerController.analyzeImage(
        imagePath,
      );
      if (!mounted) {
        return;
      }
      final Barcode? matchingBarcode = capture?.barcodes.isNotEmpty ?? false
          ? _pickMatchingBarcode(capture!.barcodes)
          : null;
      if (matchingBarcode == null) {
        setState(() {
          _statusMessage = AppLocalizations.of(context)!.imageScanNotFound;
          _isProcessing = false;
        });
        return;
      }
      final l = AppLocalizations.of(context)!;
      final String code = matchingBarcode.rawValue ?? l.noData;
      final String format = matchingBarcode.format.name;
      final String result = '${DateFormat('yyyyMMdd_HHmmss').format(DateTime.now())},$format,$code';
      await Future.sync(() => widget.onScan(result));
      if (Model.beepOnScan && Model.beepVolume > 0.0) {
        final soundId = Model.selectedBeepSound;
        final audio = AudioPlayer();
        audio.setVolume(Model.beepVolume);
        audio.play(AssetSource('sound/beep$soundId.mp3'));
      }
      if (!mounted) {
        return;
      }
      setState(() {
        _statusMessage = code;
        _isProcessing = false;
      });
      if (Model.snackBarTime > 0) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(code),
            duration: Duration(
              milliseconds: (Model.snackBarTime * 1000).toInt(),
            ),
          ),
        );
      }
    } catch (error) {
      if (!mounted) {
        return;
      }
      setState(() {
        _statusMessage = error.toString();
        _isProcessing = false;
      });
    } finally {
      if (tempPath != null) {
        try {
          File(tempPath).delete();
        } catch (_) {
          // ignore cleanup errors
        }
      }
    }
  }

  Barcode? _pickMatchingBarcode(List<Barcode> barcodes) {
    final selection = Model.selectedBarcodeFormat;
    if (selection == 'all') {
      return barcodes.first;
    }
    for (final barcode in barcodes) {
      if (barcode.format.name == selection) {
        return barcode;
      }
    }
    return null;
  }

  ColorFilter _contrastFilter() {
    final c = _contrast;
    final t = (1 - c) / 2;
    return ColorFilter.matrix(<double>[
      c, 0, 0, 0, t,
      0, c, 0, 0, t,
      0, 0, c, 0, t,
      0, 0, 0, 1, 0,
    ]);
  }

  Future<String> _prepareImagePathForScan() async {
    // Wait for any pending layout/paint before capturing.
    await WidgetsBinding.instance.endOfFrame;
    final boundary = _imageBoundaryKey.currentContext
        ?.findRenderObject() as RenderRepaintBoundary?;
    if (boundary == null) {
      return _selectedImage!.path;
    }
    final pixelRatio = MediaQuery.of(context).devicePixelRatio;
    final ui.Image captured =
        await boundary.toImage(pixelRatio: pixelRatio);
    final byteData =
        await captured.toByteData(format: ui.ImageByteFormat.png);
    if (byteData == null) {
      return _selectedImage!.path;
    }
    final tempPath =
        '${Directory.systemTemp.path}${Platform.pathSeparator}image_scan_${DateTime.now().millisecondsSinceEpoch}.png';
    final tempFile = File(tempPath);
    await tempFile.writeAsBytes(
      byteData.buffer.asUint8List(),
      flush: true,
    );
    return tempPath;
  }

  @override
  Widget build(BuildContext context) {
    if (_isFirst) {
      _isFirst = false;
      _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
    }
    final l = AppLocalizations.of(context)!;
    final statusStyle = Theme.of(context).textTheme.bodyMedium;
    final double statusHeight = (statusStyle?.fontSize ?? 14) * (statusStyle?.height ?? 1.2);
    return Scaffold(
      backgroundColor: _themeColor.mainBackColor,
      appBar: AppBar(
        backgroundColor: _themeColor.mainBackColor,
        title: Text(l.imageScan),
        centerTitle: true,
      ),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 8),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Row(children:[
                Expanded(
                  child: FilledButton.icon(
                    onPressed: _isProcessing ? null : _pickImage,
                    icon: const Icon(Icons.photo_library_outlined),
                    label: Text(l.selectImage),
                  )
                ),
                const SizedBox(width: 8),
                Expanded(
                  child: FilledButton.icon(
                    onPressed: _selectedImage == null || _isProcessing
                        ? null
                        : _scanSelectedImage,
                    icon: const Icon(Icons.qr_code_scanner),
                    label: Text(l.scan),
                  )
                ),
              ]),
              const SizedBox(height: 16),
              SizedBox(
                height: 4,
                child: _isProcessing
                    ? const LinearProgressIndicator()
                    : const SizedBox.shrink(),
              ),
              const SizedBox(height: 16),
              Expanded(
                child: _selectedImage == null
                  ? Center(child: Text(l.noData))
                  : ClipRRect(
                    borderRadius: BorderRadius.circular(12),
                    child: RepaintBoundary(
                      key: _imageBoundaryKey,
                      child: Container(
                        color: Colors.white,
                        child: kIsWeb
                            ? const SizedBox()
                            : InteractiveViewer(
                          transformationController: _transformationController,
                          panEnabled: true,
                          minScale: 0.8,
                          maxScale: 4,
                          child: ColorFiltered(
                            colorFilter: _contrastFilter(),
                            child: Image.file(
                              File(_selectedImage!.path),
                              fit: BoxFit.contain,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              if (_selectedImage != null && !kIsWeb) ...[
                const SizedBox(height: 12),
                Row(
                  children: [
                    const Icon(Icons.tonality),
                    Expanded(
                      child: Slider(
                        value: _contrast,
                        onChanged: (value) {
                          setState(() {
                            _contrast = value;
                          });
                        },
                        min: 0,
                        max: 2,
                        divisions: 40,
                      ),
                    ),
                  ],
                ),
              ],
              const SizedBox(height: 12),
              SizedBox(
                height: statusHeight,
                child: _statusMessage == null
                  ? const SizedBox.shrink()
                  : Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      _statusMessage!,
                      style: statusStyle,
                    ),
                  ),
              ),
            ],
          ),
        ),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: _adManager),
    );
  }
}
/// Copyright© ao-system, Inc.

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

import 'package:barcodereader/l10n/app_localizations.dart';
import 'package:barcodereader/scan_provider.dart';
import 'package:barcodereader/ad_manager.dart';
import 'package:barcodereader/ad_banner_widget.dart';
import 'package:barcodereader/model.dart';
import 'package:barcodereader/theme_color.dart';

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

class _InfoPageState extends State<InfoPage> {
  late AdManager _adManager;
  late ThemeColor _themeColor;
  bool _isFirst = true;

  @override
  void initState() {
    super.initState();
    _adManager = AdManager();
  }

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

  @override
  Widget build(BuildContext context) {
    if (_isFirst) {
      _isFirst = false;
      _themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
    }
    final l = AppLocalizations.of(context)!;
    return Scaffold(
      backgroundColor: _themeColor.backColor,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        title: Text(l.information),
        centerTitle: true,
      ),
      body: SafeArea(
        child: Consumer<ScanProvider>(
          builder: (context, scanProvider, child) {
            final results = scanProvider.scanResults;
            if (results.isEmpty) {
              return Center(
                child: Text(l.noData),
              );
            }
            final breakdown = _calculateBreakdown(results);
            return ListView(
              padding: const EdgeInsets.all(16.0),
              children: [
                _buildTotal(results.length),
                const Divider(height: 40),
                for (final entry in breakdown)
                  _buildEntry(entry.key, entry.value)
              ],
            );
          },
        ),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: _adManager),
    );
  }

  List<MapEntry<String, int>> _calculateBreakdown(List<String> scanResults) {
    final counts = <String, int>{};
    for (final result in scanResults) {
      final parts = result.split(',');
      String format;
      if (parts.length >= 2) {
        final formatPart = parts[1];
        final normalizedFormat = formatPart.split('.').last;
        format = normalizedFormat.isEmpty ? 'unknown' : normalizedFormat;
      } else {
        format = 'unknown';
      }
      counts.update(format, (value) => value + 1, ifAbsent: () => 1);
    }
    final sortedEntries = counts.entries.toList()
      ..sort((a, b) {
        final countCompare = b.value.compareTo(a.value);
        if (countCompare != 0) {
          return countCompare;
        }
        return a.key.compareTo(b.key);
      });
    return sortedEntries;
  }

  Widget _buildTotal(int count) {
    return SizedBox(
      width: double.infinity,
      child: Card(
        color: _themeColor.cardColor,
        elevation: 0,
        shadowColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
          child: Text('Total: $count', style: Theme.of(context).textTheme.titleLarge),
        )
      )
    );
  }

  Widget _buildEntry(String format, int count) {
    return SizedBox(
      width: double.infinity,
      child: Card(
        color: _themeColor.cardColor,
        elevation: 0,
        shadowColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
          child: Text('$format: $count', style: Theme.of(context).textTheme.titleLarge),
        )
      )
    );
  }

}
/// Copyright© ao-system, Inc.

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,
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  //UI設定
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      systemNavigationBarColor: Colors.transparent,
      statusBarColor: Colors.transparent,
      systemNavigationBarContrastEnforced: false,
      systemStatusBarContrastEnforced: false,
    ),
  );
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => ScanProvider()),
      ],
      child: 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> {
  late final AdManager adManager;
  ThemeMode _themeMode = ThemeMode.system;
  Locale? _locale;
  bool _hasError = false;
  bool _isReady = false;

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

  void _initState() async {
    try {
      //ad
      adManager = AdManager();
      //アプリの基本データ
      await Model.ensureReady();
      //ATT
      //iOSは「アプリがactive/resumed状態」でないとrequestTrackingがダイアログを出さず即座にnotDeterminedを返すため、ライフサイクルがresumedになるまで待つ。
      //(iOSは「設定→トラッキング」でトグルを変えるとアプリプロセスをkillして再起動するので、起動時にgetTrackingStatusを読めば常に最新の値が手に入る)
      if (!kIsWeb && Platform.isIOS) {
        if (await _waitForResumed()) {
          final attService = AttService();
          //未決定(初回起動)のときだけダイアログ表示。既に決定済みならスキップ。
          if (await attService.getTrackingStatus() == AttStatus.notDetermined) {
            await attService.requestTracking();
          }
        }
      }
      //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;
        });
      }
    }
  }

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

  //アプリがactive/resumed状態になるまで待つ。すでにresumedならすぐにtrueを返す。タイムアウト時はfalse。
  Future<bool> _waitForResumed({
    Duration timeout = const Duration(seconds: 5),
  }) async {
    final binding = WidgetsBinding.instance;
    if (binding.lifecycleState == AppLifecycleState.resumed) {
      return true;
    }
    final completer = Completer<bool>();
    late final AppLifecycleListener listener;
    listener = AppLifecycleListener(
      onStateChange: (state) {
        if (state == AppLifecycleState.resumed && !completer.isCompleted) {
          completer.complete(true);
        }
      },
    );
    try {
      return await completer.future.timeout(timeout, onTimeout: () => false);
    } finally {
      listener.dispose();
    }
  }

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

  Color _getRainbowAccentColor(int hue) {
    return HSVColor.fromAHSV(1.0, hue.toDouble(), 1.0, 1.0).toColor();
  }

  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 = _getRainbowAccentColor(Model.colorHue);
    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,
            ),
          ),
        ),
      ),
    );
  }

}
/// Copyright© ao-system, Inc.

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

import 'package:barcodereader/l10n/app_localizations.dart';

class Model {
  Model._();

  static const String _prefContinuousScan = 'continuousScan';
  static const String _prefScanInterval = 'scanInterval';
  static const String _prefScanAreaRestrictionLevel = 'scanAreaRestrictionLevel';
  static const String _prefSelectedBarcodeFormat = 'selectedBarcodeFormat';
  static const String _prefSnackBarTime = 'snackBarTime';
  static const String _prefStartScanOnLaunch = 'startScanOnLaunch';
  static const String _prefBeepOnScan = 'beepOnScan';
  static const String _prefBeepVolume = 'beepVolume';
  static const String _prefSelectedBeepSound = 'selectedBeepSound';
  static const String _prefFlashOnScan = 'flashOnScan';
  static const String _prefLockOrientation = 'lockOrientation';
  static const String _prefColorHue = 'colorHue';
  static const String _prefColorSaturation = 'colorSaturation';
  static const String _prefColorBrightness = 'colorBrightness';
  static const String _prefWakelockEnabled = 'wakelockEnabled';
  static const String _prefThemeNumber = 'themeNumber';
  static const String _prefLanguageCode = 'languageCode';

  static bool _ready = false;
  static bool _continuousScan = false;
  static double _scanInterval = 1.0;
  static int _scanAreaRestrictionLevel = 0;
  static String _selectedBarcodeFormat = 'all';
  static double _snackBarTime = 1.0;
  static bool _startScanOnLaunch = false;
  static bool _beepOnScan = true;
  static double _beepVolume = 1.0;
  static int _selectedBeepSound = 1;
  static bool _flashOnScan = true;
  static bool _lockOrientation = false;
  static int _colorHue = 250;
  static double _colorSaturation = 0.6;
  static double _colorBrightness = 1.0;
  static bool _wakelockEnabled = false;
  static int _themeNumber = 0;
  static String _languageCode = '';

  static bool get continuousScan => _continuousScan;
  static double get scanInterval => _scanInterval;
  static int get scanAreaRestrictionLevel => _scanAreaRestrictionLevel;
  static String get selectedBarcodeFormat => _selectedBarcodeFormat;
  static double get snackBarTime => _snackBarTime;
  static bool get startScanOnLaunch => _startScanOnLaunch;
  static bool get beepOnScan => _beepOnScan;
  static double get beepVolume => _beepVolume;
  static int get selectedBeepSound => _selectedBeepSound;
  static bool get flashOnScan => _flashOnScan;
  static bool get lockOrientation => _lockOrientation;
  static int get colorHue => _colorHue;
  static double get colorSaturation => _colorSaturation;
  static double get colorBrightness => _colorBrightness;
  static bool get wakelockEnabled => _wakelockEnabled;
  static int get themeNumber => _themeNumber;
  static String get languageCode => _languageCode;

  static Future<void> ensureReady() async {
    if (_ready) {
      return;
    }
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    //
    _continuousScan = prefs.getBool(_prefContinuousScan) ?? false;
    _scanInterval = (prefs.getDouble(_prefScanInterval) ?? 1.0).clamp(0.0, 3.0);
    _scanAreaRestrictionLevel = prefs.getInt(_prefScanAreaRestrictionLevel) ?? 0;
    _selectedBarcodeFormat = prefs.getString(_prefSelectedBarcodeFormat) ?? 'all';
    _snackBarTime = (prefs.getDouble(_prefSnackBarTime) ?? 1.0).clamp(0.0, 10.0);
    _startScanOnLaunch = prefs.getBool(_prefStartScanOnLaunch) ?? false;
    _beepOnScan = prefs.getBool(_prefBeepOnScan) ?? true;
    _beepVolume = (prefs.getDouble(_prefBeepVolume) ?? 1.0).clamp(0.0, 1.0);
    _selectedBeepSound = prefs.getInt(_prefSelectedBeepSound) ?? 1;
    _flashOnScan = prefs.getBool(_prefFlashOnScan) ?? true;
    _lockOrientation = prefs.getBool(_prefLockOrientation) ?? false;
    _colorHue = (prefs.getInt(_prefColorHue) ?? 250).clamp(0, 360);
    _colorSaturation = (prefs.getDouble(_prefColorSaturation) ?? 0.6).clamp(0.0, 1.0);
    _colorBrightness = (prefs.getDouble(_prefColorBrightness) ?? 1.0).clamp(0.0, 1.0);
    _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> setContinuousScan(bool value) async {
    _continuousScan = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefContinuousScan, value);
  }

  static Future<void> setScanInterval(double value) async {
    _scanInterval = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefScanInterval, value);
  }

  static Future<void> setScanAreaRestrictionLevel(int value) async {
    _scanAreaRestrictionLevel = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefScanAreaRestrictionLevel, value);
  }

  static Future<void> setSelectedBarcodeFormat(String value) async {
    _selectedBarcodeFormat = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString(_prefSelectedBarcodeFormat, value);
  }

  static Future<void> setSnackBarTime(double value) async {
    _snackBarTime = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefSnackBarTime, value);
  }

  static Future<void> setStartScanOnLaunch(bool value) async {
    _startScanOnLaunch = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefStartScanOnLaunch, value);
  }

  static Future<void> setBeepOnScan(bool value) async {
    _beepOnScan = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefBeepOnScan, value);
  }

  static Future<void> setBeepVolume(double value) async {
    _beepVolume = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefBeepVolume, value);
  }

  static Future<void> setSelectedBeepSound(int value) async {
    _selectedBeepSound = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefSelectedBeepSound, value);
  }

  static Future<void> setFlashOnScan(bool value) async {
    _flashOnScan = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefFlashOnScan, value);
  }

  static Future<void> setLockOrientation(bool value) async {
    _lockOrientation = value;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_prefLockOrientation, value);
  }

  static Future<void> setColorHue(int value) async {
    _colorHue = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefColorHue, value);
  }

  static Future<void> setColorSaturation(double value) async {
    _colorSaturation = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefColorSaturation, value);
  }

  static Future<void> setColorBrightness(double value) async {
    _colorBrightness = value;
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_prefColorBrightness, 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 SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefThemeNumber, value);
  }

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

}
/// Copyright© ao-system, Inc.

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,
  );
}
/// Copyright© ao-system, Inc.

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

class DuplicateRemovalEntry {
  const DuplicateRemovalEntry({required this.data, required this.removedCount});

  final String data;
  final int removedCount;
}

class ScanProvider extends ChangeNotifier {
  List<String> _scanResults = [];
  static const String _scanResultsKey = 'scan_results';
  List<String> get scanResults => _scanResults;

  Future<void> loadScanResults() async {
    final prefs = await SharedPreferences.getInstance();
    _scanResults = prefs.getStringList(_scanResultsKey) ?? [];
    notifyListeners();
  }

  Future<void> addScanResult(String result) async {
    _scanResults.add(result);
    await _saveResults();
  }

  Future<void> deleteScanResult(int index) async {
    if (index >= 0 && index < _scanResults.length) {
      _scanResults.removeAt(index);
      await _saveResults();
    }
  }

  Future<void> deleteLastScanResult() async {
    if (_scanResults.isNotEmpty) {
      _scanResults.removeLast();
      await _saveResults();
    }
  }

  Future<void> deleteAllScanResults() async {
    _scanResults.clear();
    await _saveResults();
  }

  Future<List<DuplicateRemovalEntry>> removeDuplicateScanResults() async {
    final seenKeys = <String>{};
    final removalCounts = <String, int>{};
    final displayTexts = <String, String>{};
    final indexesToRemove = <int>[];

    for (var i = 0; i < _scanResults.length; i++) {
      final entry = _scanResults[i];
      final parts = entry.split(',');

      final hasContent = parts.length >= 3;
      final format = hasContent ? parts[1] : '';
      final content = hasContent ? parts.sublist(2).join(',') : entry;
      final key = hasContent ? '$format::$content' : entry;

      displayTexts.putIfAbsent(key, () => content);

      if (!seenKeys.add(key)) {
        removalCounts.update(key, (value) => value + 1, ifAbsent: () => 1);
        indexesToRemove.add(i);
      }
    }

    if (indexesToRemove.isEmpty) {
      return const <DuplicateRemovalEntry>[];
    }

    indexesToRemove.sort((a, b) => b.compareTo(a));
    for (final index in indexesToRemove) {
      _scanResults.removeAt(index);
    }

    await _saveResults();

    return removalCounts.entries
        .map(
          (entry) => DuplicateRemovalEntry(
            data: displayTexts[entry.key] ?? entry.key,
            removedCount: entry.value,
          ),
        )
        .toList();
  }

  Future<void> _saveResults() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setStringList(_scanResultsKey, _scanResults);
    notifyListeners();
  }

  String getFormattedResults() {
    return _scanResults.map((res) {
      final parts = res.split(',');
      if (parts.length >= 2) {
        parts[1] = parts[1].split('.').last;
        return parts.join(',');
      }
      return res;
    }).join('\n');
  }

  String getLastScanContent() {
    if (_scanResults.isEmpty) return '';
    final parts = _scanResults.last.split(',');
    if (parts.length >= 3) {
      return parts.sublist(2).join(',');
    }
    return _scanResults.last;
  }

  String toCsv() {
    return _scanResults
        .map((line) {
          final parts = line.split(',');
          if (parts.length >= 3) {
            final timestamp = parts[0];
            final format = parts[1];
            final content =
                '"${parts.sublist(2).join(',').replaceAll('"', '""')}"';
            return '"$timestamp","$format",$content';
          }
          return line;
        })
        .join('\n');
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:intl/intl.dart';

import 'package:barcodereader/l10n/app_localizations.dart';
import 'package:barcodereader/model.dart';

class ScannerPage extends StatefulWidget {
  final Function(String result) onScan;
  const ScannerPage({super.key, required this.onScan});
  @override
  State<ScannerPage> createState() => _ScannerPageState();
}

class _ScannerPageState extends State<ScannerPage> {
  late final MobileScannerController _scannerController;
  bool _isScanPaused = false;
  bool _isFlashing = false;
  final Color _flashColor = HSVColor.fromAHSV(1,Model.colorHue.toDouble(),0.5,1).toColor();

  @override
  void initState() {
    super.initState();
    _scannerController = _createScannerController();
    _setOrientation();
  }

  MobileScannerController _createScannerController() {
    final BarcodeFormat? selectedFormat = _selectedBarcodeFormat();
    final List<BarcodeFormat> formats = selectedFormat == null
        ? const <BarcodeFormat>[]
        : <BarcodeFormat>[selectedFormat];
    return MobileScannerController(formats: formats);
  }

  BarcodeFormat? _selectedBarcodeFormat() {
    final selection = Model.selectedBarcodeFormat;
    if (selection == 'all') {
      return null;
    }
    try {
      return BarcodeFormat.values
          .firstWhere((format) => format.name == selection);
    } catch (_) {
      return null;
    }
  }

  void _setOrientation() {
    if (Model.lockOrientation) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
    }
  }

  @override
  void dispose() {
    _scannerController.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final mediaSize = MediaQuery.of(context).size;
    final scanWindow = _calculateScanWindow(mediaSize, Model.scanAreaRestrictionLevel);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: _isFlashing ? _flashColor : Colors.grey[700],
        foregroundColor: Colors.black,
        actions: [
          IconButton(
            icon: ValueListenableBuilder<MobileScannerState>(
              valueListenable: _scannerController,
              builder: (context, state, child) {
                switch (state.torchState) {
                  case TorchState.off:
                    return Icon(
                      Icons.flash_off,
                      color: _isFlashing ? Colors.grey : Colors.white,
                    );
                  case TorchState.on:
                    return const Icon(Icons.flash_on, color: Colors.yellow);
                  default:
                    return const Icon(Icons.no_flash, color: Colors.grey);
                }
              },
            ),
            onPressed: () => _scannerController.toggleTorch(),
          ),
          IconButton(
            icon: const Icon(Icons.flip_camera_ios),
            onPressed: () => _scannerController.switchCamera(),
          ),
        ],
      ),
      body: Stack(children: [
        MobileScanner(
          scanWindow: scanWindow,
          controller: _scannerController,
          onDetect: (capture) {
            if (_isScanPaused) {
              return;
            }
            final List<Barcode> barcodes = capture.barcodes;
            if (barcodes.isNotEmpty) {
              final Barcode? matchingBarcode =
                  _pickMatchingBarcode(barcodes);
              if (matchingBarcode == null) {
                return;
              }
              if (mounted) {
                setState(() {
                  _isScanPaused = true;
                });
              }
              final String code = matchingBarcode.rawValue ??
                  AppLocalizations.of(context)!.noData;
              final String format = matchingBarcode.format.name;
              final String result =
                  '${DateFormat('yyyyMMdd_HHmmss').format(DateTime.now())},$format,$code';
              widget.onScan(result);
              if (Model.beepOnScan && Model.beepVolume > 0.0) {
                final soundId = Model.selectedBeepSound;
                final audio = AudioPlayer();
                audio.setVolume(Model.beepVolume);
                audio.play(AssetSource('sound/beep$soundId.mp3'));
              }
              if (Model.flashOnScan) {
                if (mounted) {
                  setState(() {
                    _isFlashing = true;
                  });
                }
              }
              if (Model.snackBarTime > 0) {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(
                    content: Text(code),
                    duration: Duration(milliseconds: (Model.snackBarTime * 1000).toInt()),
                  ),
                );
              }
              if (Model.continuousScan) {
                if (Model.flashOnScan) {
                  Timer(const Duration(milliseconds: 250), () {
                    if (mounted) {
                      setState(() {
                        _isFlashing = false;
                      });
                    }
                  });
                }
                final interval = (Model.scanInterval * 1000).toInt();
                Timer(Duration(milliseconds: interval), () {
                  if (mounted) {
                    setState(() {
                      _isScanPaused = false;
                    });
                  }
                });
              } else {
                final popDelay = Model.flashOnScan ? 200 : 50;
                Timer(Duration(milliseconds: popDelay), () {
                  if (mounted) {
                    Navigator.of(context).pop();
                  }
                });
              }
            }
          }),
        if (scanWindow != null) _buildScanWindowOverlay(context, scanWindow),
      ]),
    );
  }

  Rect? _calculateScanWindow(Size size, int restrictionLevel) {
    if (restrictionLevel <= 0) {
      return null;
    }
    double heightDivisor;
    double widthFactor;
    switch (restrictionLevel) {
      case 1:
        heightDivisor = 1;
        widthFactor = 1;
        break;
      case 2:
        heightDivisor = 1.5;
        widthFactor = 1;
        break;
      case 3:
        heightDivisor = 2;
        widthFactor = 1;
        break;
      case 4:
        heightDivisor = 2.5;
        widthFactor = 1;
        break;
      case 5:
        heightDivisor = 3;
        widthFactor = 1;
        break;
      case 6:
        heightDivisor = 3.5;
        widthFactor = 1;
        break;
      case 7:
        heightDivisor = 4;
        widthFactor = 1;
        break;
      case 8:
        heightDivisor = 4.5;
        widthFactor = 1;
        break;
      default:
        return null;
    }
    final double width = size.width * widthFactor;
    final double height = size.width / heightDivisor;
    final Offset center = Offset(size.width / 2, size.height / 3);
    final double clampedWidth = width.clamp(0.0, size.width);
    final double clampedHeight = height.clamp(0.0, size.height);
    return Rect.fromCenter(
      center: center,
      width: clampedWidth,
      height: clampedHeight,
    );
  }

  Widget _buildScanWindowOverlay(BuildContext context, Rect scanWindow) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final screenWidth = constraints.maxWidth;
        final screenHeight = constraints.maxHeight;
        return CustomPaint(
          size: Size(screenWidth, screenHeight),
          painter: _ScanWindowOverlayPainter(scanWindow),
        );
      },
    );
  }

  Barcode? _pickMatchingBarcode(List<Barcode> barcodes) {
    final selection = Model.selectedBarcodeFormat;
    if (selection == 'all' || selection.isEmpty) {
      return barcodes.first;
    }
    for (final barcode in barcodes) {
      if (barcode.format.name == selection) {
        return barcode;
      }
    }
    // Unknown selection: fall back to the first detected barcode.
    return barcodes.first;
  }
}

// CustomPainter を使用してオーバーレイを描画
class _ScanWindowOverlayPainter extends CustomPainter {
  final Rect scanWindow;
  _ScanWindowOverlayPainter(this.scanWindow);
  @override
  void paint(Canvas canvas, Size size) {
    final screenRect = Rect.fromLTWH(0, 0, size.width, size.height);
    // 暗くする色
    final paint = Paint()..color = Colors.black.withValues(alpha: 0.6);
    // Path を作成し、画面全体からスキャンウィンドウの領域をくり抜く
    final Path path = Path()
      ..addRect(screenRect) // 画面全体の矩形を追加
      ..addRect(scanWindow); // スキャンウィンドウの矩形を追加(これからくり抜く)
    // Path.fillType を EvenOdd に設定すると、重なり合う領域がくり抜かれる
    path.fillType = PathFillType.evenOdd;
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant _ScanWindowOverlayPainter oldDelegate) {
    return oldDelegate.scanWindow != scanWindow;
  }

}
/// Copyright© ao-system, Inc.

import 'package:flutter/material.dart';

import 'package:barcodereader/theme_color.dart';
import 'package:barcodereader/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,
      ),
    );
  }
}
/// Copyright© ao-system, Inc.

import 'dart:async';
import "dart:io";
import "package:app_settings/app_settings.dart";
import 'package:audioplayers/audioplayers.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:mobile_scanner/mobile_scanner.dart';

import "package:barcodereader/setting_card.dart";
import 'package:barcodereader/l10n/app_localizations.dart';
import 'package:barcodereader/ad_banner_widget.dart';
import 'package:barcodereader/ad_ump_status.dart';
import 'package:barcodereader/model.dart';
import 'package:barcodereader/loading_screen.dart';
import 'package:barcodereader/theme_color.dart';
import 'package:barcodereader/_secrets.dart';
import 'package:barcodereader/main.dart';
import 'package:barcodereader/att_service.dart';

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

class _SettingPageState extends State<SettingPage> {
  AdUmpState _adUmpState = AdUmpState.initial;
  late final AdUmpService _adUmpService;
  bool _wakelockEnabled = false;
  int _themeNumber = 0;
  late ThemeColor _themeColor;
  String _languageCode = '';
  final _inAppReview = InAppReview.instance;
  bool _isReady = false;
  //
  late bool _continuousScan;
  late double _scanInterval;
  late int _scanAreaRestrictionLevel;
  late String _selectedBarcodeFormat;
  late double _snackBarTime;
  late bool _startScanOnLaunch;
  late bool _beepOnScan;
  late double _beepVolume;
  late int _selectedBeepSound;
  late bool _flashOnScan;
  late bool _lockOrientation;
  late int _colorHue;
  late double _colorSaturation;
  late double _colorBrightness;
  late Color _accentColor;

  final List<BarcodeFormat> _supportedFormats =
    BarcodeFormat.values
      .where((format) => format != BarcodeFormat.unknown && format != BarcodeFormat.all)
      .toList();

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

  void _initState() async {
    //ump
    _adUmpService = AdUmpService();
    await _refreshConsentInfo();
    //model
    _continuousScan = Model.continuousScan;
    _scanInterval = Model.scanInterval;
    _scanAreaRestrictionLevel = Model.scanAreaRestrictionLevel;
    _selectedBarcodeFormat = Model.selectedBarcodeFormat;
    _snackBarTime = Model.snackBarTime;
    _startScanOnLaunch = Model.startScanOnLaunch;
    _beepOnScan = Model.beepOnScan;
    _beepVolume = Model.beepVolume;
    _selectedBeepSound = Model.selectedBeepSound;
    _flashOnScan = Model.flashOnScan;
    _lockOrientation = Model.lockOrientation;
    _colorHue = Model.colorHue;
    _colorSaturation = Model.colorSaturation;
    _colorBrightness = Model.colorBrightness;
    _wakelockEnabled = Model.wakelockEnabled;
    _themeNumber = Model.themeNumber;
    _languageCode = Model.languageCode;
    _accentColor = _getRainbowAccentColor(_colorHue, _colorSaturation, _colorBrightness);
    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 _onApply() async {
    await Model.setContinuousScan(_continuousScan);
    await Model.setScanInterval(_scanInterval);
    await Model.setScanAreaRestrictionLevel(_scanAreaRestrictionLevel);
    await Model.setSelectedBarcodeFormat(_selectedBarcodeFormat);
    await Model.setSnackBarTime(_snackBarTime);
    await Model.setStartScanOnLaunch(_startScanOnLaunch);
    await Model.setBeepOnScan(_beepOnScan);
    await Model.setBeepVolume(_beepVolume);
    await Model.setSelectedBeepSound(_selectedBeepSound);
    await Model.setFlashOnScan(_flashOnScan);
    await Model.setLockOrientation(_lockOrientation);
    await Model.setColorHue(_colorHue);
    await Model.setColorSaturation(_colorSaturation);
    await Model.setColorBrightness(_colorBrightness);
    await Model.setWakelockEnabled(_wakelockEnabled);
    await Model.setThemeNumber(_themeNumber);
    await Model.setLanguageCode(_languageCode);
    if (mounted) {
      Navigator.of(context).pop(true);
    }
  }

  String _scanAreaRestrictionLabel(AppLocalizations l, int value) {
    return value <= 0 ? l.scanAreaRestrictionNone : value.toString();
  }

  Color _getRainbowAccentColor(int hue, double saturation, double brightness) {
    return HSVColor.fromAHSV(1.0, hue.toDouble(), saturation, brightness).toColor();
  }

  @override
  Widget build(BuildContext context) {
    if (!_isReady) {
      return LoadingScreen();
    }
    final AppLocalizations l = AppLocalizations.of(context)!;
    final TextTheme t = Theme.of(context).textTheme;
    return Scaffold(
      backgroundColor: _themeColor.backColor,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        title: Text(l.setting),
        centerTitle: true,
        leading: IconButton(
          icon: const Icon(Icons.close),
          onPressed: () => Navigator.of(context).pop(),
        ),
        actions: [
          IconButton(icon: const Icon(Icons.check), onPressed: _onApply),
          const SizedBox(width: 10),
        ],
      ),
      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: [
                    _buildContinuousScan(l, t),
                    _buildLimitAreaScan(l, t),
                    _buildBarcodeFormatSelector(l, t),
                    _buildSnackBarTime(l, t),
                    _buildLaunch(l, t),
                    _buildBeep(l, t),
                    _buildOrientation(l, t),
                    _buildWakelockEnabled(l,t),
                    _buildColorScheme(l, t),
                    _buildTheme(l, t),
                    _buildLanguage(l, t),
                    _buildReview(l, t),
                    _buildCmp(l, t),
                    _buildAtt(l, t),
                  ]),
                ),
              ),
            ),
          ),
        ]),
      ),
      bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
    );
  }

  Widget _buildContinuousScan(AppLocalizations l, TextTheme t) {
    return Column(children:[
      SettingCard.top(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(l.continuousScan, style: t.bodyMedium),
          trailing: Switch(
            value: _continuousScan,
            onChanged: (value) => setState(() => _continuousScan = value),
          ),
        )
      ),
      SettingCard.bottom(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(
            l.scanInterval(_scanInterval.toStringAsFixed(1)),
            style: t.bodyMedium,
          ),
          subtitle: Slider(
            value: _scanInterval,
            min: 0.0,
            max: 3.0,
            divisions: 15,
            label: '${_scanInterval.toStringAsFixed(1)}s',
            onChanged: (value) {
              setState(() {
                _scanInterval = value;
              });
            },
          ),
        )
      )
    ]);
  }

  Widget _buildLimitAreaScan(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(
          '${l.scanAreaRestriction}: ${_scanAreaRestrictionLabel(l, _scanAreaRestrictionLevel)}',
          style: t.bodyMedium,
        ),
        subtitle: Slider(
          value: _scanAreaRestrictionLevel.toDouble(),
          min: 0,
          max: 8,
          divisions: 8,
          label: _scanAreaRestrictionLabel(l, _scanAreaRestrictionLevel),
          onChanged: (value) {
            setState(() {
              _scanAreaRestrictionLevel = value.round();
            });
          },
        ),
      ),
    );
  }

  Widget _buildBarcodeFormatSelector(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(
          l.detectableBarcodeFormat,
          style: t.bodyMedium,
        ),
        trailing: DropdownButton<String>(
          value: _selectedBarcodeFormat,
          items: [
            const DropdownMenuItem(value: 'all', child: Text('all')),
            ..._supportedFormats.map(
                  (format) => DropdownMenuItem(
                value: format.name,
                child: Text(format.name),
              ),
            ),
          ],
          onChanged: (value) {
            if (value != null) {
              setState(() => _selectedBarcodeFormat = value);
            }
          },
        ),
      ),
    );
  }


  Widget _buildSnackBarTime(AppLocalizations l, TextTheme t) {
    return SettingCard(
      child: ListTile(
        contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        title: Text(
          l.snackBarTime,
          style: t.bodyMedium,
          softWrap: true,
          overflow: TextOverflow.visible,
        ),
        subtitle: Row(
          children: [
            Text(_snackBarTime.toStringAsFixed(1)),
            Expanded(
              child: Slider(
                value: _snackBarTime,
                min: 0.0,
                max: 10.0,
                divisions: 20,
                label: _snackBarTime.toStringAsFixed(1),
                onChanged: (double value) {
                  setState(() {
                    _snackBarTime = value;
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

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

  Widget _buildBeep(AppLocalizations l, TextTheme t) {
    return Column(children:[
      SettingCard.top(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(l.beepOnScan, style: t.bodyMedium),
          trailing: Switch(
            value: _beepOnScan,
            onChanged: (value) => setState(() => _beepOnScan = value),
          ),
        )
      ),
      SettingCard.flat(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(
            l.beepVolume,
            style: t.bodyMedium,
          ),
          subtitle: Row(
            children: [
              Text(_beepVolume.toStringAsFixed(1)),
              Expanded(
                child: Slider(
                  value: _beepVolume,
                  min: 0.0,
                  max: 1.0,
                  divisions: 10,
                  label: _beepVolume.toStringAsFixed(1),
                  onChanged: (value) {
                    setState(() => _beepVolume = value);
                  },
                ),
              ),
            ],
          ),
        )
      ),
      SettingCard.flat(
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(l.beepSound, style: t.bodyMedium),
          trailing: DropdownButton<int>(
            value: _selectedBeepSound,
            items: List.generate(14, (index) {
              final soundNum = index + 1;
              return DropdownMenuItem(
                value: soundNum,
                child: Text(l.beepSoundName(soundNum.toString())),
              );
            }),
            onChanged: (value) {
              if (value != null) {
                setState(() => _selectedBeepSound = value);
                AudioPlayer().play(AssetSource('sound/beep$value.mp3'));
              }
            },
          ),
        )
      ),
      SettingCard.bottom(
        child :ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(l.flashOnScan, style: t.bodyMedium),
          trailing: Switch(
            value: _flashOnScan,
            onChanged: (value) => setState(() => _flashOnScan = value),
          ),
        )
      ),
    ]);
  }

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

  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 _buildColorScheme(AppLocalizations l, TextTheme t) {
    return Column(children:[
      SettingCard.top(
        // タイトル
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(
            l.colorScheme,
            style: t.bodyMedium,
            softWrap: true,
            overflow: TextOverflow.visible,
          ),
          subtitle: Text(
            l.colorScheme1,
            style: t.bodySmall,
          ),
        )
      ),
      SettingCard.flat(
        // Hue
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(
            'Hue: ${_colorHue.toStringAsFixed(0)}',
            style: t.bodyMedium,
          ),
          subtitle: SliderTheme(
            data: SliderTheme.of(context).copyWith(
              activeTrackColor: _accentColor,
              inactiveTrackColor: _accentColor.withValues(alpha: 0.3),
              thumbColor: _accentColor,
              overlayColor: _accentColor.withValues(alpha: 0.2),
              valueIndicatorColor: _accentColor,
            ),
            child: Slider(
              value: _colorHue.toDouble(),
              min: 0,
              max: 360,
              divisions: 360,
              label: _colorHue.toString(),
              onChanged: (value) {
                setState(() {
                  _colorHue = value.toInt();
                  _accentColor = _getRainbowAccentColor(
                    _colorHue,
                    _colorSaturation,
                    _colorBrightness,
                  );
                });
              },
            ),
          ),
        )
      ),
      SettingCard.flat(
        // Saturation
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(
            'Saturation: ${_colorSaturation.toStringAsFixed(1)}',
            style: t.bodyMedium,
          ),
          subtitle: SliderTheme(
            data: SliderTheme.of(context).copyWith(
              activeTrackColor: _accentColor,
              inactiveTrackColor: _accentColor.withValues(alpha: 0.3),
              thumbColor: _accentColor,
              overlayColor: _accentColor.withValues(alpha: 0.2),
              valueIndicatorColor: _accentColor,
            ),
            child: Slider(
              value: _colorSaturation,
              min: 0.0,
              max: 1.0,
              divisions: 10,
              label: _colorSaturation.toStringAsFixed(1),
              onChanged: (value) {
                setState(() {
                  _colorSaturation = value;
                  _accentColor = _getRainbowAccentColor(
                    _colorHue,
                    _colorSaturation,
                    _colorBrightness,
                  );
                });
              },
            ),
          ),
        )
      ),
      SettingCard.bottom(
        // Brightness
        child: ListTile(
          contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          title: Text(
            'Brightness: ${_colorBrightness.toStringAsFixed(1)}',
            style: t.bodyMedium,
          ),
          subtitle: SliderTheme(
            data: SliderTheme.of(context).copyWith(
              activeTrackColor: _accentColor,
              inactiveTrackColor: _accentColor.withValues(alpha: 0.3),
              thumbColor: _accentColor,
              overlayColor: _accentColor.withValues(alpha: 0.2),
              valueIndicatorColor: _accentColor,
            ),
            child: Slider(
              value: _colorBrightness,
              min: 0.0,
              max: 1.0,
              divisions: 10,
              label: _colorBrightness.toStringAsFixed(1),
              onChanged: (value) {
                setState(() {
                  _colorBrightness = value;
                  _accentColor = _getRainbowAccentColor(
                    _colorHue,
                    _colorSaturation,
                    _colorBrightness,
                  );
                });
              },
            ),
          ),
        ),
      ),
    ]);
  }

  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<AttStatus>(
              future: AttService().getTrackingStatus(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(
                    child: 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 ?? AttStatus.unknown;
                final label = status.name;
                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),
                      ),
                    ],
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }

}
/// Copyright© ao-system, Inc.

import 'package:flutter/material.dart';

import 'package:barcodereader/model.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;

  Color _getRainbowAccentColor(int hue, double saturation, double brightness) {
    return HSVColor.fromAHSV(1, hue.toDouble(), saturation, brightness).toColor();
  }

  //main page
  Color get mainBackColor => _isLight ? Color.fromRGBO(221, 221, 221, 1.0) : Color.fromRGBO(51, 51, 51, 1.0);
  Color get mainForeColor => _isLight ? Color.fromRGBO(34, 34, 34, 1.0) : Color.fromRGBO(221, 221, 221, 1.0);

  Color get mainButtonColor1 => _isLight
      ? _getRainbowAccentColor(Model.colorHue, 0.4 * Model.colorSaturation, 1.0 * Model.colorBrightness)
      : _getRainbowAccentColor(Model.colorHue, 1.0 * Model.colorSaturation, 0.4 * Model.colorBrightness);
  Color get mainButtonColor2 => _isLight
      ? _getRainbowAccentColor(Model.colorHue, 0.5 * Model.colorSaturation, 1.0 * Model.colorBrightness)
      : _getRainbowAccentColor(Model.colorHue, 1.0 * Model.colorSaturation, 0.35 * Model.colorBrightness);
  Color get mainButtonColor3 => _isLight
      ? _getRainbowAccentColor(Model.colorHue, 0.6 * Model.colorSaturation, 1.0 * Model.colorBrightness)
      : _getRainbowAccentColor(Model.colorHue, 1.0 * Model.colorSaturation, 0.3 * Model.colorBrightness);
  Color get mainResultBackColor => _isLight
      ? _getRainbowAccentColor(Model.colorHue, 0.2 * Model.colorSaturation, 1.0 * Model.colorBrightness)
      : _getRainbowAccentColor(Model.colorHue, 1.0 * Model.colorSaturation, 0.2 * Model.colorBrightness);
  //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]!;
}
/// Copyright© ao-system, Inc.

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;
    }
  }
}