mirror of
https://github.com/flutter/samples.git
synced 2026-03-31 16:55:34 +00:00
slide_puzzle: use pkg:provider
This commit is contained in:
@@ -2,32 +2,31 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'app_state.dart';
|
||||
import 'core/puzzle_animator.dart';
|
||||
import 'flutter.dart';
|
||||
import 'puzzle_flow_delegate.dart';
|
||||
import 'themes.dart';
|
||||
import 'widgets/material_interior_alt.dart';
|
||||
|
||||
abstract class SharedTheme {
|
||||
SharedTheme(this._appState);
|
||||
|
||||
final AppState _appState;
|
||||
|
||||
PuzzleProxy get puzzle => _appState.puzzle;
|
||||
const SharedTheme();
|
||||
|
||||
String get name;
|
||||
|
||||
Color get puzzleThemeBackground;
|
||||
|
||||
RoundedRectangleBorder get puzzleBorder;
|
||||
RoundedRectangleBorder puzzleBorder(bool small);
|
||||
|
||||
Color get puzzleBackgroundColor;
|
||||
|
||||
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(
|
||||
Widget child, {
|
||||
@@ -43,6 +42,8 @@ abstract class SharedTheme {
|
||||
);
|
||||
|
||||
Widget createButton(
|
||||
AppState appState,
|
||||
bool small,
|
||||
int tileValue,
|
||||
Widget content, {
|
||||
Color color,
|
||||
@@ -50,178 +51,164 @@ abstract class SharedTheme {
|
||||
}) =>
|
||||
AnimatedContainer(
|
||||
duration: _puzzleAnimationDuration,
|
||||
padding: tilePadding,
|
||||
padding: tilePadding(appState.puzzle),
|
||||
child: RaisedButton(
|
||||
elevation: 4,
|
||||
clipBehavior: Clip.hardEdge,
|
||||
animationDuration: _puzzleAnimationDuration,
|
||||
onPressed: () => _tilePress(tileValue),
|
||||
shape: shape ?? puzzleBorder,
|
||||
onPressed: () => appState.clickOrShake(tileValue),
|
||||
shape: shape ?? puzzleBorder(small),
|
||||
padding: const EdgeInsets.symmetric(),
|
||||
child: content,
|
||||
color: color,
|
||||
),
|
||||
);
|
||||
|
||||
double _previousConstraintWidth;
|
||||
bool _small;
|
||||
|
||||
bool get small => _small;
|
||||
|
||||
void _updateConstraints(BoxConstraints constraints) {
|
||||
Widget _updateConstraints(
|
||||
BoxConstraints constraints, Widget Function(bool small) builder) {
|
||||
const _smallWidth = 580;
|
||||
|
||||
final constraintWidth =
|
||||
constraints.hasBoundedWidth ? constraints.maxWidth : 1000.0;
|
||||
|
||||
if (constraintWidth == _previousConstraintWidth) {
|
||||
assert(_small != null);
|
||||
return;
|
||||
}
|
||||
|
||||
_previousConstraintWidth = constraintWidth;
|
||||
|
||||
if (_previousConstraintWidth < _smallWidth) {
|
||||
_small = true;
|
||||
} else {
|
||||
_small = false;
|
||||
}
|
||||
return builder(constraintWidth < _smallWidth);
|
||||
}
|
||||
|
||||
Widget build(BuildContext context, BoxConstraints constraints) {
|
||||
_updateConstraints(constraints);
|
||||
return Material(
|
||||
child: Stack(
|
||||
children: <Widget>[
|
||||
const SizedBox.expand(
|
||||
child: FittedBox(
|
||||
fit: BoxFit.cover,
|
||||
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,
|
||||
Widget build(BuildContext context, BoxConstraints constraints) =>
|
||||
_updateConstraints(
|
||||
constraints,
|
||||
(small) => Material(
|
||||
child: Stack(
|
||||
children: <Widget>[
|
||||
const SizedBox.expand(
|
||||
child: FittedBox(
|
||||
fit: BoxFit.cover,
|
||||
child: Image(
|
||||
image: AssetImage('seattle.jpg'),
|
||||
),
|
||||
),
|
||||
),
|
||||
AnimatedContainer(
|
||||
duration: _puzzleAnimationDuration,
|
||||
color: puzzleThemeBackground,
|
||||
child: Center(
|
||||
child: _styledWrapper(
|
||||
small,
|
||||
SizedBox(
|
||||
width: 580,
|
||||
child: Consumer<AppState>(
|
||||
builder: (context, appState, _) => 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:
|
||||
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)),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
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(),
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: Flow(
|
||||
delegate: PuzzleFlowDelegate(
|
||||
small ? const Size(90, 90) : const Size(140, 140),
|
||||
puzzle,
|
||||
_appState.animationNotifier,
|
||||
),
|
||||
children: List<Widget>.generate(
|
||||
puzzle.length,
|
||||
_tileButton,
|
||||
),
|
||||
),
|
||||
),
|
||||
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;
|
||||
|
||||
// Thought about using AnimatedContainer here, but it causes some weird
|
||||
// resizing behavior
|
||||
Widget _styledWrapper(Widget child) => MaterialInterior(
|
||||
Widget _styledWrapper(bool small, Widget child) => MaterialInterior(
|
||||
duration: _puzzleAnimationDuration,
|
||||
shape: puzzleBorder,
|
||||
shape: puzzleBorder(small),
|
||||
color: puzzleBackgroundColor,
|
||||
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(
|
||||
color: puzzleAccentColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
);
|
||||
|
||||
List<Widget> _bottomControls(BuildContext context) => <Widget>[
|
||||
List<Widget> _bottomControls(AppState appState) => <Widget>[
|
||||
IconButton(
|
||||
onPressed: puzzle.reset,
|
||||
onPressed: appState.puzzle.reset,
|
||||
icon: Icon(Icons.refresh, color: puzzleAccentColor),
|
||||
//Icons.refresh,
|
||||
),
|
||||
Checkbox(
|
||||
value: _appState.autoPlay,
|
||||
onChanged: _setAutoPlay,
|
||||
value: appState.autoPlay,
|
||||
onChanged: appState.setAutoPlayFunction,
|
||||
activeColor: puzzleAccentColor,
|
||||
),
|
||||
Expanded(
|
||||
child: Container(),
|
||||
),
|
||||
Text(
|
||||
puzzle.clickCount.toString(),
|
||||
appState.puzzle.clickCount.toString(),
|
||||
textAlign: TextAlign.right,
|
||||
style: _infoStyle,
|
||||
),
|
||||
@@ -229,7 +216,7 @@ abstract class SharedTheme {
|
||||
SizedBox(
|
||||
width: 28,
|
||||
child: Text(
|
||||
puzzle.incorrectTiles.toString(),
|
||||
appState.puzzle.incorrectTiles.toString(),
|
||||
textAlign: TextAlign.right,
|
||||
style: _infoStyle,
|
||||
),
|
||||
@@ -237,11 +224,11 @@ abstract class SharedTheme {
|
||||
const Text(' Tiles left ')
|
||||
];
|
||||
|
||||
Widget _tileButton(int i) {
|
||||
if (i == puzzle.tileCount && !puzzle.solved) {
|
||||
Widget _tileButton(int i, AppState appState, bool small) {
|
||||
if (i == appState.puzzle.tileCount && !appState.puzzle.solved) {
|
||||
return const Center();
|
||||
}
|
||||
|
||||
return tileButton(i);
|
||||
return tileButton(i, appState, small);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user