From 867698439f855f1fc8fd79606eab009e6a1eb202 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Thu, 24 Dec 2020 09:44:02 +0800 Subject: [PATCH] [NNBD] Migrate sample code pt 5 (#72845) --- .../lib/src/widgets/animated_list.dart | 86 +++++++++---------- .../lib/src/widgets/animated_size.dart | 2 +- .../flutter/lib/src/widgets/autofill.dart | 8 +- .../flutter/lib/src/widgets/dismissible.dart | 2 +- .../flutter/lib/src/widgets/focus_scope.dart | 31 ++++--- .../flutter/lib/src/widgets/overflow_bar.dart | 2 +- .../flutter/lib/src/widgets/sliver_fill.dart | 8 +- 7 files changed, 69 insertions(+), 70 deletions(-) diff --git a/packages/flutter/lib/src/widgets/animated_list.dart b/packages/flutter/lib/src/widgets/animated_list.dart index 1e2f63da0b..0a982e8c7d 100644 --- a/packages/flutter/lib/src/widgets/animated_list.dart +++ b/packages/flutter/lib/src/widgets/animated_list.dart @@ -48,7 +48,7 @@ class _ActiveItem implements Comparable<_ActiveItem> { /// /// {@youtube 560 315 https://www.youtube.com/watch?v=ZtfItHwFlZ8} /// -/// {@tool dartpad --template=freeform_no_null_safety} +/// {@tool dartpad --template=freeform} /// This sample application uses an [AnimatedList] to create an effect when /// items are removed or added to the list. /// @@ -65,9 +65,9 @@ class _ActiveItem implements Comparable<_ActiveItem> { /// /// class _AnimatedListSampleState extends State { /// final GlobalKey _listKey = GlobalKey(); -/// ListModel _list; -/// int _selectedItem; -/// int _nextItem; // The next item inserted when the user presses the '+' button. +/// late ListModel _list; +/// int? _selectedItem; +/// late int _nextItem; // The next item inserted when the user presses the '+' button. /// /// @override /// void initState() { @@ -111,14 +111,14 @@ class _ActiveItem implements Comparable<_ActiveItem> { /// /// // Insert the "next item" into the list model. /// void _insert() { -/// final int index = _selectedItem == null ? _list.length : _list.indexOf(_selectedItem); +/// final int index = _selectedItem == null ? _list.length : _list.indexOf(_selectedItem!); /// _list.insert(index, _nextItem++); /// } /// /// // Remove the selected item from the list model. /// void _remove() { /// if (_selectedItem != null) { -/// _list.removeAt(_list.indexOf(_selectedItem)); +/// _list.removeAt(_list.indexOf(_selectedItem!)); /// setState(() { /// _selectedItem = null; /// }); @@ -168,30 +168,30 @@ class _ActiveItem implements Comparable<_ActiveItem> { /// /// of [AnimatedListState.insertItem] and [AnimatedList.removeItem]. /// class ListModel { /// ListModel({ -/// @required this.listKey, -/// @required this.removedItemBuilder, -/// Iterable initialItems, -/// }) : assert(listKey != null), -/// assert(removedItemBuilder != null), -/// _items = List.from(initialItems ?? []); +/// required this.listKey, +/// required this.removedItemBuilder, +/// Iterable? initialItems, +/// }) : _items = List.from(initialItems ?? []); /// /// final GlobalKey listKey; /// final dynamic removedItemBuilder; /// final List _items; /// -/// AnimatedListState get _animatedList => listKey.currentState; +/// AnimatedListState? get _animatedList => listKey.currentState; /// /// void insert(int index, E item) { /// _items.insert(index, item); -/// _animatedList.insertItem(index); +/// _animatedList!.insertItem(index); /// } /// /// E removeAt(int index) { /// final E removedItem = _items.removeAt(index); /// if (removedItem != null) { -/// _animatedList.removeItem( +/// _animatedList!.removeItem( /// index, -/// (BuildContext context, Animation animation) => removedItemBuilder(removedItem, context, animation), +/// (BuildContext context, Animation animation) { +/// return removedItemBuilder(removedItem, context, animation); +/// }, /// ); /// } /// return removedItem; @@ -212,24 +212,22 @@ class _ActiveItem implements Comparable<_ActiveItem> { /// /// varies from 0 to 128 as the animation varies from 0.0 to 1.0. /// class CardItem extends StatelessWidget { /// const CardItem({ -/// Key key, -/// @required this.animation, +/// Key? key, /// this.onTap, -/// @required this.item, -/// this.selected: false -/// }) : assert(animation != null), -/// assert(item != null && item >= 0), -/// assert(selected != null), +/// this.selected = false, +/// required this.animation, +/// required this.item, +/// }) : assert(item >= 0), /// super(key: key); /// /// final Animation animation; -/// final VoidCallback onTap; +/// final VoidCallback? onTap; /// final int item; /// final bool selected; /// /// @override /// Widget build(BuildContext context) { -/// TextStyle textStyle = Theme.of(context).textTheme.headline4; +/// TextStyle textStyle = Theme.of(context).textTheme.headline4!; /// if (selected) /// textStyle = textStyle.copyWith(color: Colors.lightGreenAccent[400]); /// return Padding( @@ -517,7 +515,7 @@ class AnimatedListState extends State with TickerProviderStateMixi /// [GlobalKey] or use the static [SliverAnimatedList.of] method from an item's /// input callback. /// -/// {@tool dartpad --template=freeform_no_null_safety} +/// {@tool dartpad --template=freeform} /// This sample application uses a [SliverAnimatedList] to create an animated /// effect when items are removed or added to the list. /// @@ -538,9 +536,9 @@ class AnimatedListState extends State with TickerProviderStateMixi /// final GlobalKey _listKey = GlobalKey(); /// final GlobalKey _scaffoldKey = GlobalKey(); /// final GlobalKey _scaffoldMessengerKey = GlobalKey(); -/// ListModel _list; -/// int _selectedItem; -/// int _nextItem; // The next item inserted when the user presses the '+' button. +/// late ListModel _list; +/// int? _selectedItem; +/// late int _nextItem; // The next item inserted when the user presses the '+' button. /// /// @override /// void initState() { @@ -583,19 +581,19 @@ class AnimatedListState extends State with TickerProviderStateMixi /// /// // Insert the "next item" into the list model. /// void _insert() { -/// final int index = _selectedItem == null ? _list.length : _list.indexOf(_selectedItem); +/// final int index = _selectedItem == null ? _list.length : _list.indexOf(_selectedItem!); /// _list.insert(index, _nextItem++); /// } /// /// // Remove the selected item from the list model. /// void _remove() { /// if (_selectedItem != null) { -/// _list.removeAt(_list.indexOf(_selectedItem)); +/// _list.removeAt(_list.indexOf(_selectedItem!)); /// setState(() { /// _selectedItem = null; /// }); /// } else { -/// _scaffoldMessengerKey.currentState.showSnackBar(SnackBar( +/// _scaffoldMessengerKey.currentState!.showSnackBar(SnackBar( /// content: Text( /// 'Select an item to remove from the list.', /// style: TextStyle(fontSize: 20), @@ -658,18 +656,16 @@ class AnimatedListState extends State with TickerProviderStateMixi /// // of [AnimatedListState.insertItem] and [AnimatedList.removeItem]. /// class ListModel { /// ListModel({ -/// @required this.listKey, -/// @required this.removedItemBuilder, -/// Iterable initialItems, -/// }) : assert(listKey != null), -/// assert(removedItemBuilder != null), -/// _items = List.from(initialItems ?? []); +/// required this.listKey, +/// required this.removedItemBuilder, +/// Iterable? initialItems, +/// }) : _items = List.from(initialItems ?? []); /// /// final GlobalKey listKey; /// final dynamic removedItemBuilder; /// final List _items; /// -/// SliverAnimatedListState get _animatedList => listKey.currentState; +/// SliverAnimatedListState get _animatedList => listKey.currentState!; /// /// void insert(int index, E item) { /// _items.insert(index, item); @@ -702,18 +698,16 @@ class AnimatedListState extends State with TickerProviderStateMixi /// // transitions from 0.0 to 1.0. /// class CardItem extends StatelessWidget { /// const CardItem({ -/// Key key, -/// @required this.animation, -/// @required this.item, +/// Key? key, /// this.onTap, /// this.selected = false, -/// }) : assert(animation != null), -/// assert(item != null && item >= 0), -/// assert(selected != null), +/// required this.animation, +/// required this.item, +/// }) : assert(item >= 0), /// super(key: key); /// /// final Animation animation; -/// final VoidCallback onTap; +/// final VoidCallback? onTap; /// final int item; /// final bool selected; /// diff --git a/packages/flutter/lib/src/widgets/animated_size.dart b/packages/flutter/lib/src/widgets/animated_size.dart index da4b6f2fa6..ab7bc0ea5a 100644 --- a/packages/flutter/lib/src/widgets/animated_size.dart +++ b/packages/flutter/lib/src/widgets/animated_size.dart @@ -11,7 +11,7 @@ import 'framework.dart'; /// Animated widget that automatically transitions its size over a given /// duration whenever the given child's size changes. /// -/// {@tool dartpad --template=stateful_widget_scaffold_center_freeform_state_no_null_safety} +/// {@tool dartpad --template=stateful_widget_scaffold_center_freeform_state} /// This example makes a [Container] react to being touched, causing the child /// of the [AnimatedSize] widget, here a [FlutterLogo], to animate. /// diff --git a/packages/flutter/lib/src/widgets/autofill.dart b/packages/flutter/lib/src/widgets/autofill.dart index 6812def69a..489a2eb777 100644 --- a/packages/flutter/lib/src/widgets/autofill.dart +++ b/packages/flutter/lib/src/widgets/autofill.dart @@ -51,7 +51,7 @@ enum AutofillContextAction { /// autofillable input fields in an [AutofillGroup], so the user input of the /// [Form] can be saved for future autofill by the platform. /// -/// {@tool dartpad --template=stateful_widget_scaffold_no_null_safety} +/// {@tool dartpad --template=stateful_widget_scaffold} /// /// An example form with autofillable fields grouped into different /// `AutofillGroup`s. @@ -92,8 +92,10 @@ enum AutofillContextAction { /// const Text('Billing address'), /// Checkbox( /// value: isSameAddress, -/// onChanged: (bool newValue) { -/// setState(() { isSameAddress = newValue; }); +/// onChanged: (bool? newValue) { +/// if (newValue != null) { +/// setState(() { isSameAddress = newValue; }); +/// } /// }, /// ), /// // Again the address fields are grouped together for the same reason. diff --git a/packages/flutter/lib/src/widgets/dismissible.dart b/packages/flutter/lib/src/widgets/dismissible.dart index ba69a5aab6..ef1d9eee66 100644 --- a/packages/flutter/lib/src/widgets/dismissible.dart +++ b/packages/flutter/lib/src/widgets/dismissible.dart @@ -65,7 +65,7 @@ enum DismissDirection { /// /// {@youtube 560 315 https://www.youtube.com/watch?v=iEMgjrfuc58} /// -/// {@tool dartpad --template=stateful_widget_scaffold_no_null_safety} +/// {@tool dartpad --template=stateful_widget_scaffold} /// /// This sample shows how you can use the [Dismissible] widget to /// remove list items using swipe gestures. Swipe any of the list diff --git a/packages/flutter/lib/src/widgets/focus_scope.dart b/packages/flutter/lib/src/widgets/focus_scope.dart index 86602a8a42..ce4520673a 100644 --- a/packages/flutter/lib/src/widgets/focus_scope.dart +++ b/packages/flutter/lib/src/widgets/focus_scope.dart @@ -48,7 +48,7 @@ import 'inherited_notifier.dart'; /// the focus traversal order, call `Focus.of(context).nextFocus()`. To unfocus /// a widget, call `Focus.of(context).unfocus()`. /// -/// {@tool dartpad --template=stateful_widget_scaffold_no_null_safety} +/// {@tool dartpad --template=stateful_widget_scaffold} /// This example shows how to manage focus using the [Focus] and [FocusScope] /// widgets. See [FocusNode] for a similar example that doesn't use [Focus] or /// [FocusScope]. @@ -93,7 +93,7 @@ import 'inherited_notifier.dart'; /// debugLabel: 'Scope', /// autofocus: true, /// child: DefaultTextStyle( -/// style: textTheme.headline4, +/// style: textTheme.headline4!, /// child: Focus( /// onKey: _handleKeyPress, /// debugLabel: 'Button', @@ -128,7 +128,7 @@ import 'inherited_notifier.dart'; /// ``` /// {@end-tool} /// -/// {@tool dartpad --template=stateless_widget_material_no_null_safety} +/// {@tool dartpad --template=stateless_widget_material} /// This example shows how to wrap another widget in a [Focus] widget to make it /// focusable. It wraps a [Container], and changes the container's color when it /// is set as the [FocusManager.primaryFocus]. @@ -139,7 +139,10 @@ import 'inherited_notifier.dart'; /// /// ```dart preamble /// class FocusableText extends StatelessWidget { -/// const FocusableText(this.data, {Key key, this.autofocus}) : super(key: key); +/// const FocusableText(this.data, { +/// Key? key, +/// required this.autofocus, +/// }) : super(key: key); /// /// /// The string to display as the text for this widget. /// final String data; @@ -186,7 +189,7 @@ import 'inherited_notifier.dart'; /// ``` /// {@end-tool} /// -/// {@tool dartpad --template=stateful_widget_material_no_null_safety} +/// {@tool dartpad --template=stateful_widget_material} /// This example shows how to focus a newly-created widget immediately after it /// is created. /// @@ -738,7 +741,7 @@ class _FocusState extends State { /// the focus traversal order, call `Focus.of(context).nextFocus()`. To unfocus /// a widget, call `Focus.of(context).unfocus()`. /// -/// {@tool dartpad --template=stateful_widget_material_no_null_safety} +/// {@tool dartpad --template=stateful_widget_material} /// This example demonstrates using a [FocusScope] to restrict focus to a particular /// portion of the app. In this case, restricting focus to the visible part of a /// Stack. @@ -749,19 +752,19 @@ class _FocusState extends State { /// /// This is just a separate widget to simplify the example. /// class Pane extends StatelessWidget { /// const Pane({ -/// Key key, -/// this.focusNode, +/// Key? key, +/// required this.focusNode, /// this.onPressed, +/// required this.backgroundColor, +/// required this.icon, /// this.child, -/// this.backgroundColor, -/// this.icon, /// }) : super(key: key); /// /// final FocusNode focusNode; -/// final VoidCallback onPressed; -/// final Widget child; +/// final VoidCallback? onPressed; /// final Color backgroundColor; /// final Widget icon; +/// final Widget? child; /// /// @override /// Widget build(BuildContext context) { @@ -835,7 +838,7 @@ class _FocusState extends State { /// child: Text('ANOTHER BUTTON TO FOCUS'), /// ), /// DefaultTextStyle( -/// style: Theme.of(context).textTheme.headline2, +/// style: Theme.of(context).textTheme.headline2!, /// child: Text('BACKDROP')), /// ], /// ), @@ -864,7 +867,7 @@ class _FocusState extends State { /// ? null /// : () => setState(() => backdropIsVisible = true), /// child: DefaultTextStyle( -/// style: Theme.of(context).textTheme.headline2, +/// style: Theme.of(context).textTheme.headline2!, /// child: Text('FOREGROUND')), /// ), /// ), diff --git a/packages/flutter/lib/src/widgets/overflow_bar.dart b/packages/flutter/lib/src/widgets/overflow_bar.dart index 380fd770b9..fd45105d61 100644 --- a/packages/flutter/lib/src/widgets/overflow_bar.dart +++ b/packages/flutter/lib/src/widgets/overflow_bar.dart @@ -44,7 +44,7 @@ enum OverflowBarAlignment { /// If the layout overflows, then children's order within their /// column is specified by [overflowDirection] instead. /// -/// {@tool dartpad --template=stateless_widget_scaffold_center_no_null_safety} +/// {@tool dartpad --template=stateless_widget_scaffold_center} /// /// This example defines a simple approximation of a dialog /// layout, where the layout of the dialog's action buttons are diff --git a/packages/flutter/lib/src/widgets/sliver_fill.dart b/packages/flutter/lib/src/widgets/sliver_fill.dart index bdf5df8ab9..7084df6d61 100644 --- a/packages/flutter/lib/src/widgets/sliver_fill.dart +++ b/packages/flutter/lib/src/widgets/sliver_fill.dart @@ -203,7 +203,7 @@ class _RenderSliverFractionalPadding extends RenderSliverEdgeInsetsPadding { /// of space that has been scrolled beforehand has not exceeded the main axis /// extent of the viewport. /// -/// {@tool dartpad --template=stateless_widget_scaffold_no_null_safety} +/// {@tool dartpad --template=stateless_widget_scaffold} /// /// In this sample the [SliverFillRemaining] sizes its [child] to fill the /// remaining extent of the viewport in both axes. The icon is centered in the @@ -239,7 +239,7 @@ class _RenderSliverFractionalPadding extends RenderSliverEdgeInsetsPadding { /// [SliverFillRemaining] will defer to the size of its [child] if the /// child's size exceeds the remaining space in the viewport. /// -/// {@tool dartpad --template=stateless_widget_scaffold_no_null_safety} +/// {@tool dartpad --template=stateless_widget_scaffold} /// /// In this sample the [SliverFillRemaining] defers to the size of its [child] /// because the child's extent exceeds that of the remaining extent of the @@ -281,7 +281,7 @@ class _RenderSliverFractionalPadding extends RenderSliverEdgeInsetsPadding { /// [SliverFillRemaining] will defer to the size of its [child] if the /// [SliverConstraints.precedingScrollExtent] exceeded the length of the viewport's main axis. /// -/// {@tool dartpad --template=stateless_widget_scaffold_no_null_safety} +/// {@tool dartpad --template=stateless_widget_scaffold} /// /// In this sample the [SliverFillRemaining] defers to the size of its [child] /// because the [SliverConstraints.precedingScrollExtent] has gone @@ -330,7 +330,7 @@ class _RenderSliverFractionalPadding extends RenderSliverEdgeInsetsPadding { /// /// {@animation 250 500 https://flutter.github.io/assets-for-api-docs/assets/widgets/sliver_fill_remaining_fill_overscroll.mp4} /// -/// {@tool sample --template=stateless_widget_scaffold_no_null_safety} +/// {@tool sample --template=stateless_widget_scaffold} /// /// In this sample the [SliverFillRemaining]'s child stretches to fill the /// overscroll area when [fillOverscroll] is true. This sample also features a