From cfe0c2a10c8d9bee05c962974b1e976988962425 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Wed, 28 Sep 2022 20:19:51 +0200 Subject: [PATCH] unnecessary_stateful_widgets (#112296) --- .../flutter/lib/src/cupertino/dialog.dart | 21 ++++++---------- .../flutter/lib/src/material/app_bar.dart | 23 +++++++----------- .../gesture_config_regression_test.dart | 24 +++++-------------- .../test/material/popup_menu_test.dart | 11 +++------ .../flutter/test/material/theme_test.dart | 7 +----- .../test/widgets/editable_text_test.dart | 15 ++++-------- .../test/widgets/reorderable_list_test.dart | 21 +++++++--------- .../test/widgets/router_restoration_test.dart | 11 +++------ .../test/widgets/slivers_keepalive_test.dart | 20 ++++------------ .../test/widgets/widget_inspector_test.dart | 14 ++++------- 10 files changed, 52 insertions(+), 115 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/dialog.dart b/packages/flutter/lib/src/cupertino/dialog.dart index 13cb42bb6e..b99d7d96e2 100644 --- a/packages/flutter/lib/src/cupertino/dialog.dart +++ b/packages/flutter/lib/src/cupertino/dialog.dart @@ -1436,7 +1436,7 @@ class _CupertinoAlertContentSection extends StatelessWidget { // // See [_RenderCupertinoDialogActions] for details about action button sizing // and layout. -class _CupertinoAlertActionSection extends StatefulWidget { +class _CupertinoAlertActionSection extends StatelessWidget { const _CupertinoAlertActionSection({ required this.children, this.scrollController, @@ -1461,35 +1461,28 @@ class _CupertinoAlertActionSection extends StatefulWidget { final bool isActionSheet; - @override - _CupertinoAlertActionSectionState createState() => - _CupertinoAlertActionSectionState(); -} - -class _CupertinoAlertActionSectionState - extends State<_CupertinoAlertActionSection> { @override Widget build(BuildContext context) { final double devicePixelRatio = MediaQuery.of(context).devicePixelRatio; final List interactiveButtons = []; - for (int i = 0; i < widget.children.length; i += 1) { + for (int i = 0; i < children.length; i += 1) { interactiveButtons.add( _PressableActionButton( - child: widget.children[i], + child: children[i], ), ); } return CupertinoScrollbar( - controller: widget.scrollController, + controller: scrollController, child: SingleChildScrollView( - controller: widget.scrollController, + controller: scrollController, child: _CupertinoDialogActionsRenderWidget( actionButtons: interactiveButtons, dividerThickness: _kDividerThickness / devicePixelRatio, - hasCancelButton: widget.hasCancelButton, - isActionSheet: widget.isActionSheet, + hasCancelButton: hasCancelButton, + isActionSheet: isActionSheet, ), ), ); diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 7e834e186a..3f422a452c 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -2192,7 +2192,7 @@ class _RenderAppBarTitleBox extends RenderAligningShiftedBox { enum _ScrollUnderFlexibleVariant { medium, large } -class _ScrollUnderFlexibleSpace extends StatefulWidget { +class _ScrollUnderFlexibleSpace extends StatelessWidget { const _ScrollUnderFlexibleSpace({ this.title, required this.variant, @@ -2205,20 +2205,15 @@ class _ScrollUnderFlexibleSpace extends StatefulWidget { final bool? centerCollapsedTitle; final bool primary; - @override - State<_ScrollUnderFlexibleSpace> createState() => _ScrollUnderFlexibleSpaceState(); -} - -class _ScrollUnderFlexibleSpaceState extends State<_ScrollUnderFlexibleSpace> { @override Widget build(BuildContext context) { late final ThemeData theme = Theme.of(context); final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType()!; - final double topPadding = widget.primary ? MediaQuery.of(context).viewPadding.top : 0; + final double topPadding = primary ? MediaQuery.of(context).viewPadding.top : 0; final double collapsedHeight = settings.minExtent - topPadding; final double scrollUnderHeight = settings.maxExtent - settings.minExtent; final _ScrollUnderFlexibleConfig config; - switch (widget.variant) { + switch (variant) { case _ScrollUnderFlexibleVariant.medium: config = _MediumScrollUnderFlexibleConfig(context); break; @@ -2229,19 +2224,19 @@ class _ScrollUnderFlexibleSpaceState extends State<_ScrollUnderFlexibleSpace> { late final Widget? collapsedTitle; late final Widget? expandedTitle; - if (widget.title != null) { + if (title != null) { collapsedTitle = config.collapsedTextStyle != null ? DefaultTextStyle( style: config.collapsedTextStyle!, - child: widget.title!, + child: title!, ) - : widget.title; + : title; expandedTitle = config.expandedTextStyle != null ? DefaultTextStyle( style: config.expandedTextStyle!, - child: widget.title!, + child: title!, ) - : widget.title; + : title; } late final bool centerTitle; @@ -2259,7 +2254,7 @@ class _ScrollUnderFlexibleSpaceState extends State<_ScrollUnderFlexibleSpace> { return true; } } - centerTitle = widget.centerCollapsedTitle + centerTitle = centerCollapsedTitle ?? theme.appBarTheme.centerTitle ?? platformCenter(); } diff --git a/packages/flutter/test/gestures/gesture_config_regression_test.dart b/packages/flutter/test/gestures/gesture_config_regression_test.dart index d51e3efc57..7ac1ad832c 100644 --- a/packages/flutter/test/gestures/gesture_config_regression_test.dart +++ b/packages/flutter/test/gestures/gesture_config_regression_test.dart @@ -12,17 +12,11 @@ class TestResult { bool dragUpdate = false; } -class NestedScrollableCase extends StatefulWidget { +class NestedScrollableCase extends StatelessWidget { const NestedScrollableCase({super.key, required this.testResult}); final TestResult testResult; - @override - State createState() => _NestedScrollableCaseState(); -} - -class _NestedScrollableCaseState extends State { - @override Widget build(BuildContext context) { return Scaffold( @@ -37,10 +31,10 @@ class _NestedScrollableCaseState extends State { child: GestureDetector( behavior: HitTestBehavior.opaque, onVerticalDragDown: (DragDownDetails details) { - widget.testResult.dragStarted = true; + testResult.dragStarted = true; }, onVerticalDragUpdate: (DragUpdateDetails details){ - widget.testResult.dragUpdate = true; + testResult.dragUpdate = true; }, onVerticalDragEnd: (_) {}, child: Text('List Item $index', key: ValueKey(index), @@ -56,17 +50,11 @@ class _NestedScrollableCaseState extends State { } } -class NestedDragableCase extends StatefulWidget { +class NestedDragableCase extends StatelessWidget { const NestedDragableCase({super.key, required this.testResult}); final TestResult testResult; - @override - State createState() => _NestedDragableCaseState(); -} - -class _NestedDragableCaseState extends State { - @override Widget build(BuildContext context) { return Scaffold( @@ -83,10 +71,10 @@ class _NestedDragableCaseState extends State { feedback: const Text('Dragging'), child: Text('List Item $index'), onDragStarted: () { - widget.testResult.dragStarted = true; + testResult.dragStarted = true; }, onDragUpdate: (DragUpdateDetails details){ - widget.testResult.dragUpdate = true; + testResult.dragUpdate = true; }, onDragEnd: (_) {}, ), diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index 9cc07b955e..7b28d88361 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -3014,7 +3014,7 @@ void main() { }); } -class TestApp extends StatefulWidget { +class TestApp extends StatelessWidget { const TestApp({ super.key, required this.textDirection, @@ -3024,11 +3024,6 @@ class TestApp extends StatefulWidget { final TextDirection textDirection; final Widget? child; - @override - State createState() => _TestAppState(); -} - -class _TestAppState extends State { @override Widget build(BuildContext context) { return Localizations( @@ -3040,14 +3035,14 @@ class _TestAppState extends State { child: MediaQuery( data: MediaQueryData.fromWindow(WidgetsBinding.instance.window), child: Directionality( - textDirection: widget.textDirection, + textDirection: textDirection, child: Navigator( onGenerateRoute: (RouteSettings settings) { assert(settings.name == '/'); return MaterialPageRoute( settings: settings, builder: (BuildContext context) => Material( - child: widget.child, + child: child, ), ); }, diff --git a/packages/flutter/test/material/theme_test.dart b/packages/flutter/test/material/theme_test.dart index ed129c83f2..7f917f6fdd 100644 --- a/packages/flutter/test/material/theme_test.dart +++ b/packages/flutter/test/material/theme_test.dart @@ -728,14 +728,9 @@ void main() { } int testBuildCalled = 0; -class Test extends StatefulWidget { +class Test extends StatelessWidget { const Test({ super.key }); - @override - State createState() => _TestState(); -} - -class _TestState extends State { @override Widget build(BuildContext context) { testBuildCalled += 1; diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 2054c0ba93..ff0f2375f5 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -13131,7 +13131,7 @@ class _CustomTextSelectionControls extends TextSelectionControls { } // A fake text selection toolbar with only a paste button. -class _CustomTextSelectionToolbar extends StatefulWidget { +class _CustomTextSelectionToolbar extends StatelessWidget { const _CustomTextSelectionToolbar({ required this.anchorAbove, required this.anchorBelow, @@ -13144,16 +13144,11 @@ class _CustomTextSelectionToolbar extends StatefulWidget { final VoidCallback? handlePaste; final VoidCallback? handleCut; - @override - _CustomTextSelectionToolbarState createState() => _CustomTextSelectionToolbarState(); -} - -class _CustomTextSelectionToolbarState extends State<_CustomTextSelectionToolbar> { @override Widget build(BuildContext context) { return TextSelectionToolbar( - anchorAbove: widget.anchorAbove, - anchorBelow: widget.anchorBelow, + anchorAbove: anchorAbove, + anchorBelow: anchorBelow, toolbarBuilder: (BuildContext context, Widget child) { return Container( color: Colors.pink, @@ -13163,12 +13158,12 @@ class _CustomTextSelectionToolbarState extends State<_CustomTextSelectionToolbar children: [ TextSelectionToolbarTextButton( padding: TextSelectionToolbarTextButton.getPadding(0, 2), - onPressed: widget.handleCut, + onPressed: handleCut, child: const Text('Cut'), ), TextSelectionToolbarTextButton( padding: TextSelectionToolbarTextButton.getPadding(1, 2), - onPressed: widget.handlePaste, + onPressed: handlePaste, child: const Text('Paste'), ), ], diff --git a/packages/flutter/test/widgets/reorderable_list_test.dart b/packages/flutter/test/widgets/reorderable_list_test.dart index afce2894c5..1b5841621a 100644 --- a/packages/flutter/test/widgets/reorderable_list_test.dart +++ b/packages/flutter/test/widgets/reorderable_list_test.dart @@ -1125,7 +1125,7 @@ void main() { }); } -class TestList extends StatefulWidget { +class TestList extends StatelessWidget { const TestList({ super.key, this.textColor, @@ -1144,24 +1144,19 @@ class TestList extends StatefulWidget { final bool reverse; final void Function(int)? onReorderStart, onReorderEnd; - @override - State createState() => _TestListState(); -} - -class _TestListState extends State { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: DefaultTextStyle( - style: TextStyle(color: widget.textColor), + style: TextStyle(color: textColor), child: IconTheme( - data: IconThemeData(color: widget.iconColor), + data: IconThemeData(color: iconColor), child: StatefulBuilder( builder: (BuildContext outerContext, StateSetter setState) { - final List items = widget.items; + final List items = this.items; return CustomScrollView( - reverse: widget.reverse, + reverse: reverse, slivers: [ SliverReorderableList( itemBuilder: (BuildContext context, int index) { @@ -1190,9 +1185,9 @@ class _TestListState extends State { items.insert(toIndex, items.removeAt(fromIndex)); }); }, - proxyDecorator: widget.proxyDecorator, - onReorderStart: widget.onReorderStart, - onReorderEnd: widget.onReorderEnd, + proxyDecorator: proxyDecorator, + onReorderStart: onReorderStart, + onReorderEnd: onReorderEnd, ), ], ); diff --git a/packages/flutter/test/widgets/router_restoration_test.dart b/packages/flutter/test/widgets/router_restoration_test.dart index 19bbe04643..80a1d98f26 100644 --- a/packages/flutter/test/widgets/router_restoration_test.dart +++ b/packages/flutter/test/widgets/router_restoration_test.dart @@ -140,27 +140,22 @@ class _TestRouteInformationProvider extends RouteInformationProvider with Change } } -class _TestWidget extends StatefulWidget { +class _TestWidget extends StatelessWidget { const _TestWidget({this.withInformationProvider = false, this.routerKey}); final bool withInformationProvider; final Key? routerKey; - @override - State<_TestWidget> createState() => _TestWidgetState(); -} - -class _TestWidgetState extends State<_TestWidget> { @override Widget build(BuildContext context) { return RootRestorationScope( restorationId: 'root', child: Router( - key: widget.routerKey, + key: routerKey, restorationScopeId: 'router', routerDelegate: _TestRouterDelegate(), routeInformationParser: _TestRouteInformationParser(), - routeInformationProvider: widget.withInformationProvider ? _TestRouteInformationProvider() : null, + routeInformationProvider: withInformationProvider ? _TestRouteInformationProvider() : null, ), ); } diff --git a/packages/flutter/test/widgets/slivers_keepalive_test.dart b/packages/flutter/test/widgets/slivers_keepalive_test.dart index a602c00f5e..5065232cbe 100644 --- a/packages/flutter/test/widgets/slivers_keepalive_test.dart +++ b/packages/flutter/test/widgets/slivers_keepalive_test.dart @@ -470,7 +470,7 @@ class _SwitchingChildBuilderTest extends State { } } -class SwitchingChildListTest extends StatefulWidget { +class SwitchingChildListTest extends StatelessWidget { const SwitchingChildListTest({ required this.children, this.viewportFraction = 1.0, @@ -480,11 +480,6 @@ class SwitchingChildListTest extends StatefulWidget { final List children; final double viewportFraction; - @override - State createState() => _SwitchingChildListTest(); -} - -class _SwitchingChildListTest extends State { @override Widget build(BuildContext context) { return Directionality( @@ -496,8 +491,8 @@ class _SwitchingChildListTest extends State { cacheExtent: 0, slivers: [ SliverFillViewport( - viewportFraction: widget.viewportFraction, - delegate: SliverChildListDelegate(widget.children), + viewportFraction: viewportFraction, + delegate: SliverChildListDelegate(children), ), ], ), @@ -507,7 +502,7 @@ class _SwitchingChildListTest extends State { } } -class SwitchingSliverListTest extends StatefulWidget { +class SwitchingSliverListTest extends StatelessWidget { const SwitchingSliverListTest({ required this.children, this.viewportFraction = 1.0, @@ -517,11 +512,6 @@ class SwitchingSliverListTest extends StatefulWidget { final List children; final double viewportFraction; - @override - State createState() => _SwitchingSliverListTest(); -} - -class _SwitchingSliverListTest extends State { @override Widget build(BuildContext context) { return Directionality( @@ -533,7 +523,7 @@ class _SwitchingSliverListTest extends State { cacheExtent: 0, slivers: [ SliverList( - delegate: SliverChildListDelegate(widget.children), + delegate: SliverChildListDelegate(children), ), ], ), diff --git a/packages/flutter/test/widgets/widget_inspector_test.dart b/packages/flutter/test/widgets/widget_inspector_test.dart index 87103b0062..0745ebde51 100644 --- a/packages/flutter/test/widgets/widget_inspector_test.dart +++ b/packages/flutter/test/widgets/widget_inspector_test.dart @@ -29,13 +29,9 @@ import 'widget_inspector_test_utils.dart'; // Start of block of code where widget creation location line numbers and // columns will impact whether tests pass. -class ClockDemo extends StatefulWidget { +class ClockDemo extends StatelessWidget { const ClockDemo({ super.key }); - @override - State createState() => _ClockDemoState(); -} -class _ClockDemoState extends State { @override Widget build(BuildContext context) { return Directionality( @@ -3455,7 +3451,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { _CreationLocation location = knownLocations[id]!; expect(location.file, equals(file)); // ClockText widget. - expect(location.line, equals(63)); + expect(location.line, equals(59)); expect(location.column, equals(9)); expect(location.name, equals('ClockText')); expect(count, equals(1)); @@ -3465,7 +3461,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { location = knownLocations[id]!; expect(location.file, equals(file)); // Text widget in _ClockTextState build method. - expect(location.line, equals(101)); + expect(location.line, equals(97)); expect(location.column, equals(12)); expect(location.name, equals('Text')); expect(count, equals(1)); @@ -3492,7 +3488,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { location = knownLocations[id]!; expect(location.file, equals(file)); // ClockText widget. - expect(location.line, equals(63)); + expect(location.line, equals(59)); expect(location.column, equals(9)); expect(location.name, equals('ClockText')); expect(count, equals(3)); // 3 clock widget instances rebuilt. @@ -3502,7 +3498,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { location = knownLocations[id]!; expect(location.file, equals(file)); // Text widget in _ClockTextState build method. - expect(location.line, equals(101)); + expect(location.line, equals(97)); expect(location.column, equals(12)); expect(location.name, equals('Text')); expect(count, equals(3)); // 3 clock widget instances rebuilt.