import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:my_flutter_puzzle/application/states/image_splitter_state.dart'; import 'package:my_flutter_puzzle/application/states/puzzle_state.dart'; import 'package:my_flutter_puzzle/cubits/count_down_timer_cubit.dart'; import 'package:my_flutter_puzzle/models/puzzle_data.dart'; import 'package:my_flutter_puzzle/providers.dart'; import 'package:my_flutter_puzzle/res/palette.dart'; import 'package:my_flutter_puzzle/screens/photo/photo_screen_large.dart'; import 'package:my_flutter_puzzle/utils/color_brightness.dart'; import 'package:my_flutter_puzzle/utils/puzzle_solver.dart'; import 'package:rive/rive.dart'; class PhotoScreen extends ConsumerStatefulWidget { const PhotoScreen({ required this.solverClient, required this.initialPuzzleData, required this.puzzleSize, required this.riveController, required this.duration, required this.level, Key? key, }) : super(key: key); final PuzzleSolverClient solverClient; final PuzzleData initialPuzzleData; final int duration; final int puzzleSize; final int level; final RiveAnimationController riveController; @override ConsumerState createState() => _PhotoScreenState(); } class _PhotoScreenState extends ConsumerState { late final PuzzleSolverClient _solverClient; late final int _puzzleSize; late final PuzzleData _initialPuzzleData; late final RiveAnimationController _riveController; late ThemeData _themeData = Theme.of(context); @override void initState() { _solverClient = widget.solverClient; _puzzleSize = widget.puzzleSize; _initialPuzzleData = widget.initialPuzzleData; _riveController = widget.riveController; super.initState(); } @override Widget build(BuildContext context) { ref.listen(puzzleNotifierProvider(_solverClient), (previous, PuzzleState next) { if (next is PuzzleSolved) { BlocProvider.of(context).stop(); } }); ref.listen(imageSplitterNotifierProvider, (previous, next) { if (next is ImageSplitterComplete) { _themeData = Theme.of(context).copyWith( colorScheme: ColorScheme( brightness: Theme.of(context).brightness, primary: next.palette.vibrantColor?.color ?? Palette.blue, onPrimary: next.palette.lightVibrantColor?.color ?? Colors.white, secondary: Theme.of(context).colorScheme.secondary, onSecondary: Theme.of(context).colorScheme.onSecondary, error: Theme.of(context).colorScheme.error, onError: Theme.of(context).colorScheme.onError, background: next.palette.darkMutedColor?.color ?? Palette.blue.darken(0.3), onBackground: Colors.white, surface: Theme.of(context).colorScheme.surface, onSurface: Theme.of(context).colorScheme.onSurface, ), ); setState(() {}); } }); return Consumer( builder: (context, ref, child) { final state = ref.watch(imageSplitterNotifierProvider); return state.maybeWhen( () { return Theme( data: _themeData, child: PhotoScreenLarge( solverClient: _solverClient, level: widget.level, initialPuzzleData: _initialPuzzleData, duration: widget.duration, puzzleSize: _puzzleSize, riveController: _riveController, ), ); }, complete: (image, images, palette) { _themeData = Theme.of(context).copyWith( colorScheme: ColorScheme( brightness: Theme.of(context).brightness, primary: palette.vibrantColor?.color ?? Palette.blue, onPrimary: palette.lightVibrantColor?.color ?? Colors.white, secondary: Theme.of(context).colorScheme.secondary, onSecondary: Theme.of(context).colorScheme.onSecondary, error: Theme.of(context).colorScheme.error, onError: Theme.of(context).colorScheme.onError, background: palette.darkMutedColor?.color ?? Palette.blue.darken(0.3), onBackground: Colors.white, surface: Theme.of(context).colorScheme.surface, onSurface: Theme.of(context).colorScheme.onSurface, ), ); return Theme( data: _themeData, child: PhotoScreenLarge( solverClient: _solverClient, initialPuzzleData: _initialPuzzleData, puzzleSize: _puzzleSize, level: widget.level, riveController: _riveController, duration: widget.duration, ), ); }, orElse: () => Theme( data: _themeData, child: PhotoScreenLarge( solverClient: _solverClient, initialPuzzleData: _initialPuzzleData, duration: widget.duration, level: widget.level, puzzleSize: _puzzleSize, riveController: _riveController, ), ), ); }, ); } }