slide_puzzle: use pkg:provider

pull/88/head
Kevin Moore 5 years ago
parent 051f5bec29
commit 0e50bbd6c9

@ -25,7 +25,7 @@ class PuzzleApp extends StatelessWidget {
class _PuzzleHome extends StatefulWidget { class _PuzzleHome extends StatefulWidget {
final int _rows, _columns; final int _rows, _columns;
const _PuzzleHome(this._rows, this._columns, {Key key}) : super(key: key); const _PuzzleHome(this._rows, this._columns);
@override @override
PuzzleHomeState createState() => PuzzleHomeState createState() =>

@ -4,28 +4,25 @@
import 'core/puzzle_animator.dart'; import 'core/puzzle_animator.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'shared_theme.dart';
abstract class AppState { abstract class AppState {
TabController get tabController;
PuzzleProxy get puzzle; PuzzleProxy get puzzle;
bool get autoPlay; bool get autoPlay;
void setAutoPlay(bool newValue); void setAutoPlay(bool newValue);
AnimationNotifier get animationNotifier; Listenable get animationNotifier;
Iterable<SharedTheme> get themeData;
SharedTheme get currentTheme;
set currentTheme(SharedTheme theme);
}
abstract class AnimationNotifier implements Listenable { void clickOrShake(int tileValue) {
void animate(); setAutoPlay(false);
puzzle.clickOrShake(tileValue);
}
void dispose(); void Function(bool newValue) get setAutoPlayFunction {
if (puzzle.solved) {
return null;
}
return setAutoPlay;
}
} }

@ -4,17 +4,15 @@
import 'dart:async'; import 'dart:async';
import 'package:provider/provider.dart';
import 'app_state.dart'; import 'app_state.dart';
import 'core/puzzle_animator.dart'; import 'core/puzzle_animator.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'shared_theme.dart'; import 'shared_theme.dart';
import 'theme_plaster.dart'; import 'themes.dart';
import 'theme_seattle.dart';
import 'theme_simple.dart';
class PuzzleHomeState extends State class PuzzleHomeState extends State with TickerProviderStateMixin, AppState {
with TickerProviderStateMixin
implements AppState {
TabController _tabController; TabController _tabController;
AnimationController _controller; AnimationController _controller;
@ -22,41 +20,21 @@ class PuzzleHomeState extends State
final PuzzleAnimator puzzle; final PuzzleAnimator puzzle;
@override @override
final animationNotifier = _AnimationNotifier(); final _AnimationNotifier animationNotifier = _AnimationNotifier();
@override
TabController get tabController => _tabController;
SharedTheme _currentTheme; SharedTheme _currentTheme;
@override
SharedTheme get currentTheme => _currentTheme;
@override
set currentTheme(SharedTheme theme) {
setState(() {
_currentTheme = theme;
});
}
Duration _tickerTimeSinceLastEvent = Duration.zero; Duration _tickerTimeSinceLastEvent = Duration.zero;
Ticker _ticker; Ticker _ticker;
Duration _lastElapsed; Duration _lastElapsed;
StreamSubscription sub; StreamSubscription _puzzleEventSubscription;
@override @override
bool autoPlay = false; bool autoPlay = false;
PuzzleHomeState(this.puzzle) { PuzzleHomeState(this.puzzle) {
sub = puzzle.onEvent.listen(_onPuzzleEvent); _puzzleEventSubscription = puzzle.onEvent.listen(_onPuzzleEvent);
_themeDataCache = List.unmodifiable([
ThemeSimple(this),
ThemeSeattle(this),
ThemePlaster(this),
]);
_currentTheme = themeData.first; _currentTheme = themes.first;
} }
@override @override
@ -70,18 +48,15 @@ class PuzzleHomeState extends State
duration: const Duration(milliseconds: 200), duration: const Duration(milliseconds: 200),
); );
_tabController = TabController(vsync: this, length: _themeDataCache.length); _tabController = TabController(vsync: this, length: themes.length);
_tabController.addListener(() { _tabController.addListener(() {
currentTheme = _themeDataCache[_tabController.index]; setState(() {
_currentTheme = themes[_tabController.index];
});
}); });
} }
List<SharedTheme> _themeDataCache;
@override
Iterable<SharedTheme> get themeData => _themeDataCache;
@override @override
void setAutoPlay(bool newValue) { void setAutoPlay(bool newValue) {
if (newValue != autoPlay) { if (newValue != autoPlay) {
@ -96,8 +71,13 @@ class PuzzleHomeState extends State
} }
@override @override
Widget build(BuildContext context) => Widget build(BuildContext context) => MultiProvider(
LayoutBuilder(builder: _currentTheme.build); providers: [
ListenableProvider.value(listenable: _tabController),
Provider<AppState>.value(value: this),
],
child: LayoutBuilder(builder: _currentTheme.build),
);
@override @override
void dispose() { void dispose() {
@ -105,7 +85,7 @@ class PuzzleHomeState extends State
_tabController.dispose(); _tabController.dispose();
_controller?.dispose(); _controller?.dispose();
_ticker?.dispose(); _ticker?.dispose();
sub.cancel(); _puzzleEventSubscription.cancel();
super.dispose(); super.dispose();
} }
@ -165,10 +145,7 @@ class PuzzleHomeState extends State
} }
} }
class _AnimationNotifier extends ChangeNotifier implements AnimationNotifier { class _AnimationNotifier extends ChangeNotifier {
_AnimationNotifier();
@override
void animate() { void animate() {
notifyListeners(); notifyListeners();
} }

@ -2,32 +2,31 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'package:provider/provider.dart';
import 'app_state.dart'; import 'app_state.dart';
import 'core/puzzle_animator.dart'; import 'core/puzzle_animator.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'puzzle_flow_delegate.dart'; import 'puzzle_flow_delegate.dart';
import 'themes.dart';
import 'widgets/material_interior_alt.dart'; import 'widgets/material_interior_alt.dart';
abstract class SharedTheme { abstract class SharedTheme {
SharedTheme(this._appState); const SharedTheme();
final AppState _appState;
PuzzleProxy get puzzle => _appState.puzzle;
String get name; String get name;
Color get puzzleThemeBackground; Color get puzzleThemeBackground;
RoundedRectangleBorder get puzzleBorder; RoundedRectangleBorder puzzleBorder(bool small);
Color get puzzleBackgroundColor; Color get puzzleBackgroundColor;
Color get puzzleAccentColor; Color get puzzleAccentColor;
EdgeInsetsGeometry get tilePadding => const EdgeInsets.all(6); EdgeInsetsGeometry tilePadding(PuzzleProxy puzzle) => const EdgeInsets.all(6);
Widget tileButton(int i); Widget tileButton(int i, AppState appState, bool small);
Ink createInk( Ink createInk(
Widget child, { Widget child, {
@ -43,6 +42,8 @@ abstract class SharedTheme {
); );
Widget createButton( Widget createButton(
AppState appState,
bool small,
int tileValue, int tileValue,
Widget content, { Widget content, {
Color color, Color color,
@ -50,178 +51,164 @@ abstract class SharedTheme {
}) => }) =>
AnimatedContainer( AnimatedContainer(
duration: _puzzleAnimationDuration, duration: _puzzleAnimationDuration,
padding: tilePadding, padding: tilePadding(appState.puzzle),
child: RaisedButton( child: RaisedButton(
elevation: 4, elevation: 4,
clipBehavior: Clip.hardEdge, clipBehavior: Clip.hardEdge,
animationDuration: _puzzleAnimationDuration, animationDuration: _puzzleAnimationDuration,
onPressed: () => _tilePress(tileValue), onPressed: () => appState.clickOrShake(tileValue),
shape: shape ?? puzzleBorder, shape: shape ?? puzzleBorder(small),
padding: const EdgeInsets.symmetric(), padding: const EdgeInsets.symmetric(),
child: content, child: content,
color: color, color: color,
), ),
); );
double _previousConstraintWidth; Widget _updateConstraints(
bool _small; BoxConstraints constraints, Widget Function(bool small) builder) {
bool get small => _small;
void _updateConstraints(BoxConstraints constraints) {
const _smallWidth = 580; const _smallWidth = 580;
final constraintWidth = final constraintWidth =
constraints.hasBoundedWidth ? constraints.maxWidth : 1000.0; constraints.hasBoundedWidth ? constraints.maxWidth : 1000.0;
if (constraintWidth == _previousConstraintWidth) { return builder(constraintWidth < _smallWidth);
assert(_small != null);
return;
}
_previousConstraintWidth = constraintWidth;
if (_previousConstraintWidth < _smallWidth) {
_small = true;
} else {
_small = false;
}
} }
Widget build(BuildContext context, BoxConstraints constraints) { Widget build(BuildContext context, BoxConstraints constraints) =>
_updateConstraints(constraints); _updateConstraints(
return Material( constraints,
child: Stack( (small) => Material(
children: <Widget>[ child: Stack(
const SizedBox.expand( children: <Widget>[
child: FittedBox( const SizedBox.expand(
fit: BoxFit.cover, child: FittedBox(
child: Image( fit: BoxFit.cover,
image: AssetImage('seattle.jpg'), child: Image(
), image: AssetImage('seattle.jpg'),
),
),
AnimatedContainer(
duration: _puzzleAnimationDuration,
color: puzzleThemeBackground,
child: Center(
child: _styledWrapper(
SizedBox(
width: 580,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black26,
width: 1,
),
),
),
margin: const EdgeInsets.symmetric(horizontal: 20),
child: TabBar(
controller: _appState.tabController,
labelPadding: const EdgeInsets.fromLTRB(0, 20, 0, 12),
labelColor: puzzleAccentColor,
indicatorColor: puzzleAccentColor,
indicatorWeight: 1.5,
unselectedLabelColor: Colors.black.withOpacity(0.6),
tabs: _appState.themeData
.map((st) => Text(
st.name.toUpperCase(),
style: const TextStyle(
letterSpacing: 0.5,
),
))
.toList(),
), ),
), ),
Container( ),
constraints: const BoxConstraints.tightForFinite(), AnimatedContainer(
padding: const EdgeInsets.all(10), duration: _puzzleAnimationDuration,
child: Flow( color: puzzleThemeBackground,
delegate: PuzzleFlowDelegate( child: Center(
small ? const Size(90, 90) : const Size(140, 140), child: _styledWrapper(
puzzle, small,
_appState.animationNotifier, SizedBox(
), width: 580,
children: List<Widget>.generate( child: Consumer<AppState>(
puzzle.length, builder: (context, appState, _) => Column(
_tileButton, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black26,
width: 1,
),
),
),
margin: const EdgeInsets.symmetric(
horizontal: 20),
child: TabBar(
controller:
Provider.of<TabController>(context),
labelPadding: const EdgeInsets.fromLTRB(
0, 20, 0, 12),
labelColor: puzzleAccentColor,
indicatorColor: puzzleAccentColor,
indicatorWeight: 1.5,
unselectedLabelColor:
Colors.black.withOpacity(0.6),
tabs: themes
.map((st) => Text(
st.name.toUpperCase(),
style: const TextStyle(
letterSpacing: 0.5,
),
))
.toList(),
),
),
Container(
constraints:
const BoxConstraints.tightForFinite(),
padding: const EdgeInsets.all(10),
child: Flow(
delegate: PuzzleFlowDelegate(
small
? const Size(90, 90)
: const Size(140, 140),
appState.puzzle,
appState.animationNotifier,
),
children: List<Widget>.generate(
appState.puzzle.length,
(i) =>
_tileButton(i, appState, small),
),
),
),
Container(
decoration: const BoxDecoration(
border: Border(
top: BorderSide(
color: Colors.black26, width: 1),
),
),
padding: const EdgeInsets.only(
left: 10,
bottom: 6,
top: 2,
right: 10,
),
child: Row(
children: _bottomControls(appState)),
)
],
),
),
), ),
), ),
), ),
Container( )
decoration: const BoxDecoration( ],
border: Border( )));
top: BorderSide(color: Colors.black26, width: 1),
),
),
padding: const EdgeInsets.only(
left: 10,
bottom: 6,
top: 2,
right: 10,
),
child: Row(children: _bottomControls(context)),
)
],
),
),
),
),
)
],
));
}
Duration get _puzzleAnimationDuration => kThemeAnimationDuration * 3; Duration get _puzzleAnimationDuration => kThemeAnimationDuration * 3;
// Thought about using AnimatedContainer here, but it causes some weird // Thought about using AnimatedContainer here, but it causes some weird
// resizing behavior // resizing behavior
Widget _styledWrapper(Widget child) => MaterialInterior( Widget _styledWrapper(bool small, Widget child) => MaterialInterior(
duration: _puzzleAnimationDuration, duration: _puzzleAnimationDuration,
shape: puzzleBorder, shape: puzzleBorder(small),
color: puzzleBackgroundColor, color: puzzleBackgroundColor,
child: child, child: child,
); );
void Function(bool newValue) get _setAutoPlay {
if (puzzle.solved) {
return null;
}
return _appState.setAutoPlay;
}
void _tilePress(int tileValue) {
_appState.setAutoPlay(false);
_appState.puzzle.clickOrShake(tileValue);
}
TextStyle get _infoStyle => TextStyle( TextStyle get _infoStyle => TextStyle(
color: puzzleAccentColor, color: puzzleAccentColor,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
); );
List<Widget> _bottomControls(BuildContext context) => <Widget>[ List<Widget> _bottomControls(AppState appState) => <Widget>[
IconButton( IconButton(
onPressed: puzzle.reset, onPressed: appState.puzzle.reset,
icon: Icon(Icons.refresh, color: puzzleAccentColor), icon: Icon(Icons.refresh, color: puzzleAccentColor),
//Icons.refresh, //Icons.refresh,
), ),
Checkbox( Checkbox(
value: _appState.autoPlay, value: appState.autoPlay,
onChanged: _setAutoPlay, onChanged: appState.setAutoPlayFunction,
activeColor: puzzleAccentColor, activeColor: puzzleAccentColor,
), ),
Expanded( Expanded(
child: Container(), child: Container(),
), ),
Text( Text(
puzzle.clickCount.toString(), appState.puzzle.clickCount.toString(),
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: _infoStyle, style: _infoStyle,
), ),
@ -229,7 +216,7 @@ abstract class SharedTheme {
SizedBox( SizedBox(
width: 28, width: 28,
child: Text( child: Text(
puzzle.incorrectTiles.toString(), appState.puzzle.incorrectTiles.toString(),
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: _infoStyle, style: _infoStyle,
), ),
@ -237,11 +224,11 @@ abstract class SharedTheme {
const Text(' Tiles left ') const Text(' Tiles left ')
]; ];
Widget _tileButton(int i) { Widget _tileButton(int i, AppState appState, bool small) {
if (i == puzzle.tileCount && !puzzle.solved) { if (i == appState.puzzle.tileCount && !appState.puzzle.solved) {
return const Center(); return const Center();
} }
return tileButton(i); return tileButton(i, appState, small);
} }
} }

@ -14,7 +14,7 @@ class ThemePlaster extends SharedTheme {
@override @override
String get name => 'Plaster'; String get name => 'Plaster';
ThemePlaster(AppState baseTheme) : super(baseTheme); const ThemePlaster();
@override @override
Color get puzzleThemeBackground => _chocolate; Color get puzzleThemeBackground => _chocolate;
@ -26,7 +26,7 @@ class ThemePlaster extends SharedTheme {
Color get puzzleAccentColor => _orangeIsh; Color get puzzleAccentColor => _orangeIsh;
@override @override
RoundedRectangleBorder get puzzleBorder => RoundedRectangleBorder( RoundedRectangleBorder puzzleBorder(bool small) => RoundedRectangleBorder(
side: const BorderSide( side: const BorderSide(
color: Color.fromARGB(255, 103, 103, 105), color: Color.fromARGB(255, 103, 103, 105),
width: 8, width: 8,
@ -37,14 +37,14 @@ class ThemePlaster extends SharedTheme {
); );
@override @override
Widget tileButton(int i) { Widget tileButton(int i, AppState appState, bool small) {
final correctColumn = i % puzzle.width; final correctColumn = i % appState.puzzle.width;
final correctRow = i ~/ puzzle.width; final correctRow = i ~/ appState.puzzle.width;
final primary = (correctColumn + correctRow).isEven; final primary = (correctColumn + correctRow).isEven;
if (i == puzzle.tileCount) { if (i == appState.puzzle.tileCount) {
assert(puzzle.solved); assert(appState.puzzle.solved);
return Center( return Center(
child: Icon( child: Icon(
Icons.thumb_up, Icons.thumb_up,
@ -64,6 +64,8 @@ class ThemePlaster extends SharedTheme {
); );
return createButton( return createButton(
appState,
small,
i, i,
content, content,
color: primary ? _orangeIsh : _yellowIsh, color: primary ? _orangeIsh : _yellowIsh,

@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'app_state.dart'; import 'app_state.dart';
import 'core/puzzle_animator.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'shared_theme.dart'; import 'shared_theme.dart';
import 'widgets/decoration_image_plus.dart'; import 'widgets/decoration_image_plus.dart';
@ -11,7 +12,7 @@ class ThemeSeattle extends SharedTheme {
@override @override
String get name => 'Seattle'; String get name => 'Seattle';
ThemeSeattle(AppState proxy) : super(proxy); const ThemeSeattle();
@override @override
Color get puzzleThemeBackground => const Color.fromARGB(153, 90, 135, 170); Color get puzzleThemeBackground => const Color.fromARGB(153, 90, 135, 170);
@ -23,18 +24,20 @@ class ThemeSeattle extends SharedTheme {
Color get puzzleAccentColor => const Color(0xff000579f); Color get puzzleAccentColor => const Color(0xff000579f);
@override @override
RoundedRectangleBorder get puzzleBorder => const RoundedRectangleBorder( RoundedRectangleBorder puzzleBorder(bool small) =>
const RoundedRectangleBorder(
borderRadius: BorderRadius.all( borderRadius: BorderRadius.all(
Radius.circular(1), Radius.circular(1),
), ),
); );
@override @override
EdgeInsetsGeometry get tilePadding => EdgeInsetsGeometry tilePadding(PuzzleProxy puzzle) =>
puzzle.solved ? const EdgeInsets.all(1) : const EdgeInsets.all(4); puzzle.solved ? const EdgeInsets.all(1) : const EdgeInsets.all(4);
@override @override
Widget tileButton(int i) { Widget tileButton(int i, AppState appState, bool small) {
final puzzle = appState.puzzle;
if (i == puzzle.tileCount && !puzzle.solved) { if (i == puzzle.tileCount && !puzzle.solved) {
assert(puzzle.solved); assert(puzzle.solved);
} }
@ -69,6 +72,6 @@ class ThemeSeattle extends SharedTheme {
padding: EdgeInsets.all(small ? 20 : 32), padding: EdgeInsets.all(small ? 20 : 32),
); );
return createButton(i, content); return createButton(appState, small, i, content);
} }
} }

@ -12,7 +12,7 @@ class ThemeSimple extends SharedTheme {
@override @override
String get name => 'Simple'; String get name => 'Simple';
ThemeSimple(AppState proxy) : super(proxy); const ThemeSimple();
@override @override
Color get puzzleThemeBackground => Colors.white; Color get puzzleThemeBackground => Colors.white;
@ -24,7 +24,8 @@ class ThemeSimple extends SharedTheme {
Color get puzzleAccentColor => _accentBlue; Color get puzzleAccentColor => _accentBlue;
@override @override
RoundedRectangleBorder get puzzleBorder => const RoundedRectangleBorder( RoundedRectangleBorder puzzleBorder(bool small) =>
const RoundedRectangleBorder(
side: BorderSide(color: Colors.black26, width: 1), side: BorderSide(color: Colors.black26, width: 1),
borderRadius: BorderRadius.all( borderRadius: BorderRadius.all(
Radius.circular(4), Radius.circular(4),
@ -32,9 +33,9 @@ class ThemeSimple extends SharedTheme {
); );
@override @override
Widget tileButton(int i) { Widget tileButton(int i, AppState appState, bool small) {
if (i == puzzle.tileCount) { if (i == appState.puzzle.tileCount) {
assert(puzzle.solved); assert(appState.puzzle.solved);
return const Center( return const Center(
child: Icon( child: Icon(
Icons.thumb_up, Icons.thumb_up,
@ -44,7 +45,7 @@ class ThemeSimple extends SharedTheme {
); );
} }
final correctPosition = puzzle.isCorrectPosition(i); final correctPosition = appState.puzzle.isCorrectPosition(i);
final content = createInk( final content = createInk(
Center( Center(
@ -60,6 +61,8 @@ class ThemeSimple extends SharedTheme {
); );
return createButton( return createButton(
appState,
small,
i, i,
content, content,
color: const Color.fromARGB(255, 13, 87, 155), color: const Color.fromARGB(255, 13, 87, 155),

@ -0,0 +1,9 @@
import 'theme_plaster.dart';
import 'theme_seattle.dart';
import 'theme_simple.dart';
const themes = [
ThemeSimple(),
ThemeSeattle(),
ThemePlaster(),
];

@ -334,6 +334,15 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.13.11" version: "0.13.11"
provider:
dependency: "direct main"
description:
path: "."
ref: flutter_web
resolved-ref: "5cf4521d4d635d7d7ca8ddbd6e28048a7f319ee0"
url: "https://github.com/kevmoo/provider"
source: git
version: "2.1.0"
pub_semver: pub_semver:
dependency: transitive dependency: transitive
description: description:

@ -6,6 +6,7 @@ environment:
dependencies: dependencies:
flutter_web: any flutter_web: any
flutter_web_ui: any flutter_web_ui: any
provider: any
dev_dependencies: dev_dependencies:
pedantic: ^1.3.0 pedantic: ^1.3.0
@ -24,3 +25,7 @@ dependency_overrides:
git: git:
url: https://github.com/flutter/flutter_web url: https://github.com/flutter/flutter_web
path: packages/flutter_web_ui path: packages/flutter_web_ui
provider:
git:
url: https://github.com/kevmoo/provider
ref: flutter_web

Loading…
Cancel
Save