Skip to content

Commit

Permalink
more const immutable classes (#104988)
Browse files Browse the repository at this point in the history
* more const immutable classes

* more const constructors in dev/ and examples/
  • Loading branch information
a14n authored May 31, 2022
1 parent 16b7348 commit 552c50e
Show file tree
Hide file tree
Showing 22 changed files with 94 additions and 41 deletions.
8 changes: 6 additions & 2 deletions dev/benchmarks/test_apps/stocks/lib/stock_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum _StockMenuItem { autorefresh, refresh, speedUp, speedDown }
enum StockHomeTab { market, portfolio }

class _NotImplementedDialog extends StatelessWidget {
const _NotImplementedDialog();

@override
Widget build(BuildContext context) {
return AlertDialog(
Expand Down Expand Up @@ -97,7 +99,7 @@ class StockHomeState extends State<StockHome> {
case _StockMenuItem.refresh:
showDialog<void>(
context: context,
builder: (BuildContext context) => _NotImplementedDialog(),
builder: (BuildContext context) => const _NotImplementedDialog(),
);
break;
case _StockMenuItem.speedUp:
Expand Down Expand Up @@ -303,7 +305,7 @@ class StockHomeState extends State<StockHome> {
void _handleCreateCompany() {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) => _CreateCompanySheet(),
builder: (BuildContext context) => const _CreateCompanySheet(),
);
}

Expand Down Expand Up @@ -339,6 +341,8 @@ class StockHomeState extends State<StockHome> {
}

class _CreateCompanySheet extends StatelessWidget {
const _CreateCompanySheet();

@override
Widget build(BuildContext context) {
return Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class _CustomValueIndicatorShape extends SliderComponentShape {
class _SliderDemoState extends State<SliderDemo> {
@override
Widget build(BuildContext context) {
final List<ComponentDemoTabData> demos = <ComponentDemoTabData>[
const List<ComponentDemoTabData> demos = <ComponentDemoTabData>[
ComponentDemoTabData(
tabName: 'SINGLE',
description: 'Sliders containing 1 thumb',
Expand All @@ -229,7 +229,7 @@ class _SliderDemoState extends State<SliderDemo> {
),
];

return TabbedComponentDemoScaffold(
return const TabbedComponentDemoScaffold(
title: 'Sliders',
demos: demos,
isScrollable: false,
Expand All @@ -239,6 +239,8 @@ class _SliderDemoState extends State<SliderDemo> {
}

class _Sliders extends StatefulWidget {
const _Sliders();

@override
_SlidersState createState() => _SlidersState();
}
Expand Down Expand Up @@ -357,6 +359,8 @@ class _SlidersState extends State<_Sliders> {
}

class _RangeSliders extends StatefulWidget {
const _RangeSliders();

@override
_RangeSlidersState createState() => _RangeSlidersState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class MyApp extends StatelessWidget {
}

class ListTileCursor extends MaterialStateMouseCursor {
const ListTileCursor();

@override
MouseCursor resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
Expand All @@ -45,8 +47,8 @@ class MyStatelessWidget extends StatelessWidget {

@override
Widget build(BuildContext context) {
return ListTile(
title: const Text('Disabled ListTile'),
return const ListTile(
title: Text('Disabled ListTile'),
enabled: false,
mouseCursor: ListTileCursor(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class MyApp extends StatelessWidget {

class SelectedBorder extends RoundedRectangleBorder
implements MaterialStateOutlinedBorder {
const SelectedBorder();

@override
OutlinedBorder? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
Expand Down Expand Up @@ -54,7 +56,7 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget> {
isSelected = value;
});
},
shape: SelectedBorder(),
shape: const SelectedBorder(),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ class IVBuilderExampleApp extends StatelessWidget {
appBar: AppBar(
title: const Text('IV Builder Example'),
),
body: _IVBuilderExample(),
body: const _IVBuilderExample(),
),
);
}
}

class _IVBuilderExample extends StatefulWidget {
const _IVBuilderExample();

@override
State<_IVBuilderExample> createState() => _IVBuilderExampleState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ enum _CupertinoTextSelectionToolbarItemsSlot {
}

class _NullElement extends Element {
_NullElement() : super(_NullWidget());
_NullElement() : super(const _NullWidget());

static _NullElement instance = _NullElement();

Expand All @@ -1021,6 +1021,8 @@ class _NullElement extends Element {
}

class _NullWidget extends Widget {
const _NullWidget();

@override
Element createElement() => throw UnimplementedError();
}
4 changes: 3 additions & 1 deletion packages/flutter/lib/src/material/date_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ class _CalendarDateRangePickerState extends State<_CalendarDateRangePicker> {

return Column(
children: <Widget>[
_DayHeaders(),
const _DayHeaders(),
if (_showWeekBottomDivider) const Divider(height: 0),
Expanded(
child: _CalendarKeyboardNavigator(
Expand Down Expand Up @@ -1947,6 +1947,8 @@ class _FocusedDate extends InheritedWidget {


class _DayHeaders extends StatelessWidget {
const _DayHeaders();

/// Builds widgets showing abbreviated days of week. The first widget in the
/// returned list corresponds to the first day of week for the current locale.
///
Expand Down
5 changes: 4 additions & 1 deletion packages/flutter/lib/src/widgets/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,10 @@ class ButtonActivateIntent extends Intent {
abstract class ActivateAction extends Action<ActivateIntent> { }

/// An [Intent] that selects the currently focused control.
class SelectIntent extends Intent { }
class SelectIntent extends Intent {
/// Creates an intent that selects the currently focused control.
const SelectIntent();
}

/// An action that selects the currently focused control.
///
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/lib/src/widgets/widget_inspector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ mixin WidgetInspectorService {
///
/// {@macro flutter.widgets.WidgetInspectorService.getChildrenSummaryTree}
bool isWidgetCreationTracked() {
_widgetCreationTracked ??= _WidgetForTypeTests() is _HasCreationLocation;
_widgetCreationTracked ??= const _WidgetForTypeTests() is _HasCreationLocation;
return _widgetCreationTracked!;
}

Expand Down Expand Up @@ -2190,6 +2190,8 @@ class _ElementLocationStatsTracker {
}

class _WidgetForTypeTests extends Widget {
const _WidgetForTypeTests();

@override
Element createElement() => throw UnimplementedError();
}
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/test/animation/animations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:flutter_test/flutter_test.dart';
import '../scheduler/scheduler_tester.dart';

class BogusCurve extends Curve {
const BogusCurve();

@override
double transform(double t) => 100.0;
}
Expand Down Expand Up @@ -234,7 +236,7 @@ void main() {
final AnimationController controller = AnimationController(
vsync: const TestVSync(),
);
final CurvedAnimation curved = CurvedAnimation(parent: controller, curve: BogusCurve());
final CurvedAnimation curved = CurvedAnimation(parent: controller, curve: const BogusCurve());
FlutterError? error;
try {
curved.value;
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/test/cupertino/colors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ void main() {
}

class _NullElement extends Element {
_NullElement() : super(_NullWidget());
_NullElement() : super(const _NullWidget());

static _NullElement instance = _NullElement();

Expand All @@ -597,6 +597,8 @@ class _NullElement extends Element {
}

class _NullWidget extends Widget {
const _NullWidget();

@override
Element createElement() => throw UnimplementedError();
}
4 changes: 3 additions & 1 deletion packages/flutter/test/cupertino/dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ void main() {

testWidgets('CupertinoDialogRoute is state restorable', (WidgetTester tester) async {
await tester.pumpWidget(
CupertinoApp(
const CupertinoApp(
restorationScopeId: 'app',
home: _RestorableDialogTestWidget(),
),
Expand Down Expand Up @@ -1546,6 +1546,8 @@ Widget createAppWithCenteredButton(Widget child) {


class _RestorableDialogTestWidget extends StatelessWidget {
const _RestorableDialogTestWidget();

static Route<Object?> _dialogBuilder(BuildContext context, Object? arguments) {
return CupertinoDialogRoute<void>(
context: context,
Expand Down
8 changes: 6 additions & 2 deletions packages/flutter/test/cupertino/route_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ void main() {
});

testWidgets('Popping routes should cancel down events', (WidgetTester tester) async {
await tester.pumpWidget(_TestPostRouteCancel());
await tester.pumpWidget(const _TestPostRouteCancel());

final TestGesture gesture = await tester.createGesture();
await gesture.down(tester.getCenter(find.text('PointerCancelEvents: 0')));
Expand Down Expand Up @@ -1861,7 +1861,7 @@ void main() {

testWidgets('CupertinoModalPopupRoute is state restorable', (WidgetTester tester) async {
await tester.pumpWidget(
CupertinoApp(
const CupertinoApp(
restorationScopeId: 'app',
home: _RestorableModalTestWidget(),
),
Expand Down Expand Up @@ -2249,6 +2249,8 @@ Widget buildNavigator({
// Holding the 'Hold' button at the moment of popping will force the navigator to
// cancel the down event, increasing the Home counter by 1.
class _TestPostRouteCancel extends StatefulWidget {
const _TestPostRouteCancel();

@override
State<StatefulWidget> createState() => _TestPostRouteCancelState();
}
Expand Down Expand Up @@ -2308,6 +2310,8 @@ class _TestPostRouteCancelState extends State<_TestPostRouteCancel> {
}

class _RestorableModalTestWidget extends StatelessWidget {
const _RestorableModalTestWidget();

static Route<void> _modalBuilder(BuildContext context, Object? arguments) {
return CupertinoModalPopupRoute<void>(
builder: (BuildContext context) {
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter/test/material/dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,7 @@ void main() {

testWidgets('DialogRoute is state restorable', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
const MaterialApp(
restorationScopeId: 'app',
home: _RestorableDialogTestWidget(),
),
Expand Down Expand Up @@ -2294,6 +2294,8 @@ void main() {
}

class _RestorableDialogTestWidget extends StatelessWidget {
const _RestorableDialogTestWidget();

static Route<Object?> _materialDialogBuilder(BuildContext context, Object? arguments) {
return DialogRoute<void>(
context: context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void main() {
// We create the geometry listener here, but it can only be set up
// after it is pumped into the widget tree and a tester is
// available.
geometryListener = _GeometryListener();
geometryListener = const _GeometryListener();
geometry = null;
listenerState = null;
previousRect = null;
Expand Down Expand Up @@ -1516,6 +1516,8 @@ void main() {
}

class _GeometryListener extends StatefulWidget {
const _GeometryListener();

@override
State createState() => _GeometryListenerState();
}
Expand Down
18 changes: 10 additions & 8 deletions packages/flutter/test/material/scaffold_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,7 @@ void main() {
bottomNavigationBar: ConstrainedBox(
key: key,
constraints: const BoxConstraints.expand(height: 80.0),
child: _GeometryListener(),
child: const _GeometryListener(),
),
)));

Expand All @@ -1255,7 +1255,7 @@ void main() {
await tester.pumpWidget(MaterialApp(home: Scaffold(
body: ConstrainedBox(
constraints: const BoxConstraints.expand(height: 80.0),
child: _GeometryListener(),
child: const _GeometryListener(),
),
)));

Expand Down Expand Up @@ -1337,7 +1337,7 @@ void main() {
body: Container(),
floatingActionButton: FloatingActionButton(
key: key,
child: _GeometryListener(),
child: const _GeometryListener(),
onPressed: () { },
),
)));
Expand All @@ -1358,7 +1358,7 @@ void main() {
await tester.pumpWidget(MaterialApp(home: Scaffold(
body: ConstrainedBox(
constraints: const BoxConstraints.expand(height: 80.0),
child: _GeometryListener(),
child: const _GeometryListener(),
),
)));

Expand All @@ -1376,15 +1376,15 @@ void main() {
await tester.pumpWidget(MaterialApp(home: Scaffold(
body: ConstrainedBox(
constraints: const BoxConstraints.expand(height: 80.0),
child: _GeometryListener(),
child: const _GeometryListener(),
),
)));

await tester.pumpWidget(MaterialApp(home: Scaffold(
body: Container(),
floatingActionButton: FloatingActionButton(
key: key,
child: _GeometryListener(),
child: const _GeometryListener(),
onPressed: () { },
),
)));
Expand Down Expand Up @@ -1439,7 +1439,7 @@ void main() {
await tester.pumpWidget(MaterialApp(home: Scaffold(
body: ConstrainedBox(
constraints: const BoxConstraints.expand(height: 80.0),
child: _GeometryListener(),
child: const _GeometryListener(),
),
)));

Expand All @@ -1452,7 +1452,7 @@ void main() {
body: Container(),
floatingActionButton: FloatingActionButton(
key: key,
child: _GeometryListener(),
child: const _GeometryListener(),
onPressed: () { },
),
)));
Expand Down Expand Up @@ -2424,6 +2424,8 @@ void main() {
}

class _GeometryListener extends StatefulWidget {
const _GeometryListener();

@override
_GeometryListenerState createState() => _GeometryListenerState();
}
Expand Down
Loading

0 comments on commit 552c50e

Please sign in to comment.