Skip to content

Commit

Permalink
add key to constructors of public widgets (#47884)
Browse files Browse the repository at this point in the history
* add key to constructors of public widgets

* fix a test

* fix a test
  • Loading branch information
a14n committed Jan 6, 2020
1 parent 2ae5832 commit 1fc43d9
Show file tree
Hide file tree
Showing 53 changed files with 184 additions and 93 deletions.
4 changes: 3 additions & 1 deletion packages/flutter/lib/src/cupertino/action_sheet.dart
Expand Up @@ -265,12 +265,14 @@ class CupertinoActionSheetAction extends StatelessWidget {
///
/// The [child] and [onPressed] arguments must not be null.
const CupertinoActionSheetAction({
Key key,
@required this.onPressed,
this.isDefaultAction = false,
this.isDestructiveAction = false,
@required this.child,
}) : assert(child != null),
assert(onPressed != null);
assert(onPressed != null),
super(key: key);

/// The callback that is called when the button is tapped.
///
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/lib/src/cupertino/date_picker.dart
Expand Up @@ -230,6 +230,7 @@ class CupertinoDatePicker extends StatefulWidget {
///
/// [use24hFormat] decides whether 24 hour format is used. Defaults to false.
CupertinoDatePicker({
Key key,
this.mode = CupertinoDatePickerMode.dateAndTime,
@required this.onDateTimeChanged,
DateTime initialDateTime,
Expand All @@ -247,7 +248,8 @@ class CupertinoDatePicker extends StatefulWidget {
assert(
minuteInterval > 0 && 60 % minuteInterval == 0,
'minute interval is not a positive integer factor of 60',
) {
),
super(key: key) {
assert(this.initialDateTime != null);
assert(
mode != CupertinoDatePickerMode.dateAndTime || minimumDate == null || !this.initialDateTime.isBefore(minimumDate),
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/lib/src/cupertino/nav_bar.dart
Expand Up @@ -1270,11 +1270,13 @@ class CupertinoNavigationBarBackButton extends StatelessWidget {
///
/// The [color] parameter must not be null.
const CupertinoNavigationBarBackButton({
Key key,
this.color,
this.previousPageTitle,
this.onPressed,
}) : _backChevron = null,
_backLabel = null;
_backLabel = null,
super(key: key);

// Allow the back chevron and label to be separately created (and keyed)
// because they animate separately during page transitions.
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/page_scaffold.dart
Expand Up @@ -200,7 +200,7 @@ class _CupertinoPageScaffoldState extends State<CupertinoPageScaffold> {
///
/// Used by [CupertinoPageScaffold] to either shift away fully obstructed content
/// or provide a padding guide to partially obstructed content.
abstract class ObstructingPreferredSizeWidget extends PreferredSizeWidget {
abstract class ObstructingPreferredSizeWidget implements PreferredSizeWidget {
/// If true, this widget fully obstructs widgets behind it by the specified
/// size.
///
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/lib/src/material/reorderable_list.dart
Expand Up @@ -58,6 +58,7 @@ class ReorderableListView extends StatefulWidget {

/// Creates a reorderable list.
ReorderableListView({
Key key,
this.header,
@required this.children,
@required this.onReorder,
Expand All @@ -70,7 +71,8 @@ class ReorderableListView extends StatefulWidget {
assert(
children.every((Widget w) => w.key != null),
'All children of this widget must have a key.',
);
),
super(key: key);

/// A non-reorderable header widget to show before the list.
///
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/lib/src/widgets/platform_view.dart
Expand Up @@ -922,12 +922,14 @@ class PlatformViewSurface extends LeafRenderObjectWidget {
///
/// The [controller] must not be null.
const PlatformViewSurface({
Key key,
@required this.controller,
@required this.hitTestBehavior,
@required this.gestureRecognizers,
}) : assert(controller != null),
assert(hitTestBehavior != null),
assert(gestureRecognizers != null);
assert(gestureRecognizers != null),
super(key: key);

/// The controller for the platform view integrated by this [PlatformViewSurface].
///
Expand Down
5 changes: 3 additions & 2 deletions packages/flutter/lib/src/widgets/text.dart
Expand Up @@ -58,13 +58,14 @@ class DefaultTextStyle extends InheritedTheme {
///
/// This constructor creates a [DefaultTextStyle] that lacks a [child], which
/// means the constructed value cannot be incorporated into the tree.
const DefaultTextStyle.fallback()
const DefaultTextStyle.fallback({ Key key })
: style = const TextStyle(),
textAlign = null,
softWrap = true,
maxLines = null,
overflow = TextOverflow.clip,
textWidthBasis = TextWidthBasis.parent;
textWidthBasis = TextWidthBasis.parent,
super(key: key, child: null);

/// Creates a default text style that overrides the text styles in scope at
/// this point in the widget tree.
Expand Down
Expand Up @@ -115,11 +115,13 @@ class ValueListenableBuilder<T> extends StatefulWidget {
/// The [child] is optional but is good practice to use if part of the widget
/// subtree does not depend on the value of the [valueListenable].
const ValueListenableBuilder({
Key key,
@required this.valueListenable,
@required this.builder,
this.child,
}) : assert(valueListenable != null),
assert(builder != null);
assert(builder != null),
super(key: key);

/// The [ValueListenable] whose value you depend on in order to build.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/material/bottom_app_bar_test.dart
Expand Up @@ -419,7 +419,7 @@ class ClipCachePainter extends CustomPainter {
}

class ShapeListener extends StatefulWidget {
const ShapeListener(this.child);
const ShapeListener(this.child, { Key key }) : super(key: key);

final Widget child;

Expand Down
9 changes: 8 additions & 1 deletion packages/flutter/test/material/dropdown_form_field_test.dart
Expand Up @@ -107,10 +107,17 @@ class _TestAppState extends State<TestApp> {
}

class TestApp extends StatefulWidget {
const TestApp({ this.textDirection, this.child, this.mediaSize });
const TestApp({
Key key,
this.textDirection,
this.child,
this.mediaSize,
}) : super(key: key);

final TextDirection textDirection;
final Widget child;
final Size mediaSize;

@override
_TestAppState createState() => _TestAppState();
}
Expand Down
9 changes: 8 additions & 1 deletion packages/flutter/test/material/dropdown_test.dart
Expand Up @@ -93,10 +93,17 @@ Widget buildFrame({
}

class TestApp extends StatefulWidget {
const TestApp({ this.textDirection, this.child, this.mediaSize });
const TestApp({
Key key,
this.textDirection,
this.child,
this.mediaSize,
}) : super(key: key);

final TextDirection textDirection;
final Widget child;
final Size mediaSize;

@override
_TestAppState createState() => _TestAppState();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/material/expansion_panel_test.dart
Expand Up @@ -55,7 +55,7 @@ class _SimpleExpansionPanelListTestWidgetState extends State<SimpleExpansionPane
}

class ExpansionPanelListSemanticsTest extends StatefulWidget {
const ExpansionPanelListSemanticsTest({this.headerKey});
const ExpansionPanelListSemanticsTest({ Key key, this.headerKey }) : super(key: key);

final Key headerKey;

Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/test/material/feedback_test.dart
Expand Up @@ -201,11 +201,11 @@ void main () {
}

class TestWidget extends StatelessWidget {

const TestWidget({
Key key,
this.tapHandler = nullHandler,
this.longPressHandler = nullHandler,
});
}) : super(key: key);

final HandlerCreator tapHandler;
final HandlerCreator longPressHandler;
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter/test/material/material_test.dart
Expand Up @@ -11,6 +11,7 @@ import '../rendering/mock_canvas.dart';
import '../widgets/test_border.dart' show TestBorder;

class NotifyMaterial extends StatelessWidget {
const NotifyMaterial({ Key key }) : super(key: key);
@override
Widget build(BuildContext context) {
LayoutChangedNotification().dispatch(context);
Expand Down Expand Up @@ -101,7 +102,7 @@ void main() {

testWidgets('LayoutChangedNotification test', (WidgetTester tester) async {
await tester.pumpWidget(
Material(
const Material(
child: NotifyMaterial(),
),
);
Expand Down
8 changes: 7 additions & 1 deletion packages/flutter/test/material/popup_menu_test.dart
Expand Up @@ -1194,9 +1194,15 @@ void main() {
}

class TestApp extends StatefulWidget {
const TestApp({ this.textDirection, this.child });
const TestApp({
Key key,
this.textDirection,
this.child,
}) : super(key: key);

final TextDirection textDirection;
final Widget child;

@override
_TestAppState createState() => _TestAppState();
}
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter/test/material/search_test.dart
Expand Up @@ -644,11 +644,12 @@ void main() {

class TestHomePage extends StatelessWidget {
const TestHomePage({
Key key,
this.results,
this.delegate,
this.passInInitialQuery = false,
this.initialQuery,
});
}) : super(key: key);

final List<String> results;
final SearchDelegate<String> delegate;
Expand Down
Expand Up @@ -24,6 +24,8 @@ class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
}

class MyHomePage extends StatefulWidget {
const MyHomePage({ Key key }) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();
}
Expand Down Expand Up @@ -73,7 +75,7 @@ class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {

void main() {
testWidgets('Tabbed CustomScrollViews, warp from tab 1 to 3', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(home: MyHomePage()));
await tester.pumpWidget(const MaterialApp(home: MyHomePage()));

// should not crash.
await tester.tap(find.text('Tab 2'));
Expand Down
7 changes: 6 additions & 1 deletion packages/flutter/test/material/tabs_test.dart
Expand Up @@ -127,7 +127,12 @@ Widget buildFrame({
typedef TabControllerFrameBuilder = Widget Function(BuildContext context, TabController controller);

class TabControllerFrame extends StatefulWidget {
const TabControllerFrame({ this.length, this.initialIndex = 0, this.builder });
const TabControllerFrame({
Key key,
this.length,
this.initialIndex = 0,
this.builder,
}) : super(key: key);

final int length;
final int initialIndex;
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/material/theme_test.dart
Expand Up @@ -650,7 +650,7 @@ void main() {

int testBuildCalled;
class Test extends StatefulWidget {
const Test();
const Test({ Key key }) : super(key: key);

@override
_TestState createState() => _TestState();
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter/test/material/will_pop_test.dart
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
bool willPopValue = false;

class SamplePage extends StatefulWidget {
const SamplePage({ Key key }) : super(key: key);
@override
SamplePageState createState() => SamplePageState();
}
Expand Down Expand Up @@ -86,7 +87,7 @@ void main() {
onPressed: () {
showDialog<void>(
context: context,
builder: (BuildContext context) => SamplePage(),
builder: (BuildContext context) => const SamplePage(),
);
},
),
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/widgets/baseline_test.dart
Expand Up @@ -94,7 +94,7 @@ void main() {
}

class BaselineDetector extends LeafRenderObjectWidget {
const BaselineDetector(this.callback);
const BaselineDetector(this.callback, { Key key }) : super(key: key);

final VoidCallback callback;

Expand Down
15 changes: 9 additions & 6 deletions packages/flutter/test/widgets/build_scope_test.dart
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter/widgets.dart';
import 'test_widgets.dart';

class ProbeWidget extends StatefulWidget {
const ProbeWidget({ Key key }) : super(key: key);
@override
ProbeWidgetState createState() => ProbeWidgetState();
}
Expand Down Expand Up @@ -36,7 +37,7 @@ class ProbeWidgetState extends State<ProbeWidget> {
}

class BadWidget extends StatelessWidget {
const BadWidget(this.parentState);
const BadWidget(this.parentState, { Key key }) : super(key: key);

final BadWidgetParentState parentState;

Expand All @@ -48,6 +49,7 @@ class BadWidget extends StatelessWidget {
}

class BadWidgetParent extends StatefulWidget {
const BadWidgetParent({ Key key }) : super(key: key);
@override
BadWidgetParentState createState() => BadWidgetParentState();
}
Expand All @@ -67,6 +69,7 @@ class BadWidgetParentState extends State<BadWidgetParent> {
}

class BadDisposeWidget extends StatefulWidget {
const BadDisposeWidget({ Key key }) : super(key: key);
@override
BadDisposeWidgetState createState() => BadDisposeWidgetState();
}
Expand Down Expand Up @@ -133,14 +136,14 @@ void main() {
testWidgets('Legal times for setState', (WidgetTester tester) async {
final GlobalKey flipKey = GlobalKey();
expect(ProbeWidgetState.buildCount, equals(0));
await tester.pumpWidget(ProbeWidget());
await tester.pumpWidget(const ProbeWidget(key: Key('a')));
expect(ProbeWidgetState.buildCount, equals(1));
await tester.pumpWidget(ProbeWidget());
await tester.pumpWidget(const ProbeWidget(key: Key('b')));
expect(ProbeWidgetState.buildCount, equals(2));
await tester.pumpWidget(FlipWidget(
key: flipKey,
left: Container(),
right: ProbeWidget(),
right: const ProbeWidget(key: Key('c')),
));
expect(ProbeWidgetState.buildCount, equals(2));
final FlipWidgetState flipState1 = flipKey.currentState as FlipWidgetState;
Expand All @@ -156,13 +159,13 @@ void main() {
});

testWidgets('Setting parent state during build is forbidden', (WidgetTester tester) async {
await tester.pumpWidget(BadWidgetParent());
await tester.pumpWidget(const BadWidgetParent());
expect(tester.takeException(), isFlutterError);
await tester.pumpWidget(Container());
});

testWidgets('Setting state during dispose is forbidden', (WidgetTester tester) async {
await tester.pumpWidget(BadDisposeWidget());
await tester.pumpWidget(const BadDisposeWidget());
expect(tester.takeException(), isNull);
await tester.pumpWidget(Container());
expect(tester.takeException(), isNotNull);
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter/test/widgets/dismissible_test.dart
Expand Up @@ -208,7 +208,7 @@ Future<void> rollbackElement(WidgetTester tester, Finder finder, { @required Axi
}

class Test1215DismissibleWidget extends StatelessWidget {
const Test1215DismissibleWidget(this.text);
const Test1215DismissibleWidget(this.text, { Key key }) : super(key: key);

final String text;

Expand Down
Expand Up @@ -8,7 +8,7 @@ import 'package:flutter/widgets.dart';
typedef TestCallback = void Function(BuildContext context);

class TestWidget extends StatefulWidget {
const TestWidget(this.callback);
const TestWidget(this.callback, { Key key }) : super(key: key);

final TestCallback callback;

Expand Down

0 comments on commit 1fc43d9

Please sign in to comment.