Unverified Commit a2aa2ed0 authored by Dan Field's avatar Dan Field Committed by GitHub

Revert "Fix excessive rebuilds of DSS (#69724)" (#71459)

This reverts commit 16dce765.
parent f0f9a819
......@@ -288,17 +288,17 @@ class _DraggableSheetExtent {
class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
late _DraggableScrollableSheetScrollController _scrollController;
late _DraggableSheetExtent _extent;
// The child only gets rebuilt when dependencies or the widget change.
// Otherwise, excessive rebuilds of the child are triggered every time the
// scroll extent changes, which is very expensive and does not provide any
// helpful information to the child. If the child needs to rebuild whenever
// the scroll position changes, they can always subscribe to it.
Widget? _child;
@override
void initState() {
super.initState();
_updateExtent();
_extent = _DraggableSheetExtent(
minExtent: widget.minChildSize,
maxExtent: widget.maxChildSize,
initialExtent: widget.initialChildSize,
listener: _setExtent,
);
_scrollController = _DraggableScrollableSheetScrollController(extent: _extent);
}
@override
......@@ -317,33 +317,13 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
}
_extent._currentExtent.value = _extent.initialExtent;
}
_child = widget.builder(context, _scrollController);
}
@override
void didUpdateWidget(DraggableScrollableSheet oldWidget) {
super.didUpdateWidget(oldWidget);
_updateExtent();
// Call this unconditionally - the closure may not change even though it
// refers to things outside of its identity, e.g. a tearoff from state that
// has an `if (stateVariable)`.
_child = widget.builder(context, _scrollController);
}
void _updateExtent() {
_extent = _DraggableSheetExtent(
minExtent: widget.minChildSize,
maxExtent: widget.maxChildSize,
initialExtent: widget.initialChildSize,
listener: _setExtent,
);
_scrollController = _DraggableScrollableSheetScrollController(extent: _extent);
}
void _setExtent() {
setState(() {
// _extent has been updated when this is called.
});
}
@override
......@@ -353,7 +333,7 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
_extent.availablePixels = widget.maxChildSize * constraints.biggest.height;
final Widget sheet = FractionallySizedBox(
heightFactor: _extent.currentExtent,
child: _child,
child: widget.builder(context, _scrollController),
alignment: Alignment.bottomCenter,
);
return widget.expand ? SizedBox.expand(child: sheet) : sheet;
......@@ -364,7 +344,6 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
@override
void dispose() {
_scrollController.dispose();
_child = null;
super.dispose();
}
}
......
......@@ -309,149 +309,4 @@ void main() {
expect(notificationTypes, types);
});
}
testWidgets('Builder is not called excessively', (WidgetTester tester) async {
int buildCount = 0;
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: Stack(
children: <Widget>[
DraggableScrollableSheet(
builder: (BuildContext context, ScrollController scrollController) {
buildCount += 1;
return Container(
color: const Color(0xFFABCDEF),
child: ListView.builder(
controller: scrollController,
itemExtent: 100,
itemCount: 100,
itemBuilder: (BuildContext context, int index) => Text('Item $index'),
),
);
},
),
],
),
));
expect(buildCount, 1);
await tester.flingFrom(const Offset(0, 325), const Offset(0, -325), 200);
expect(buildCount, 1);
await tester.pumpAndSettle();
expect(buildCount, 1);
});
testWidgets('Builder is called if widget updates', (WidgetTester tester) async {
int buildCount = 0;
final GlobalKey key = GlobalKey();
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: Stack(
children: <Widget>[
DraggableScrollableSheet(
key: key,
builder: (BuildContext context, ScrollController scrollController) {
buildCount += 1;
return Container(
color: const Color(0xFFABCDEF),
child: ListView.builder(
controller: scrollController,
itemExtent: 100,
itemCount: 100,
itemBuilder: (BuildContext context, int index) => Text('Item $index'),
),
);
},
),
],
),
));
expect(buildCount, 1);
expect(find.text('Item 1'), findsOneWidget);
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: Stack(
children: <Widget>[
DraggableScrollableSheet(
key: key,
builder: (BuildContext context, ScrollController scrollController) {
buildCount += 1;
return Container(
color: const Color(0xFFFEDCBA),
child: ListView.builder(
controller: scrollController,
itemExtent: 50,
itemCount: 100,
itemBuilder: (BuildContext context, int index) => Text('New Item $index'),
),
);
},
),
],
),
));
expect(buildCount, 2);
expect(find.text('Item 1'), findsNothing);
expect(find.text('New Item 1'), findsOneWidget);
});
testWidgets('Changes to min/max/initial child size are respected', (WidgetTester tester) async {
final GlobalKey key = GlobalKey();
final Key childKey = UniqueKey();
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: Stack(
children: <Widget>[
DraggableScrollableSheet(
key: key,
minChildSize: .25,
maxChildSize: 1.0,
initialChildSize: .5,
builder: (BuildContext context, ScrollController scrollController) {
return Container(
key: childKey,
color: const Color(0xFFABCDEF),
child: ListView.builder(
controller: scrollController,
itemExtent: 100,
itemCount: 100,
itemBuilder: (BuildContext context, int index) => Text('Item $index'),
),
);
},
),
],
),
));
expect(find.text('Item 1'), findsOneWidget);
expect(tester.getRect(find.byKey(childKey)), const Rect.fromLTRB(0, 300, 800, 600));
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: Stack(
children: <Widget>[
DraggableScrollableSheet(
key: key,
minChildSize: .5,
maxChildSize: .75,
initialChildSize: .6,
builder: (BuildContext context, ScrollController scrollController) {
return Container(
key: childKey,
color: const Color(0xFFFEDCBA),
child: ListView.builder(
controller: scrollController,
itemExtent: 50,
itemCount: 100,
itemBuilder: (BuildContext context, int index) => Text('New Item $index'),
),
);
},
),
],
),
));
expect(find.text('New Item 1'), findsOneWidget);
expect(tester.getRect(find.byKey(childKey)), const Rect.fromLTRB(0, 240, 800, 600));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment