name: qranalyzer
description: "qranalyzer"
publish_to: 'none'
version: 1.2.3+10
environment:
sdk: ^3.11.5
dependencies: # flutter pub upgrade --major-versions
flutter:
sdk: flutter
cupertino_icons: ^1.0.8
flutter_localizations: #flutter gen-l10n
sdk: flutter
intl: ^0.20.2
shared_preferences: ^2.3.2
google_mobile_ads: ^8.0.0
camera: ^0.12.0
image: ^4.1.3
zxing_lib: ^1.1.4
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.3 #flutter pub run flutter_launcher_icons
flutter_native_splash: ^2.3.6 #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: '#10005f'
image: 'assets/image/splash.png'
color_dark: '#10005f'
image_dark: 'assets/image/splash.png'
fullscreen: true
android_12:
icon_background_color: '#10005f'
image: 'assets/image/splash.png'
icon_background_color_dark: '#10005f'
image_dark: 'assets/image/splash.png'
flutter:
generate: true
uses-material-design: true
config:
enable-swift-package-manager: true
assets:
- assets/icon/
- assets/image/
/// Copyright© ao-system, Inc.
import 'package:flutter/cupertino.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:qranalyzer/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:qranalyzer/_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:qranalyzer/l10n/app_localizations.dart';
import 'package:qranalyzer/_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 'package:flutter/material.dart';
import 'package:camera/camera.dart';
class CameraPage extends StatefulWidget {
const CameraPage({super.key});
@override
State<CameraPage> createState() => _CameraPageState();
}
class _CameraPageState extends State<CameraPage> {
CameraController? _controller;
bool _isReady = false;
double _currentZoom = 1.0;
double _baseZoom = 1.0;
double _maxZoom = 1.0;
double _minZoom = 1.0;
@override
void initState() {
super.initState();
_initCamera();
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
Future<void> _initCamera() async {
try {
final cameras = await availableCameras();
final camera = cameras.first;
final controller = CameraController(
camera,
ResolutionPreset.medium,
enableAudio: false,
);
await controller.initialize();
_maxZoom = await controller.getMaxZoomLevel();
_minZoom = await controller.getMinZoomLevel();
setState(() {
_controller = controller;
_isReady = true;
});
} catch (e) {
debugPrint("カメラ初期化エラー: $e");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Camera')),
body: _isReady
? GestureDetector(
onScaleStart: (details) {
_baseZoom = _currentZoom;
},
onScaleUpdate: (details) async {
if (_controller == null) return;
final newZoom = (_baseZoom * details.scale)
.clamp(_minZoom, _maxZoom);
await _controller!.setZoomLevel(newZoom);
setState(() {
_currentZoom = newZoom;
});
},
child: CameraPreview(_controller!),
)
: const Center(child: CircularProgressIndicator()),
floatingActionButton: _isReady
? FloatingActionButton(
onPressed: _takePhoto,
child: const Icon(Icons.camera_alt),
)
: null,
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
}
Future<void> _takePhoto() async {
if (_controller == null || !_controller!.value.isInitialized) {
return;
}
try {
final XFile file = await _controller!.takePicture();
Navigator.pop(context, file);
} catch (e) {
debugPrint('撮影エラー: $e');
}
}
}
/// Copyright© ao-system, Inc.
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:image/image.dart' as img;
import 'package:wakelock_plus/wakelock_plus.dart';
import 'package:qranalyzer/instruction_panel.dart';
import 'package:qranalyzer/qr_info.dart';
import 'package:qranalyzer/qr_matrix_painter.dart';
import 'package:qranalyzer/qr_original_painter.dart';
import 'package:qranalyzer/setting_page.dart';
import 'package:qranalyzer/theme_color.dart';
import 'package:qranalyzer/loading_screen.dart';
import 'package:qranalyzer/model.dart';
import 'package:qranalyzer/main.dart';
import 'package:qranalyzer/ad_banner_widget.dart';
import 'package:qranalyzer/qr_region.dart';
import 'package:qranalyzer/legend_panel.dart';
import 'package:qranalyzer/qr_unmasked_painter.dart';
import 'package:qranalyzer/qr_mask_painter.dart';
import 'package:qranalyzer/camera_page.dart';
import 'package:qranalyzer/info_panel.dart';
import 'package:qranalyzer/image_select_page.dart';
import 'package:qranalyzer/qr_processor.dart';
class MainHomePage extends StatefulWidget {
const MainHomePage({super.key});
@override
State<MainHomePage> createState() => _MainHomePageState();
}
class _MainHomePageState extends State<MainHomePage> with WidgetsBindingObserver {
late ThemeColor _themeColor;
bool _isReady = false;
//
final QrProcessor _processor = QrProcessor();
bool _isProcessing = false;
List<List<bool>>? _matrix;
String? _error;
QrInfo? _info;
List<List<QrRegion>>? _regionMap;
List<List<bool>>? _unmasked;
int? _maskPattern;
int _currentBitIndex = 0;
List<Point<int>> _bitOrder = [];
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_initState();
}
void _initState() async {
_themeColor = ThemeColor(themeNumber: Model.themeNumber, context: context);
_wakelock();
if (mounted) {
setState(() {
_isReady = true;
});
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
WakelockPlus.disable();
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> _captureAndAnalyze(XFile photo) async {
//解析開始前にすべての結果をクリアする
setState(() {
_isProcessing = true;
_matrix = null; // QR行列をクリア
_info = null; // 解析情報をクリア
_regionMap = null; // 領域マップをクリア
_unmasked = null; // マスク解除ビットをクリア
_maskPattern = null; // マスクパターンをクリア
_error = null; // エラーメッセージをクリア
});
try {
final bytes = await photo.readAsBytes();
img.Image? image = img.decodeImage(bytes);
if (image == null) {
throw Exception("Decode failed");
}
if (image.width > 1000) {
image = img.copyResize(image, width: 1000, interpolation: img.Interpolation.nearest);
}
// 1. ビット抽出
final (matrix, info) = await _processor.extractRawPattern(image);
// 2. 領域分類
final regionMap = _processor.classifyModules(info.version, info.dataCodewords, info.eccCodewords);
// 3. マスク解除
final unmasked = _processor.unmaskMatrix(matrix, regionMap, info.maskPattern);
if (!mounted) {
return;
}
// 4. 解析完了後に新しい結果をセットする
setState(() {
_matrix = matrix;
_info = info;
_regionMap = regionMap;
_unmasked = unmasked;
_maskPattern = info.maskPattern;
_bitOrder = _processor.getBitOrder(info.version, regionMap);
});
} catch (e) {
if (!mounted) {
return;
}
setState(() {
final String errorStr = e.toString();
//メッセージをカスタマイズ
if (errorStr.contains('NotFoundException')) {
_error = 'No QR code found.';
} else if (errorStr.contains('Format not found')) {
_error = 'QR code detected, but data reading failed.';
} else {
_error = 'Analysis failed.';
}
});
} finally {
if (mounted) {
setState(() => _isProcessing = false);
}
}
}
Future<void> _openSetting() 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 == false) {
return const LoadingScreen();
}
return Scaffold(
backgroundColor: _themeColor.mainBackColor,
body: Stack(children:[
_buildBackground(),
SafeArea(
child: Column(
children: [
_buildAppBar(),
_buildButton(),
const SizedBox(height: 16),
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.all(8),
child: Column(
children: [
_buildMatrixWidget(),
_buildQrUnmaskedPainterAndSeekBar(),
InfoPanel(info: _info),
InstructionPanel(),
]
)
)
)
]
)
)
]),
bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager)
);
}
Widget _buildBackground() {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [_themeColor.mainBack2Color, _themeColor.mainBackColor],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
image: DecorationImage(
image: AssetImage('assets/image/tile.png'),
repeat: ImageRepeat.repeat,
opacity: 0.1,
),
),
);
}
Widget _buildAppBar() {
final t = Theme.of(context).textTheme;
return SizedBox(
height: 52,
child: Row(
children: [
const SizedBox(width: 16),
Text('QR Analyzer', style: t.titleSmall?.copyWith(color: _themeColor.mainForeColor)),
const Spacer(),
IconButton(
onPressed: _openSetting,
icon: Icon(Icons.settings,color: _themeColor.mainForeColor.withValues(alpha: 0.6)),
),
],
)
);
}
Widget _buildButton() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
children: [
Expanded(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
elevation: 0,
side: const BorderSide(color: Colors.grey, width: 1),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
padding: const EdgeInsets.symmetric(vertical: 6),
),
onPressed: _isProcessing ? null : () async {
final XFile? photo = await Navigator.push(
context,
MaterialPageRoute(builder: (_) => const CameraPage()),
);
if (photo != null) _captureAndAnalyze(photo);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.camera_alt),
const SizedBox(width: 8),
Text(_isProcessing ? 'Analyzing...' : 'Capture\nQR code'),
],
),
),
),
const SizedBox(width: 6),
Expanded(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
elevation: 0,
side: const BorderSide(color: Colors.grey, width: 1),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
padding: const EdgeInsets.symmetric(vertical: 6),
),
onPressed: _isProcessing ? null : () async {
final XFile? photo = await Navigator.push(
context,
MaterialPageRoute(builder: (_) => const ImageSelectPage()),
);
if (photo != null) _captureAndAnalyze(photo);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.image_search),
const SizedBox(width: 8),
Text(_isProcessing ? 'Analyzing...' : 'Choose\nimage'),
],
),
),
),
],
),
);
}
Widget _buildMatrixWidget() {
return Column(children: [
if (_matrix != null) ...[
_buildQrOriginalPainter(),
const LegendPanel(),
_buildQrMatrixPainter(), // QR本体
_buildQrMaskPainter(), //マスクパターン
]
else if (_error != null)
Container(
margin: const EdgeInsets.only(left: 12, right: 12),
child: Text(_error!),
)
else
const SizedBox.shrink()
]);
}
Widget _buildQrOriginalPainter() {
if (_matrix == null || _regionMap == null) {
return const SizedBox.shrink();
}
return Padding(
padding: const EdgeInsets.only(left: 12, right: 12, top: 8),
child: AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, constraints) {
final size = constraints.biggest; // 最大サイズを取得
return CustomPaint(
size: size,
painter: QrOriginalPainter(
_matrix!,
_regionMap!,
),
);
},
),
),
);
}
Widget _buildQrMatrixPainter() {
if (_matrix == null || _regionMap == null) {
return const SizedBox.shrink();
}
return Padding(
padding: const EdgeInsets.only(left: 12, right: 12, top: 8),
child: AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, constraints) {
final size = constraints.biggest; // 最大サイズを取得
return CustomPaint(
size: size,
painter: QrMatrixPainter(
_matrix!,
_regionMap!,
),
);
},
),
),
);
}
Widget _buildQrMaskPainter() {
return Padding(
padding: const EdgeInsets.only(left: 12, right: 12, top: 8),
child: AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, constraints) {
final size = constraints.biggest;
return CustomPaint(
size: size,
painter: QrMaskPainter(
_maskPattern!,
_matrix!.length,
),
);
},
),
),
);
}
Widget _buildQrUnmaskedPainterAndSeekBar() {
if (_unmasked == null) {
return const SizedBox.shrink();
}
return Column(children:[
_buildQrUnmaskedPainter(),
_buildSeekBar()
]);
}
Widget _buildQrUnmaskedPainter() {
if (_unmasked == null || _regionMap == null || _bitOrder.isEmpty || _info == null) {
return const SizedBox.shrink();
}
// データ領域のビット総数を計算
final int dataBitLimit = _info!.dataCodewords * 8;
return Padding(
padding: const EdgeInsets.only(left: 12, right: 12, top: 8),
child: AspectRatio(
aspectRatio: 1,
child: LayoutBuilder(
builder: (context, constraints) {
final size = constraints.biggest;
return CustomPaint(
size: size,
painter: QrUnmaskedPainter(
_unmasked!,
_regionMap!,
_bitOrder,
_currentBitIndex,
dataBitLimit,
),
);
},
),
),
);
}
Widget _buildSeekBar() {
if (_bitOrder.isEmpty || _info == null || _unmasked == null) {
return const SizedBox.shrink();
}
final int dataBitLimit = _info!.dataCodewords * 8;
final bool isDataRegion = _currentBitIndex < dataBitLimit;
final currentPoint = _bitOrder[_currentBitIndex];
final Color regionColor = isDataRegion ? Colors.blue : Colors.red;
// 現在のビット値を取得 (true -> 1, false -> 0)
final bool isDark = _unmasked![currentPoint.y][currentPoint.x];
final String bitValue = isDark ? "1" : "0";
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Column(
children: [
// 情報表示行
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// 座標とビット値
Row(
children: [
const Icon(Icons.location_on, size: 14, color: Colors.grey),
const SizedBox(width: 2),
Text(
"(${currentPoint.x},${currentPoint.y})",
style: const TextStyle(fontFamily: 'monospace'),
),
const SizedBox(width: 8),
// ビット値表示バッジ
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: isDark ? Colors.white.withValues(alpha: 0.5) : Colors.black.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.grey.withValues(alpha: 0.5)),
),
child: Text(
"Bit:$bitValue",
style: TextStyle(
color: isDark ? Colors.black : Colors.white,
fontFamily: 'monospace',
fontWeight: FontWeight.bold,
fontSize: 10,
),
),
),
],
),
// 領域ラベル
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: regionColor.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: regionColor.withValues(alpha: 0.5)),
),
child: Text(
isDataRegion ? "DATA" : "ECC",
style: TextStyle(color: regionColor, fontWeight: FontWeight.bold, fontSize: 10),
),
),
// インデックス
Text(
"Index:$_currentBitIndex",
style: const TextStyle(fontSize: 12, fontFamily: 'monospace'),
),
],
),
// シークバー
SliderTheme(
data: SliderTheme.of(context).copyWith(
trackHeight: 4,
),
child: Slider(
value: _currentBitIndex.toDouble(),
min: 0,
max: (_bitOrder.length - 1).toDouble(),
onChanged: (v) => setState(() => _currentBitIndex = v.toInt()),
),
),
],
),
);
}
}
/// Copyright© ao-system, Inc.
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image/image.dart' as img;
class ImageSelectPage extends StatefulWidget {
const ImageSelectPage({super.key});
@override
State<ImageSelectPage> createState() => _ImageSelectPageState();
}
class _ImageSelectPageState extends State<ImageSelectPage> {
XFile? _selectedImage;
bool _isProcessing = false;
int? _imgWidth;
int? _imgHeight;
Future<void> _pickImage() async {
if (kIsWeb) return;
final picker = ImagePicker();
final XFile? image = await picker.pickImage(source: ImageSource.gallery);
if (image == null) return;
// Read image size
final bytes = await image.readAsBytes();
final decoded = img.decodeImage(bytes);
if (decoded != null) {
_imgWidth = decoded.width;
_imgHeight = decoded.height;
}
setState(() {
_selectedImage = image;
});
}
Future<void> _returnImage() async {
if (_selectedImage == null) return;
setState(() => _isProcessing = true);
try {
Navigator.pop(context, _selectedImage);
} finally {
setState(() => _isProcessing = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Select Image"),
centerTitle: true,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
Row(
children: [
Expanded(
child: FilledButton.icon(
onPressed: _isProcessing ? null : _pickImage,
icon: const Icon(Icons.photo_library_outlined),
label: const Text("Choose Image"),
),
),
const SizedBox(width: 8),
Expanded(
child: FilledButton.icon(
onPressed: _selectedImage == null || _isProcessing
? null
: _returnImage,
icon: const Icon(Icons.check),
label: const Text("Confirm"),
),
),
],
),
const SizedBox(height: 16),
if (_isProcessing) const LinearProgressIndicator(),
const SizedBox(height: 16),
Expanded(
child: _selectedImage == null
? const Center(child: Text("No image selected"))
: Column(
children: [
Expanded(
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Image.file(
File(_selectedImage!.path),
fit: BoxFit.contain,
),
),
),
if (_imgWidth != null && _imgHeight != null)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
"Image size: ${_imgWidth} × ${_imgHeight}",
style: const TextStyle(fontSize: 14),
),
),
],
),
),
],
),
),
),
);
}
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
import 'package:qranalyzer/qr_info.dart';
import 'package:qranalyzer/l10n/app_localizations.dart';
class InfoPanel extends StatelessWidget {
final QrInfo? info;
const InfoPanel({super.key, required this.info});
@override
Widget build(BuildContext context) {
if (info == null) {
return const SizedBox.shrink();
}
final l = AppLocalizations.of(context)!;
return Column(children:[
Card(
elevation: 0,
margin: const EdgeInsets.only(left: 12, right: 12, top: 12),
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Decoded Result: ${info!.decodedText ?? ''}"),
Text("Version: ${info!.version}"),
Text("Error Correction Level: ${info!.ecLevel}"),
Text("Mask Pattern: ${info!.maskPattern}"),
Text("Size: ${info!.size} × ${info!.size}"),
Text("Data Codewords: ${info!.dataCodewords}"),
Text("ECC Codewords: ${info!.eccCodewords}"),
],
)
)
),
Card(
margin: const EdgeInsets.only(left: 12, right: 12, top: 12),
elevation: 0,
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(l.infoImageFirst),
const SizedBox(height: 12),
Text(l.infoImageSecond),
const SizedBox(height: 12),
Text(l.infoImageThird),
const SizedBox(height: 12),
Text(l.infoImageFourth),
],
)
)
),
]);
}
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
import 'package:qranalyzer/l10n/app_localizations.dart';
class InstructionPanel extends StatelessWidget {
const InstructionPanel({super.key});
@override
Widget build(BuildContext context) {
final l = AppLocalizations.of(context)!;
return Column(children:[
_buildCard("Version:\n${l.infoVersion}"),
_buildCard("Error Correction Level:\n${l.infoECL}"),
_buildCard("Finder Pattern:\n${l.infoFinderPattern}"),
_buildCard("Alignment Pattern:\n${l.infoAlignmentPattern}"),
_buildCard("Timing Pattern:\n${l.infoTimingPattern}"),
_buildCard("Format Info:\n${l.infoFormatInfo}"),
_buildCard("Version Info:\n${l.infoVersionInfo}"),
_buildCard("Data:\n${l.infoData}"),
_buildCard("ECC: Error Correction Code\n${l.infoECC}"),
_buildCard("Unused:\n${l.infoUnused}"),
_buildCard("Dark Module:\n${l.infoDarkModule}"),
const SizedBox(height: 100),
]);
}
Widget _buildCard(String text) {
return Card(
margin: const EdgeInsets.only(left: 12, right: 12, top: 12),
elevation: 0,
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(text),
],
)
)
);
}
}
/// Copyright© ao-system, Inc.
bool isMasked(int maskPattern, int x, int y) {
switch (maskPattern) {
case 0:
return (y + x) % 2 == 0;
case 1:
return y % 2 == 0;
case 2:
return x % 3 == 0;
case 3:
return (y + x) % 3 == 0;
case 4:
return ((y ~/ 2) + (x ~/ 3)) % 2 == 0;
case 5:
return ((y * x) % 2 + (y * x) % 3) == 0;
case 6:
return (((y * x) % 2) + ((y * x) % 3)) % 2 == 0;
case 7:
return (((y + x) % 2) + ((y * x) % 3)) % 2 == 0;
default:
return false;
}
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
class LegendItem extends StatelessWidget {
final Color color;
final String label;
const LegendItem({super.key, required this.color, required this.label});
@override
Widget build(BuildContext context) {
return Row(
children: [
Container(
width: 16,
height: 16,
color: color,
),
const SizedBox(width: 8),
Text(label),
],
);
}
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
import 'package:qranalyzer/legend_item.dart';
class LegendPanel extends StatelessWidget {
const LegendPanel({super.key});
@override
Widget build(BuildContext context) {
return Card(
elevation: 0,
margin: const EdgeInsets.only(left: 12, right: 12, top: 8),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 4),
child: GridView.count(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
crossAxisCount: 2,
mainAxisSpacing: 1,
crossAxisSpacing: 0,
childAspectRatio: 6,
children: const [
LegendItem(color: Colors.green, label: "Finder Pattern"),
LegendItem(color: Colors.cyan, label: "Alignment Pattern"),
LegendItem(color: Colors.yellow, label: "Timing Pattern"),
LegendItem(color: Colors.purple, label: "Format Info"),
LegendItem(color: Colors.orange, label: "Version Info"),
LegendItem(color: Colors.blue, label: "Data"),
LegendItem(color: Colors.red, label: "ECC"),
LegendItem(color: Colors.white, label: "Unused"),
LegendItem(color: Colors.black, label: "Dark Module"),
],
),
),
);
}
}
/// 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:google_mobile_ads/google_mobile_ads.dart';
import "package:qranalyzer/home_page.dart";
import 'package:qranalyzer/l10n/app_localizations.dart';
import 'package:qranalyzer/loading_screen.dart';
import 'package:qranalyzer/model.dart';
import 'package:qranalyzer/parse_locale_tag.dart';
import 'package:qranalyzer/theme_mode_number.dart';
import 'package:qranalyzer/ad_ump_status.dart';
import 'package:qranalyzer/att_service.dart';
import 'package:qranalyzer/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(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.schemeColor);
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:qranalyzer/l10n/app_localizations.dart';
class Model {
Model._();
static const String _prefWakelockEnabled = 'wakelockEnabled';
static const String _prefSchemeColor = 'schemeColor';
static const String _prefThemeNumber = 'themeNumber';
static const String _prefLanguageCode = 'languageCode';
static bool _ready = false;
static bool _wakelockEnabled = false;
static int _schemeColor = 260;
static int _themeNumber = 0;
static String _languageCode = '';
static bool get wakelockEnabled => _wakelockEnabled;
static int get schemeColor => _schemeColor;
static int get themeNumber => _themeNumber;
static String get languageCode => _languageCode;
static Future<void> ensureReady() async {
if (_ready) {
return;
}
final SharedPreferences prefs = await SharedPreferences.getInstance();
//
_wakelockEnabled = prefs.getBool(_prefWakelockEnabled) ?? false;
_schemeColor = (prefs.getInt(_prefSchemeColor) ?? 250).clamp(0, 360);
_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> setWakelockEnabled(bool value) async {
_wakelockEnabled = value;
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool(_prefWakelockEnabled, value);
}
static Future<void> setSchemeColor(int value) async {
_schemeColor = value;
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt(_prefSchemeColor, 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.
class QrInfo {
final int version;
final String ecLevel;
final int maskPattern;
final int size;
final int dataCodewords;
final int eccCodewords;
final String? decodedText;
QrInfo({
required this.version,
required this.ecLevel,
required this.maskPattern,
required this.size,
required this.dataCodewords,
required this.eccCodewords,
required this.decodedText,
});
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
import 'package:qranalyzer/is_masked.dart';
class QrMaskPainter extends CustomPainter {
final int maskPattern;
final int size;
QrMaskPainter(this.maskPattern, this.size);
@override
void paint(Canvas canvas, Size s) {
final cell = s.width / size;
final paintMask = Paint()..color = Colors.grey.withValues(alpha: 0.5);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (isMasked(maskPattern, x, y)) {
final rect = Rect.fromLTWH(x * cell, y * cell, cell, cell);
canvas.drawRect(rect, paintMask);
}
}
}
}
@override
bool shouldRepaint(_) => true;
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
import 'package:qranalyzer/qr_region.dart';
class QrMatrixPainter extends CustomPainter {
final List<List<bool>> matrix;
final List<List<QrRegion>> regionMap;
QrMatrixPainter(this.matrix, this.regionMap);
@override
void paint(Canvas canvas, Size size) {
final paintData = Paint()..color = Colors.blue;
final paintECC = Paint()..color = Colors.red;
final paintOther = Paint()..color = Colors.white;
final paintDark = Paint()..color = Colors.black;
final paintFinder = Paint()..color = Colors.green;
final paintTiming = Paint()..color = Colors.yellow;
final paintFormat = Paint()..color = Colors.purple;
final paintVersion = Paint()..color = Colors.orange;
final paintAlignment = Paint()..color = Colors.cyan;
final n = matrix.length;
final cell = size.width / n;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
final rect = Rect.fromLTWH(x * cell, y * cell, cell, cell);
Paint bg;
switch (regionMap[y][x]) {
case QrRegion.data:
bg = paintData;
break;
case QrRegion.ecc:
bg = paintECC;
break;
case QrRegion.finder:
bg = paintFinder;
break;
case QrRegion.timing:
bg = paintTiming;
break;
case QrRegion.format:
bg = paintFormat;
break;
case QrRegion.version:
bg = paintVersion;
break;
case QrRegion.alignment:
bg = paintAlignment;
break;
default:
bg = paintOther;
}
canvas.drawRect(rect, matrix[y][x] ? paintDark : bg);
}
}
}
@override
bool shouldRepaint(_) => true;
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
import 'package:qranalyzer/qr_region.dart';
class QrOriginalPainter extends CustomPainter {
final List<List<bool>> matrix;
final List<List<QrRegion>> regionMap;
QrOriginalPainter(this.matrix, this.regionMap);
@override
void paint(Canvas canvas, Size size) {
final paintWhite = Paint()..color = Colors.white;
final paintBlack = Paint()..color = Colors.black;
final n = matrix.length;
final cell = size.width / n;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
final rect = Rect.fromLTWH(x * cell, y * cell, cell, cell);
Paint bg = paintBlack;
switch (regionMap[y][x]) {
case QrRegion.data:
case QrRegion.ecc:
case QrRegion.finder:
case QrRegion.timing:
case QrRegion.format:
case QrRegion.version:
case QrRegion.alignment:
case QrRegion.unused:
bg = paintWhite;
}
canvas.drawRect(rect, matrix[y][x] ? paintBlack : bg);
}
}
}
@override
bool shouldRepaint(_) => true;
}
/// Copyright© ao-system, Inc.
import 'dart:math';
import 'dart:typed_data';
import 'package:image/image.dart' as img;
import 'package:zxing_lib/zxing.dart';
import 'package:zxing_lib/qrcode.dart';
import 'package:zxing_lib/common.dart';
import 'package:qranalyzer/qr_info.dart';
import 'package:qranalyzer/qr_region.dart';
import 'package:qranalyzer/qr_spec.dart';
import 'package:qranalyzer/is_masked.dart';
class QrProcessor {
static const Map<int, List<int>> _alignmentTable = {
2: [6, 18],
3: [6, 22],
4: [6, 26],
5: [6, 30],
6: [6, 34],
7: [6, 22, 38],
8: [6, 24, 42],
9: [6, 26, 46],
10: [6, 28, 50],
11: [6, 30, 54],
12: [6, 32, 58],
13: [6, 34, 62],
14: [6, 26, 46, 66],
15: [6, 26, 48, 70],
16: [6, 26, 50, 74],
17: [6, 30, 54, 78],
18: [6, 30, 56, 82],
19: [6, 30, 58, 86],
20: [6, 34, 62, 90],
21: [6, 28, 50, 72, 94],
22: [6, 26, 50, 74, 98],
23: [6, 30, 54, 78, 102],
24: [6, 28, 54, 80, 106],
25: [6, 32, 58, 84, 110],
26: [6, 30, 58, 86, 114],
27: [6, 34, 62, 90, 118],
28: [6, 26, 50, 74, 98, 122],
29: [6, 30, 54, 78, 102, 126],
30: [6, 26, 52, 78, 104, 130],
31: [6, 30, 56, 82, 108, 134],
32: [6, 34, 60, 86, 112, 138],
33: [6, 30, 58, 86, 114, 142],
34: [6, 34, 62, 90, 118, 146],
35: [6, 30, 54, 78, 102, 126, 150],
36: [6, 24, 50, 76, 102, 128, 154],
37: [6, 28, 54, 80, 106, 132, 158],
38: [6, 32, 58, 84, 110, 136, 162],
39: [6, 26, 54, 82, 110, 138, 166],
40: [6, 30, 58, 86, 114, 142, 170],
};
/// メイン解析フロー (前処理リトライ機能付き)
Future<(List<List<bool>> matrix, QrInfo info)> extractRawPattern(img.Image originalImage) async {
Object? lastError;
for (int attempt = 0; attempt < 3; attempt++) {
try {
img.Image processed = _applyPreprocess(originalImage, attempt);
final Int32List argb = _convertToArgb(processed);
final source = RGBLuminanceSource(processed.width, processed.height, argb);
final bitmap = BinaryBitmap(HybridBinarizer(source));
// 検出の実行
final detectorResult = Detector(bitmap.blackMatrix).detect();
final bm = detectorResult.bits;
final int size = bm.width;
final matrix = List.generate(
size, (y) => List.generate(size, (x) => bm.get(x, y)),
);
// フォーマット情報の読み取り
final int raw1 = _readFormatLocation1(matrix);
final int raw2 = _readFormatLocation2(matrix);
// zxing_lib の標準デコーダーに丸投げする (これが最も堅牢です)
final formatInfo = FormatInformation.decodeFormatInformation(raw1, raw2);
if (formatInfo == null) {
//print("Warning: BCH error correction failed. Falling back to raw bit read.");
}
// 2. 失敗した場合、生データを 0x5412 で XOR して強引に EC を引っこ抜く (デバッグ用)
String ecLevel;
int maskPattern;
if (formatInfo != null) {
ecLevel = formatInfo.errorCorrectionLevel.name;
maskPattern = formatInfo.dataMask.toInt();
} else {
// BCH訂正が効かないほど汚れているか、座標がズレている
// 暫定的に raw1 からマスクを剥がして EC を推測する
final unmasked = raw1 ^ 0x5412;
final ecBits = (unmasked >> 13) & 0x03;
ecLevel = ["M", "L", "H", "Q"][ecBits]; // 規格順: 00, 01, 10, 11
maskPattern = (unmasked >> 10) & 0x07;
}
final int version = ((size - 21) ~/ 4) + 1;
if (version < 1 || version > 40) {
throw Exception("Invalid version: $version");
}
final (int dataCW, int eccCW) = QrSpec.get(version, ecLevel);
return (matrix, QrInfo(
version: version,
ecLevel: ecLevel,
maskPattern: maskPattern,
size: size,
dataCodewords: dataCW,
eccCodewords: eccCW,
decodedText: _tryDecodeText(bitmap),
));
} catch (e) {
lastError = e;
continue; // 次の前処理 attempt へ
}
}
throw lastError ?? Exception("Analysis failed after 3 attempts.");
}
/// 試行回数(attempt)に応じて画像の前処理を切り替える
img.Image _applyPreprocess(img.Image originalImage, int attempt) {
switch (attempt) {
case 0:
// 1回目: そのまま(リサイズのみ済みの状態)
return originalImage;
case 1:
// 2回目: コントラスト強調 + シャープ化
// 境界線をはっきりさせて検出率を上げる
var processed = img.contrast(originalImage.clone(), contrast: 1.5);
return img.convolution(processed, filter: [0, -1, 0, -1, 5, -1, 0, -1, 0]);
case 2:
// 3回目: 輝度による二値化
// 露出オーバーや影が強い場合に有効
return img.luminanceThreshold(originalImage.clone(), threshold: 0.5);
default:
return originalImage;
}
}
/// 領域の分類(Painter用)
List<List<QrRegion>> classifyModules(int version, int dataCW, int eccCW) {
final size = 21 + 4 * (version - 1);
final map = List.generate(size, (_) => List.filled(size, QrRegion.unused));
// 1. 各種機能パターンのマーク(順番が重要:上書きを許容)
_markFinderPatterns(map);
_markTimingPatterns(map);
_markFormatInfo(map);
_markVersionInfo(map, version);
_markAlignmentPatterns(map, version);
_markDarkModule(map, version);
// 2. データ+ECCの総ビット数
final int totalBits = (dataCW + eccCW) * 8;
final int dataBits = dataCW * 8;
int bitIndex = 0;
int col = size - 1;
bool upward = true;
// 3. ジグザグスキャンでデータとECCを流し込む
while (col > 0) {
if (col == 6) col--; // タイミングパターンの列を避ける
for (int i = 0; i < size; i++) {
int row = upward ? (size - 1 - i) : i;
for (int c = col; c >= col - 1; c--) {
// 未使用の場所(機能パターンが置かれていない場所)にビットを配置
if (map[row][c] == QrRegion.unused) {
if (bitIndex < totalBits) {
map[row][c] = (bitIndex < dataBits) ? QrRegion.data : QrRegion.ecc;
bitIndex++;
} else {
// 1568ビットを配置しきった後に、まだ unused の場所がある場合
// それが「剰余ビット(Remainder Bits)」です。
// 明示的に別領域として扱うことで、データ/ECCとの混同を避けます。
map[row][c] = QrRegion.unused;
}
}
}
}
col -= 2;
upward = !upward;
}
//デバッグ用:最終的な未使用マスの数を出力(V7なら0になるべき)
//int remainUnused = 0;
//for (var r in map) {
// for (var cell in r) {
// if (cell == QrRegion.unused) remainUnused++;
// }
//}
//debug print("####################Total bits placed: $bitIndex / $totalBits");
//debug print("####################Remaining Unused (Remainder): $remainUnused");
return map;
}
/// マスク解除
List<List<bool>> unmaskMatrix(List<List<bool>> matrix, List<List<QrRegion>> regionMap, int mask) {
final n = matrix.length;
return List.generate(n, (y) => List.generate(n, (x) {
final isData = regionMap[y][x] == QrRegion.data || regionMap[y][x] == QrRegion.ecc;
if (!isData) return matrix[y][x];
return isMasked(mask, x, y) ? !matrix[y][x] : matrix[y][x];
}));
}
///ビットの読み取り順序をリスト化
List<Point<int>> getBitOrder(int version, List<List<QrRegion>> regionMap) {
final size = regionMap.length;
List<Point<int>> order = [];
int col = size - 1;
bool upward = true;
while (col > 0) {
if (col == 6) col--; // タイミングパターンの列をスキップ
for (int i = 0; i < size; i++) {
int row = upward ? (size - 1 - i) : i;
for (int c = col; c >= col - 1; c--) {
// データまたはECCの領域のみを順番に追加
if (regionMap[row][c] == QrRegion.data || regionMap[row][c] == QrRegion.ecc) {
order.add(Point(c, row));
}
}
}
col -= 2;
upward = !upward;
}
return order;
}
void _markAlignmentPatterns(List<List<QrRegion>> map, int version) {
final positions = _alignmentTable[version];
if (positions == null || positions.isEmpty) {
return;
}
final n = map.length;
for (final cy in positions) {
for (final cx in positions) {
// 既存の Finder Pattern 領域(周辺1セル含む)を避ける判定
// 0〜8 (左上), n-9〜n-1 (右上/左下) あたりをカバー
if ((cx <= 8 && cy <= 8) ||
(cx >= n - 9 && cy <= 8) ||
(cx <= 8 && cy >= n - 9)) {
continue;
}
// 5x5 の範囲をマーク
for (int dy = -2; dy <= 2; dy++) {
for (int dx = -2; dx <= 2; dx++) {
final y = cy + dy;
final x = cx + dx;
if (x >= 0 && x < n && y >= 0 && y < n) {
map[y][x] = QrRegion.alignment;
}
}
}
}
}
}
// 型番情報のマーク(Version 7以上)
void _markVersionInfo(List<List<QrRegion>> map, int version) {
if (version < 7) return;
final size = map.length;
// 左下の 6x3 ブロック (縦6 x 横3)
for (int x = 0; x < 6; x++) {
for (int y = size - 11; y < size - 8; y++) {
map[y][x] = QrRegion.version;
}
}
// 右上の 3x6 ブロック (縦3 x 横6)
for (int y = 0; y < 6; y++) {
for (int x = size - 11; x < size - 8; x++) {
map[y][x] = QrRegion.version;
}
}
}
// ダークモジュールのマーク(Versionに関わらず固定位置)
void _markDarkModule(List<List<QrRegion>> map, int version) {
// 座標は常に (size - 8, 8)
final size = map.length;
map[size - 8][8] = QrRegion.format; // 役割としてはフォーマット情報に近い
}
Int32List _convertToArgb(img.Image image) {
final length = image.width * image.height;
final result = Int32List(length);
int i = 0;
for (final pixel in image) {
final r = pixel.r.toInt();
final g = pixel.g.toInt();
final b = pixel.b.toInt();
result[i++] = (0xFF << 24) | (r << 16) | (g << 8) | b;
}
return result;
}
/// Location 1 (左上) の読み取り: Bit 14 (MSB) -> Bit 0 (LSB)
int _readFormatLocation1(List<List<bool>> m) {
final List<List<int>> pixelCoordinates = [
[0, 8], [1, 8], [2, 8], [3, 8], [4, 8], [5, 8], [7, 8], [8, 8], // 14-7
[8, 7], [8, 5], [8, 4], [8, 3], [8, 2], [8, 1], [8, 0] // 6-0
];
int v = 0;
for (final c in pixelCoordinates) {
v = (v << 1) | (m[c[0]][c[1]] ? 1 : 0);
}
return v;
}
/// Location 2 (右上・左下) の読み取り: Bit 14 (MSB) -> Bit 0 (LSB)
int _readFormatLocation2(List<List<bool>> m) {
final int size = m.length;
final List<List<int>> pixelCoordinates = [
// 左下の垂直ライン (Bit 14-8)
[size - 1, 8], [size - 2, 8], [size - 3, 8], [size - 4, 8],
[size - 5, 8], [size - 6, 8], [size - 7, 8],
// 右上の水平ライン (Bit 7-0)
[8, size - 8], [8, size - 7], [8, size - 6], [8, size - 5],
[8, size - 4], [8, size - 3], [8, size - 2], [8, size - 1]
];
int v = 0;
for (final c in pixelCoordinates) {
v = (v << 1) | (m[c[0]][c[1]] ? 1 : 0);
}
return v;
}
String? _tryDecodeText(BinaryBitmap bitmap) {
try {
return QRCodeReader().decode(bitmap).text;
} catch (_) {
return null;
}
}
void _markFinderPatterns(List<List<QrRegion>> map) {
final size = map.length;
void mark(int ox, int oy) {
// 7x7ではなく8x8(セパレータ含む)をスキャン
for (int y = oy; y < oy + 8; y++) {
for (int x = ox; x < ox + 8; x++) {
// 配列の範囲内のみマーク
if (x >= 0 && x < size && y >= 0 && y < size) {
map[y][x] = QrRegion.finder; // 分類上はfinderまたはseparatorとして予約
}
}
}
}
mark(0, 0); // 左上
mark(size - 8, 0); // 右上(起点をsize-8に修正)
mark(0, size - 8); // 左下(起点をsize-8に修正)
}
void _markTimingPatterns(List<List<QrRegion>> map) {
final size = map.length;
for (int i = 0; i < size; i++) {
map[6][i] = QrRegion.timing;
map[i][6] = QrRegion.timing;
}
}
void _markFormatInfo(List<List<QrRegion>> map) {
final size = map.length;
// 左上周辺
for (int i = 0; i <= 8; i++) {
if (i != 6) map[8][i] = QrRegion.format; // 水平
if (i != 6) map[i][8] = QrRegion.format; // 垂直
}
// 右上と左下
for (int i = 0; i < 8; i++) {
map[8][size - 1 - i] = QrRegion.format; // 右上水平
map[size - 1 - i][8] = QrRegion.format; // 左下垂直
}
}
}
/// Copyright© ao-system, Inc.
enum QrRegion {
finder,
alignment,
timing,
format,
version,
data,
ecc,
unused,
}
/// Copyright© ao-system, Inc.
class QrSpec {
// 形式: version -> ecLevel -> (dataCodewords, eccCodewords)
static const Map<int, Map<String, (int, int)>> table = {
1: {"L": (19, 7), "M": (16, 10), "Q": (13, 13), "H": (9, 17)},
2: {"L": (34, 10), "M": (28, 16), "Q": (22, 22), "H": (16, 28)},
3: {"L": (55, 15), "M": (44, 26), "Q": (34, 18), "H": (26, 22)},
4: {"L": (80, 20), "M": (64, 18), "Q": (48, 26), "H": (36, 16)},
5: {"L": (108, 26), "M": (86, 24), "Q": (62, 36), "H": (46, 44)},
6: {"L": (136, 18), "M": (108, 16), "Q": (76, 48), "H": (60, 56)},
7: {"L": (156, 40), "M": (124, 72), "Q": (88, 108), "H": (66, 130)},
8: {"L": (194, 48), "M": (154, 88), "Q": (110, 132), "H": (86, 156)},
9: {"L": (232, 60), "M": (182, 110), "Q": (132, 160), "H": (100, 192)},
10: {"L": (274, 72), "M": (216, 144), "Q": (154, 192), "H": (122, 224)},
11: {"L": (324, 80), "M": (254, 174), "Q": (180, 224), "H": (140, 264)},
12: {"L": (370, 96), "M": (290, 196), "Q": (206, 260), "H": (158, 308)},
13: {"L": (428, 104), "M": (334, 224), "Q": (244, 288), "H": (180, 352)},
14: {"L": (461, 120), "M": (365, 272), "Q": (261, 320), "H": (197, 384)},
15: {"L": (523, 132), "M": (415, 306), "Q": (295, 360), "H": (223, 432)},
16: {"L": (589, 144), "M": (453, 344), "Q": (325, 408), "H": (253, 480)},
17: {"L": (647, 168), "M": (507, 384), "Q": (367, 448), "H": (283, 532)},
18: {"L": (721, 180), "M": (563, 432), "Q": (397, 504), "H": (313, 588)},
19: {"L": (795, 196), "M": (627, 476), "Q": (445, 560), "H": (341, 650)},
20: {"L": (861, 224), "M": (669, 528), "Q": (485, 632), "H": (385, 700)},
21: {"L": (932, 224), "M": (714, 576), "Q": (512, 720), "H": (406, 784)},
22: {"L": (1006, 252), "M": (782, 644), "Q": (568, 784), "H": (442, 864)},
23: {"L": (1094, 270), "M": (860, 700), "Q": (614, 840), "H": (464, 948)},
24: {"L": (1174, 300), "M": (914, 760), "Q": (664, 912), "H": (514, 1020)},
25: {"L": (1276, 312), "M": (1000, 840), "Q": (718, 1020), "H": (538, 1140)},
26: {"L": (1370, 336), "M": (1062, 896), "Q": (754, 1088), "H": (596, 1216)},
27: {"L": (1468, 360), "M": (1128, 952), "Q": (808, 1152), "H": (628, 1280)},
28: {"L": (1531, 390), "M": (1193, 1020), "Q": (871, 1240), "H": (661, 1380)},
29: {"L": (1631, 420), "M": (1267, 1080), "Q": (911, 1320), "H": (701, 1470)},
30: {"L": (1735, 450), "M": (1373, 1140), "Q": (985, 1410), "H": (745, 1560)},
31: {"L": (1843, 480), "M": (1455, 1230), "Q": (1033, 1500), "H": (793, 1680)},
32: {"L": (1955, 510), "M": (1541, 1290), "Q": (1115, 1590), "H": (845, 1770)},
33: {"L": (2071, 540), "M": (1631, 1380), "Q": (1171, 1680), "H": (901, 1890)},
34: {"L": (2191, 570), "M": (1725, 1470), "Q": (1231, 1770), "H": (961, 1980)},
35: {"L": (2306, 600), "M": (1812, 1560), "Q": (1286, 1860), "H": (986, 2100)},
36: {"L": (2434, 630), "M": (1914, 1650), "Q": (1354, 1950), "H": (1054, 2220)},
37: {"L": (2566, 660), "M": (1992, 1740), "Q": (1426, 2040), "H": (1096, 2310)},
38: {"L": (2702, 720), "M": (2102, 1830), "Q": (1502, 2130), "H": (1142, 2430)},
39: {"L": (2812, 750), "M": (2216, 1920), "Q": (1582, 2220), "H": (1222, 2520)},
40: {"L": (2956, 780), "M": (2334, 2010), "Q": (1666, 2310), "H": (1276, 2610)},
};
static (int dataCW, int eccCW) get(int version, String ec) {
final versionData = table[version];
if (versionData == null) {
throw Exception("Unsupported QR Version: $version.");
}
final spec = versionData[ec];
if (spec == null) {
throw Exception("Unsupported EC Level: $ec for Version $version.");
}
return spec;
}
}
/// Copyright© ao-system, Inc.
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:qranalyzer/qr_region.dart';
class QrUnmaskedPainter extends CustomPainter {
final List<List<bool>> matrix;
final List<List<QrRegion>> regionMap;
final List<Point<int>> bitOrder;
final int highlightIndex;
final int dataBitLimit; // データビットの総数 (dataCodewords * 8)
QrUnmaskedPainter(
this.matrix,
this.regionMap,
this.bitOrder,
this.highlightIndex,
this.dataBitLimit,
);
@override
void paint(Canvas canvas, Size size) {
final int n = matrix.length;
final double cell = size.width / n;
// --- 色の定義 ---
// 1. 基本(データ・ECC)
final Paint paintDark = Paint()..color = Colors.black.withValues(alpha: 0.8);
final Paint paintLight = Paint()..color = Colors.white.withValues(alpha: 0.8);
// 2. 機能パターン(デバッグ用に色分け)
final Paint paintFinder = Paint()..color = Colors.green.withValues(alpha: 0.4);
final Paint paintTiming = Paint()..color = Colors.orange.withValues(alpha: 0.4);
final Paint paintAlignment = Paint()..color = Colors.purple.withValues(alpha: 0.4);
final Paint paintVersionFormat = Paint()..color = Colors.teal.withValues(alpha: 0.4);
// 3. 未使用(剰余ビット)
final Paint paintUnused = Paint()..color = Colors.grey.withValues(alpha: 0.2);
// 4. ハイライト用
final Paint paintDataHighlight = Paint()..color = Colors.blue;
final Paint paintEccHighlight = Paint()..color = Colors.red;
final Paint paintDataPassed = Paint()..color = Colors.blue.withValues(alpha: 0.4);
final Paint paintEccPassed = Paint()..color = Colors.red.withValues(alpha: 0.4);
// --- 1. ベースのQRと領域の描画 ---
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
final region = regionMap[y][x];
final rect = Rect.fromLTWH(x * cell, y * cell, cell, cell);
// まずはセル自体の色(黒/白)を塗る(データ/ECC領域のみ)
if (region == QrRegion.data || region == QrRegion.ecc) {
canvas.drawRect(rect, matrix[y][x] ? paintDark : paintLight);
} else {
// 機能パターンや未使用領域を色分け描画
Paint p;
switch (region) {
case QrRegion.finder:
p = paintFinder;
break;
case QrRegion.timing:
p = paintTiming;
break;
case QrRegion.alignment:
p = paintAlignment;
break;
case QrRegion.version:
case QrRegion.format:
p = paintVersionFormat;
break;
case QrRegion.unused:
default:
p = paintUnused;
break;
}
canvas.drawRect(rect, p);
}
}
}
// --- 2. シーク位置までのビットをハイライト ---
for (int i = 0; i <= highlightIndex && i < bitOrder.length; i++) {
final p = bitOrder[i];
final rect = Rect.fromLTWH(p.x * cell, p.y * cell, cell, cell);
bool isData = i < dataBitLimit;
Paint highlightPaint;
if (i == highlightIndex) {
// 現在フォーカスされているビット
highlightPaint = isData ? paintDataHighlight : paintEccHighlight;
} else {
// 通過済みのビット
highlightPaint = isData ? paintDataPassed : paintEccPassed;
}
canvas.drawRect(rect, highlightPaint);
}
}
@override
bool shouldRepaint(covariant QrUnmaskedPainter oldDelegate) =>
oldDelegate.highlightIndex != highlightIndex ||
oldDelegate.dataBitLimit != dataBitLimit ||
oldDelegate.matrix != matrix; // 行列が変わった際も再描画
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
import 'package:qranalyzer/theme_color.dart';
import 'package:qranalyzer/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:flutter/foundation.dart";
import "package:flutter/material.dart";
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:in_app_review/in_app_review.dart';
import "package:qranalyzer/setting_card.dart";
import "package:qranalyzer/l10n/app_localizations.dart";
import "package:qranalyzer/ad_banner_widget.dart";
import "package:qranalyzer/ad_ump_status.dart";
import 'package:qranalyzer/loading_screen.dart';
import 'package:qranalyzer/theme_color.dart';
import 'package:qranalyzer/model.dart';
import 'package:qranalyzer/_secrets.dart';
import "package:qranalyzer/main.dart";
import 'package:qranalyzer/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;
late ThemeColor _themeColor;
final _inAppReview = InAppReview.instance;
bool _wakelockEnabled = true;
int _schemeColor = 0;
Color _accentColor = Colors.red;
int _themeNumber = 0;
String _languageCode = '';
bool _isReady = false;
@override
void initState() {
super.initState();
_initState();
}
void _initState() async {
//ump
_adUmpService = AdUmpService();
await _refreshConsentInfo();
//model
_wakelockEnabled = Model.wakelockEnabled;
_schemeColor = Model.schemeColor;
_accentColor = _getRainbowAccentColor(_schemeColor);
_themeNumber = Model.themeNumber;
_languageCode = Model.languageCode;
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; });
}
}
Color _getRainbowAccentColor(int hue) {
return HSVColor.fromAHSV(1.0, hue.toDouble(), 1.0, 1.0).toColor();
}
void _onApply() async {
await Model.setWakelockEnabled(_wakelockEnabled);
await Model.setSchemeColor(_schemeColor);
await Model.setThemeNumber(_themeNumber);
await Model.setLanguageCode(_languageCode);
if (!mounted) {
return;
}
Navigator.of(context).pop(true);
}
@override
Widget build(BuildContext context) {
if (!_isReady) {
return LoadingScreen();
}
final l = AppLocalizations.of(context)!;
final TextTheme t = Theme.of(context).textTheme;
return Scaffold(
backgroundColor: _themeColor.backColor,
appBar: AppBar(
backgroundColor: Colors.transparent,
leading: IconButton(
icon: const Icon(Icons.close),
onPressed: () => Navigator.of(context).pop(false)
),
actions: [
Padding(
padding: const EdgeInsets.only(right: 10),
child:IconButton(
icon: const Icon(Icons.check),
onPressed: _onApply,
)
),
],
),
body: SafeArea(
child: Column(children:[
Expanded(
child: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(), //背景タップでキーボードを仕舞う
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 0, bottom: 100),
child: Column(children: [
_buildWakelockEnabled(l, t),
_buildSchemeColor(l, t),
_buildTheme(l, t),
_buildLanguage(l, t),
_buildReview(l, t),
_buildCmp(l, t),
_buildAtt(l, t),
_buildUsage(l, t),
]),
),
),
),
),
])
),
bottomNavigationBar: AdBannerWidget(adManager: MainApp.of(context).adManager),
);
}
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 _buildSchemeColor(AppLocalizations l, TextTheme t) {
return SettingCard(
child: ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
title: Text(l.colorScheme, style: t.bodyMedium),
subtitle: Row(
children: <Widget>[
Text(_schemeColor.toStringAsFixed(0), style: t.bodyMedium),
Expanded(
child: 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: _schemeColor.toDouble(),
min: 0,
max: 360,
divisions: 360,
label: _schemeColor.toString(),
onChanged: (double value) {
setState(() {
_schemeColor = value.toInt();
_accentColor = _getRainbowAccentColor(_schemeColor);
});
},
),
),
),
],
),
),
);
}
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),
),
],
),
);
},
),
],
),
),
);
}
Widget _buildUsage(AppLocalizations l, TextTheme t) {
return SettingCard(
child: ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(l.usage1, style: t.bodyMedium),
],
),
),
);
}
}
/// Copyright© ao-system, Inc.
import 'package:flutter/material.dart';
import 'package:qranalyzer/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;
}
}
Color _getRainbowAccentColor(int hue, double saturation) {
return HSVColor.fromAHSV(1.0, hue.toDouble(), saturation, 1.0).toColor();
}
bool get _isLight => _effectiveBrightness == Brightness.light;
//main page
Color get mainBackColor => _isLight ? Color.fromRGBO(200,200,200, 1.0) : Color.fromRGBO(30,30,30, 1.0);
Color get mainBack2Color => _isLight ? Color.fromRGBO(255,255,255, 1.0) : Color.fromRGBO(50,50,50, 1.0);
Color get mainCardColor => _isLight ? Color.fromRGBO(255, 255, 255, 1.0) : Color.fromRGBO(51, 51, 51, 1.0);
Color get mainForeColor => _isLight ? Color.fromRGBO(17, 17, 17, 1.0) : Color.fromRGBO(200, 200, 200, 1.0);
Color get mainAccentForeColor => _getRainbowAccentColor(Model.schemeColor,0.6);
//setting page
Color get backColor => _isLight ? Colors.grey[200]! : 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;
}
}
}