mirror of https://github.com/flutter/samples.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
4.6 KiB
175 lines
4.6 KiB
6 years ago
|
// Copyright 2018 The Flutter team. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
// found in the LICENSE file.
|
||
|
|
||
6 years ago
|
import 'dart:ui';
|
||
|
|
||
6 years ago
|
import 'package:flutter/cupertino.dart';
|
||
|
import 'package:veggieseasons/data/veggie.dart';
|
||
|
import 'package:veggieseasons/screens/details.dart';
|
||
|
import 'package:veggieseasons/styles.dart';
|
||
|
|
||
6 years ago
|
class FrostyBackground extends StatelessWidget {
|
||
|
const FrostyBackground({
|
||
|
this.color,
|
||
|
this.intensity = 25,
|
||
|
this.child,
|
||
|
});
|
||
|
|
||
|
final Color color;
|
||
|
final double intensity;
|
||
|
final Widget child;
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return ClipRect(
|
||
|
child: BackdropFilter(
|
||
|
filter: ImageFilter.blur(sigmaX: intensity, sigmaY: intensity),
|
||
|
child: DecoratedBox(
|
||
|
decoration: BoxDecoration(
|
||
|
color: color,
|
||
|
),
|
||
|
child: child,
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
/// A Card-like Widget that responds to tap events by animating changes to its
|
||
|
/// elevation and invoking an optional [onPressed] callback.
|
||
|
class PressableCard extends StatefulWidget {
|
||
|
const PressableCard({
|
||
|
@required this.child,
|
||
6 years ago
|
this.borderRadius = const BorderRadius.all(Radius.circular(5)),
|
||
|
this.upElevation = 2,
|
||
|
this.downElevation = 0,
|
||
6 years ago
|
this.shadowColor = CupertinoColors.black,
|
||
|
this.duration = const Duration(milliseconds: 100),
|
||
|
this.onPressed,
|
||
|
Key key,
|
||
|
}) : assert(child != null),
|
||
|
assert(borderRadius != null),
|
||
|
assert(upElevation != null),
|
||
|
assert(downElevation != null),
|
||
|
assert(shadowColor != null),
|
||
|
assert(duration != null),
|
||
|
super(key: key);
|
||
|
|
||
|
final VoidCallback onPressed;
|
||
|
|
||
|
final Widget child;
|
||
|
|
||
|
final BorderRadius borderRadius;
|
||
|
|
||
|
final double upElevation;
|
||
|
|
||
|
final double downElevation;
|
||
|
|
||
|
final Color shadowColor;
|
||
|
|
||
|
final Duration duration;
|
||
|
|
||
|
@override
|
||
|
_PressableCardState createState() => _PressableCardState();
|
||
|
}
|
||
|
|
||
|
class _PressableCardState extends State<PressableCard> {
|
||
|
bool cardIsDown = false;
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return GestureDetector(
|
||
|
onTap: () {
|
||
|
setState(() => cardIsDown = false);
|
||
|
if (widget.onPressed != null) {
|
||
|
widget.onPressed();
|
||
|
}
|
||
|
},
|
||
|
onTapDown: (details) => setState(() => cardIsDown = true),
|
||
|
onTapCancel: () => setState(() => cardIsDown = false),
|
||
|
child: AnimatedPhysicalModel(
|
||
|
elevation: cardIsDown ? widget.downElevation : widget.upElevation,
|
||
|
borderRadius: widget.borderRadius,
|
||
|
shape: BoxShape.rectangle,
|
||
|
shadowColor: widget.shadowColor,
|
||
|
duration: widget.duration,
|
||
|
color: CupertinoColors.lightBackgroundGray,
|
||
|
child: ClipRRect(
|
||
|
borderRadius: widget.borderRadius,
|
||
|
child: widget.child,
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
class VeggieCard extends StatelessWidget {
|
||
6 years ago
|
VeggieCard(this.veggie, this.isInSeason, this.isPreferredCategory);
|
||
6 years ago
|
|
||
|
/// Veggie to be displayed by the card.
|
||
|
final Veggie veggie;
|
||
|
|
||
6 years ago
|
/// If the veggie is in season, it's displayed more prominently and the
|
||
|
/// image is fully saturated. Otherwise, it's reduced and de-saturated.
|
||
|
final bool isInSeason;
|
||
|
|
||
6 years ago
|
/// Whether [veggie] falls into one of user's preferred [VeggieCategory]s
|
||
|
final bool isPreferredCategory;
|
||
|
|
||
6 years ago
|
Widget _buildDetails() {
|
||
6 years ago
|
return FrostyBackground(
|
||
|
color: Color(0x90ffffff),
|
||
6 years ago
|
child: Padding(
|
||
6 years ago
|
padding: const EdgeInsets.all(16),
|
||
6 years ago
|
child: Column(
|
||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||
|
children: <Widget>[
|
||
|
Text(
|
||
|
veggie.name,
|
||
|
style: Styles.cardTitleText,
|
||
|
),
|
||
|
Text(
|
||
|
veggie.shortDescription,
|
||
|
style: Styles.cardDescriptionText,
|
||
|
),
|
||
|
],
|
||
6 years ago
|
),
|
||
|
),
|
||
6 years ago
|
);
|
||
6 years ago
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
6 years ago
|
return PressableCard(
|
||
4 years ago
|
onPressed: () => DetailsScreen.show(Navigator.of(context), veggie.id),
|
||
6 years ago
|
child: Stack(
|
||
|
children: [
|
||
6 years ago
|
Semantics(
|
||
|
label: 'A card background featuring ${veggie.name}',
|
||
|
child: Container(
|
||
6 years ago
|
height: isInSeason ? 300 : 150,
|
||
6 years ago
|
decoration: BoxDecoration(
|
||
|
image: DecorationImage(
|
||
|
fit: BoxFit.cover,
|
||
6 years ago
|
colorFilter:
|
||
|
isInSeason ? null : Styles.desaturatedColorFilter,
|
||
6 years ago
|
image: AssetImage(
|
||
|
veggie.imageAssetPath,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
6 years ago
|
),
|
||
|
Positioned(
|
||
6 years ago
|
bottom: 0,
|
||
|
left: 0,
|
||
|
right: 0,
|
||
6 years ago
|
child: _buildDetails(),
|
||
|
),
|
||
|
],
|
||
6 years ago
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|