-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sliver Constrained Cross Axis (#125239)
Reimplements what we reverted here: #125233.
- Loading branch information
Showing
6 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
examples/api/lib/widgets/sliver/sliver_constrained_cross_axis.0.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
void main() => runApp(const SliverConstrainedCrossAxisExampleApp()); | ||
|
||
class SliverConstrainedCrossAxisExampleApp extends StatelessWidget { | ||
const SliverConstrainedCrossAxisExampleApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
appBar: AppBar(title: const Text('SliverConstrainedCrossAxis Sample')), | ||
body: const SliverConstrainedCrossAxisExample(), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class SliverConstrainedCrossAxisExample extends StatelessWidget { | ||
const SliverConstrainedCrossAxisExample({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return CustomScrollView( | ||
slivers: <Widget>[ | ||
SliverConstrainedCrossAxis( | ||
maxExtent: 200, | ||
sliver: SliverList.builder( | ||
itemBuilder: (BuildContext context, int index) { | ||
return Container( | ||
color: index.isEven ? Colors.amber[300] : Colors.blue[300], | ||
height: 100.0, | ||
child: Center( | ||
child: Text( | ||
'Item $index', | ||
style: const TextStyle(fontSize: 24), | ||
), | ||
), | ||
); | ||
}, | ||
itemCount: 10, | ||
), | ||
), | ||
], | ||
); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/api/test/widgets/sliver/sliver_constrained_cross_axis.0_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:flutter_api_samples/widgets/sliver/sliver_constrained_cross_axis.0.dart' | ||
as example; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets('SliverConstrainedCrossAxis example', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.SliverConstrainedCrossAxisExampleApp(), | ||
); | ||
|
||
final RenderSliverList renderSliverList = tester.renderObject(find.byType(SliverList)); | ||
expect(renderSliverList.constraints.crossAxisExtent, equals(200)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/flutter/test/widgets/sliver_constrained_cross_axis_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2014 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
const double VIEWPORT_HEIGHT = 500; | ||
const double VIEWPORT_WIDTH = 300; | ||
|
||
void main() { | ||
testWidgets('SliverConstrainedCrossAxis basic test', (WidgetTester tester) async { | ||
await tester.pumpWidget(_buildSliverConstrainedCrossAxis(maxExtent: 50)); | ||
|
||
final RenderBox box = tester.renderObject(find.byType(Container)); | ||
expect(box.size.height, 100); | ||
expect(box.size.width, 50); | ||
|
||
final RenderSliver sliver = tester.renderObject(find.byType(SliverToBoxAdapter)); | ||
expect(sliver.geometry!.paintExtent, equals(100)); | ||
}); | ||
|
||
testWidgets('SliverConstrainedCrossAxis updates correctly', (WidgetTester tester) async { | ||
await tester.pumpWidget(_buildSliverConstrainedCrossAxis(maxExtent: 50)); | ||
|
||
final RenderBox box1 = tester.renderObject(find.byType(Container)); | ||
expect(box1.size.height, 100); | ||
expect(box1.size.width, 50); | ||
|
||
await tester.pumpWidget(_buildSliverConstrainedCrossAxis(maxExtent: 80)); | ||
|
||
final RenderBox box2 = tester.renderObject(find.byType(Container)); | ||
expect(box2.size.height, 100); | ||
expect(box2.size.width, 80); | ||
}); | ||
|
||
testWidgets('SliverConstrainedCrossAxis uses parent extent if maxExtent is greater', (WidgetTester tester) async { | ||
await tester.pumpWidget(_buildSliverConstrainedCrossAxis(maxExtent: 400)); | ||
|
||
final RenderBox box = tester.renderObject(find.byType(Container)); | ||
expect(box.size.height, 100); | ||
expect(box.size.width, VIEWPORT_WIDTH); | ||
}); | ||
|
||
testWidgets('SliverConstrainedCrossAxis constrains the height when direction is horizontal', (WidgetTester tester) async { | ||
await tester.pumpWidget(_buildSliverConstrainedCrossAxis( | ||
maxExtent: 50, | ||
scrollDirection: Axis.horizontal, | ||
)); | ||
|
||
final RenderBox box = tester.renderObject(find.byType(Container)); | ||
expect(box.size.height, 50); | ||
}); | ||
|
||
testWidgets('SliverConstrainedCrossAxis sets its own flex to 0', (WidgetTester tester) async { | ||
await tester.pumpWidget(_buildSliverConstrainedCrossAxis( | ||
maxExtent: 50, | ||
)); | ||
|
||
final RenderSliver sliver = tester.renderObject(find.byType(SliverConstrainedCrossAxis)); | ||
expect((sliver.parentData! as SliverPhysicalParentData).crossAxisFlex, equals(0)); | ||
}); | ||
} | ||
|
||
Widget _buildSliverConstrainedCrossAxis({ | ||
required double maxExtent, | ||
Axis scrollDirection = Axis.vertical, | ||
}) { | ||
return Directionality( | ||
textDirection: TextDirection.ltr, | ||
child: Center( | ||
child: SizedBox( | ||
width: VIEWPORT_WIDTH, | ||
height: VIEWPORT_HEIGHT, | ||
child: CustomScrollView( | ||
scrollDirection: scrollDirection, | ||
slivers: <Widget>[ | ||
SliverConstrainedCrossAxis( | ||
maxExtent: maxExtent, | ||
sliver: SliverToBoxAdapter( | ||
child: scrollDirection == Axis.vertical | ||
? Container(height: 100) | ||
: Container(width: 100), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} |