1
0
mirror of https://github.com/flutter/samples.git synced 2026-04-23 15:31:29 +00:00

veggieseasons: Migrate to null safety (#922)

This commit is contained in:
Brett Morgan
2021-10-12 10:27:54 +11:00
committed by GitHub
parent 994cdb4afa
commit ac2bef7d83
21 changed files with 134 additions and 148 deletions

View File

@@ -12,10 +12,10 @@ import 'package:veggieseasons/styles.dart';
class FrostedBox extends StatelessWidget {
const FrostedBox({
this.child,
Key key,
Key? key,
}) : super(key: key);
final Widget child;
final Widget? child;
@override
Widget build(BuildContext context) {
@@ -37,18 +37,15 @@ class ColorChangingIcon extends ImplicitlyAnimatedWidget {
this.icon, {
this.color = CupertinoColors.black,
this.size,
@required Duration duration,
Key key,
}) : assert(icon != null),
assert(color != null),
assert(duration != null),
super(key: key, duration: duration);
required Duration duration,
Key? key,
}) : super(key: key, duration: duration);
final Color color;
final IconData icon;
final double size;
final double? size;
@override
_ColorChangingIconState createState() => _ColorChangingIconState();
@@ -56,7 +53,7 @@ class ColorChangingIcon extends ImplicitlyAnimatedWidget {
class _ColorChangingIconState
extends AnimatedWidgetBaseState<ColorChangingIcon> {
ColorTween _colorTween;
ColorTween? _colorTween;
@override
Widget build(BuildContext context) {
@@ -73,14 +70,14 @@ class _ColorChangingIconState
_colorTween = visitor(
_colorTween,
widget.color,
(dynamic value) => ColorTween(begin: value as Color),
) as ColorTween;
(dynamic value) => ColorTween(begin: value as Color?),
) as ColorTween?;
}
}
/// A simple "close this modal" button that invokes a callback when pressed.
class CloseButton extends StatefulWidget {
const CloseButton(this.onPressed, {Key key}) : super(key: key);
const CloseButton(this.onPressed, {Key? key}) : super(key: key);
final VoidCallback onPressed;

View File

@@ -14,7 +14,7 @@ import 'settings_item.dart';
// See https://github.com/flutter/flutter/projects/29 for more info.
class SettingsGroupHeader extends StatelessWidget {
const SettingsGroupHeader(this.title, {Key key}) : super(key: key);
const SettingsGroupHeader(this.title, {Key? key}) : super(key: key);
final String title;
@@ -35,7 +35,7 @@ class SettingsGroupHeader extends StatelessWidget {
}
class SettingsGroupFooter extends StatelessWidget {
const SettingsGroupFooter(this.title, {Key key}) : super(key: key);
const SettingsGroupFooter(this.title, {Key? key}) : super(key: key);
final String title;
@@ -55,17 +55,16 @@ class SettingsGroupFooter extends StatelessWidget {
class SettingsGroup extends StatelessWidget {
SettingsGroup({
@required this.items,
required this.items,
this.header,
this.footer,
Key key,
}) : assert(items != null),
assert(items.isNotEmpty),
Key? key,
}) : assert(items.isNotEmpty),
super(key: key);
final List<SettingsItem> items;
final Widget header;
final Widget footer;
final Widget? header;
final Widget? footer;
@override
Widget build(BuildContext context) {
var brightness = CupertinoTheme.brightnessOf(context);
@@ -85,7 +84,7 @@ class SettingsGroup extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (header != null) header,
if (header != null) header!,
Container(
decoration: BoxDecoration(
color: CupertinoColors.white,
@@ -105,7 +104,7 @@ class SettingsGroup extends StatelessWidget {
children: dividedItems,
),
),
if (footer != null) footer,
if (footer != null) footer!,
],
),
);

View File

@@ -16,7 +16,7 @@ import 'package:veggieseasons/styles.dart';
typedef SettingsItemCallback = FutureOr<void> Function();
class SettingsNavigationIndicator extends StatelessWidget {
const SettingsNavigationIndicator({Key key}) : super(key: key);
const SettingsNavigationIndicator({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
@@ -30,12 +30,11 @@ class SettingsNavigationIndicator extends StatelessWidget {
class SettingsIcon extends StatelessWidget {
const SettingsIcon({
@required this.icon,
required this.icon,
this.foregroundColor = CupertinoColors.white,
this.backgroundColor = CupertinoColors.black,
Key key,
}) : assert(icon != null),
super(key: key);
Key? key,
}) : super(key: key);
final Color backgroundColor;
final Color foregroundColor;
@@ -61,20 +60,19 @@ class SettingsIcon extends StatelessWidget {
class SettingsItem extends StatefulWidget {
const SettingsItem({
@required this.label,
required this.label,
this.icon,
this.content,
this.subtitle,
this.onPress,
Key key,
}) : assert(label != null),
super(key: key);
Key? key,
}) : super(key: key);
final String label;
final Widget icon;
final Widget content;
final String subtitle;
final SettingsItemCallback onPress;
final Widget? icon;
final Widget? content;
final String? subtitle;
final SettingsItemCallback? onPress;
@override
State<StatefulWidget> createState() => SettingsItemState();
@@ -97,7 +95,7 @@ class SettingsItemState extends State<SettingsItem> {
setState(() {
pressed = true;
});
await widget.onPress();
await widget.onPress!();
Future.delayed(
const Duration(milliseconds: 150),
() {
@@ -138,7 +136,7 @@ class SettingsItemState extends State<SettingsItem> {
style: themeData.textTheme.textStyle),
const SizedBox(height: 4),
Text(
widget.subtitle,
widget.subtitle!,
style: Styles.settingsItemSubtitleText(themeData),
),
],

View File

@@ -8,10 +8,10 @@ import 'package:veggieseasons/styles.dart';
/// Presents a series of trivia questions about a particular widget, and tracks
/// the user's score.
class TriviaView extends StatefulWidget {
final int id;
final String restorationId;
final int? id;
final String? restorationId;
const TriviaView({this.id, this.restorationId, Key key}) : super(key: key);
const TriviaView({this.id, this.restorationId, Key? key}) : super(key: key);
@override
_TriviaViewState createState() => _TriviaViewState();
@@ -26,10 +26,10 @@ enum PlayerStatus {
class _TriviaViewState extends State<TriviaView> with RestorationMixin {
/// Current app state. This is used to fetch veggie data.
AppState appState;
late AppState appState;
/// The veggie trivia about which to show.
Veggie veggie;
late Veggie veggie;
/// Index of the current trivia question.
RestorableInt triviaIndex = RestorableInt(0);
@@ -45,10 +45,10 @@ class _TriviaViewState extends State<TriviaView> with RestorationMixin {
_RestorablePlayerStatus(PlayerStatus.readyToAnswer);
@override
String get restorationId => widget.restorationId;
String? get restorationId => widget.restorationId;
@override
void restoreState(RestorationBucket oldBucket, bool initialRestore) {
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(triviaIndex, 'index');
registerForRestoration(score, 'score');
registerForRestoration(status, 'status');
@@ -234,7 +234,7 @@ class _RestorablePlayerStatus extends RestorableValue<PlayerStatus> {
}
@override
PlayerStatus fromPrimitives(Object data) {
PlayerStatus fromPrimitives(Object? data) {
return PlayerStatus.values[data as int];
}
@@ -244,7 +244,7 @@ class _RestorablePlayerStatus extends RestorableValue<PlayerStatus> {
}
@override
void didUpdateValue(PlayerStatus oldValue) {
void didUpdateValue(PlayerStatus? oldValue) {
notifyListeners();
}
}

View File

@@ -14,12 +14,12 @@ class FrostyBackground extends StatelessWidget {
this.color,
this.intensity = 25,
this.child,
Key key,
Key? key,
}) : super(key: key);
final Color color;
final Color? color;
final double intensity;
final Widget child;
final Widget? child;
@override
Widget build(BuildContext context) {
@@ -41,23 +41,17 @@ class FrostyBackground extends StatelessWidget {
/// elevation and invoking an optional [onPressed] callback.
class PressableCard extends StatefulWidget {
const PressableCard({
@required this.child,
required this.child,
this.borderRadius = const BorderRadius.all(Radius.circular(5)),
this.upElevation = 2,
this.downElevation = 0,
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);
Key? key,
}) : super(key: key);
final VoidCallback onPressed;
final VoidCallback? onPressed;
final Widget child;
@@ -84,7 +78,7 @@ class _PressableCardState extends State<PressableCard> {
onTap: () {
setState(() => cardIsDown = false);
if (widget.onPressed != null) {
widget.onPressed();
widget.onPressed!();
}
},
onTapDown: (details) => setState(() => cardIsDown = true),
@@ -107,7 +101,7 @@ class _PressableCardState extends State<PressableCard> {
class VeggieCard extends StatelessWidget {
const VeggieCard(this.veggie, this.isInSeason, this.isPreferredCategory,
{Key key})
{Key? key})
: super(key: key);
/// Veggie to be displayed by the card.

View File

@@ -8,17 +8,17 @@ import 'package:veggieseasons/screens/details.dart';
import 'package:veggieseasons/styles.dart';
class ZoomClipAssetImage extends StatelessWidget {
const ZoomClipAssetImage(
{@required this.zoom,
this.height,
this.width,
@required this.imageAsset,
Key key})
: super(key: key);
const ZoomClipAssetImage({
required this.zoom,
this.height,
this.width,
required this.imageAsset,
Key? key,
}) : super(key: key);
final double zoom;
final double height;
final double width;
final double? height;
final double? width;
final String imageAsset;
@override
@@ -30,8 +30,8 @@ class ZoomClipAssetImage extends StatelessWidget {
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: OverflowBox(
maxHeight: height * zoom,
maxWidth: width * zoom,
maxHeight: height! * zoom,
maxWidth: width! * zoom,
child: Image.asset(
imageAsset,
fit: BoxFit.fill,
@@ -45,7 +45,7 @@ class ZoomClipAssetImage extends StatelessWidget {
class VeggieHeadline extends StatelessWidget {
final Veggie veggie;
const VeggieHeadline(this.veggie, {Key key}) : super(key: key);
const VeggieHeadline(this.veggie, {Key? key}) : super(key: key);
List<Widget> _buildSeasonDots(List<Season> seasons) {
var widgets = <Widget>[];