Commit fe0b9093 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Remove more clients of LazyBlock (#8071)

The last remaining client is complex_layout.
parent 073843f2
This diff is collapsed.
...@@ -8,10 +8,12 @@ import 'package:flutter/rendering.dart'; ...@@ -8,10 +8,12 @@ import 'package:flutter/rendering.dart';
class CardModel { class CardModel {
CardModel(this.value, this.height, this.color); CardModel(this.value, this.height, this.color);
int value; int value;
double height; double height;
Color color; Color color;
String get label => "Card $value";
String get label => 'Card $value';
Key get key => new ObjectKey(this); Key get key => new ObjectKey(this);
GlobalKey get targetKey => new GlobalObjectKey(this); GlobalKey get targetKey => new GlobalObjectKey(this);
} }
...@@ -21,7 +23,7 @@ enum MarkerType { topLeft, bottomRight, touch } ...@@ -21,7 +23,7 @@ enum MarkerType { topLeft, bottomRight, touch }
class _MarkerPainter extends CustomPainter { class _MarkerPainter extends CustomPainter {
const _MarkerPainter({ const _MarkerPainter({
this.size, this.size,
this.type this.type,
}); });
final double size; final double size;
...@@ -56,10 +58,10 @@ class _MarkerPainter extends CustomPainter { ...@@ -56,10 +58,10 @@ class _MarkerPainter extends CustomPainter {
class Marker extends StatelessWidget { class Marker extends StatelessWidget {
Marker({ Marker({
Key key,
this.type: MarkerType.touch, this.type: MarkerType.touch,
this.position, this.position,
this.size: 40.0, this.size: 40.0,
Key key
}) : super(key: key); }) : super(key: key);
final Point position; final Point position;
...@@ -77,10 +79,10 @@ class Marker extends StatelessWidget { ...@@ -77,10 +79,10 @@ class Marker extends StatelessWidget {
child: new CustomPaint( child: new CustomPaint(
painter: new _MarkerPainter( painter: new _MarkerPainter(
size: size, size: size,
type: type type: type,
) ),
) ),
) ),
); );
} }
} }
...@@ -92,7 +94,7 @@ class OverlayGeometryApp extends StatefulWidget { ...@@ -92,7 +94,7 @@ class OverlayGeometryApp extends StatefulWidget {
typedef void CardTapCallback(GlobalKey targetKey, Point globalPosition); typedef void CardTapCallback(GlobalKey targetKey, Point globalPosition);
class CardBuilder extends LazyBlockDelegate { class CardBuilder extends SliverChildDelegate {
CardBuilder({ this.cardModels, this.onTapUp }); CardBuilder({ this.cardModels, this.onTapUp });
final List<CardModel> cardModels; final List<CardModel> cardModels;
...@@ -102,7 +104,7 @@ class CardBuilder extends LazyBlockDelegate { ...@@ -102,7 +104,7 @@ class CardBuilder extends LazyBlockDelegate {
const TextStyle(color: Colors.white, fontSize: 18.0, fontWeight: FontWeight.bold); const TextStyle(color: Colors.white, fontSize: 18.0, fontWeight: FontWeight.bold);
@override @override
Widget buildItem(BuildContext context, int index) { Widget build(BuildContext context, int index) {
if (index >= cardModels.length) if (index >= cardModels.length)
return null; return null;
CardModel cardModel = cardModels[index]; CardModel cardModel = cardModels[index];
...@@ -115,20 +117,18 @@ class CardBuilder extends LazyBlockDelegate { ...@@ -115,20 +117,18 @@ class CardBuilder extends LazyBlockDelegate {
child: new Container( child: new Container(
height: cardModel.height, height: cardModel.height,
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: new Center(child: new Text(cardModel.label, style: cardLabelStyle)) child: new Center(child: new Text(cardModel.label, style: cardLabelStyle)),
) ),
) ),
); );
} }
@override @override
bool shouldRebuild(CardBuilder oldDelegate) { int get estimatedChildCount => cardModels.length;
return oldDelegate.cardModels != cardModels;
}
@override @override
double estimateTotalExtent(int firstIndex, int lastIndex, double minOffset, double firstStartOffset, double lastEndOffset) { bool shouldRebuild(CardBuilder oldDelegate) {
return (lastEndOffset - minOffset) * cardModels.length / (lastIndex + 1); return oldDelegate.cardModels != cardModels;
} }
} }
...@@ -144,7 +144,7 @@ class OverlayGeometryAppState extends State<OverlayGeometryApp> { ...@@ -144,7 +144,7 @@ class OverlayGeometryAppState extends State<OverlayGeometryApp> {
List<double> cardHeights = <double>[ List<double> cardHeights = <double>[
48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0, 48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0,
48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0, 48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0,
48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0 48.0, 63.0, 82.0, 146.0, 60.0, 55.0, 84.0, 96.0, 50.0,
]; ];
cardModels = new List<CardModel>.generate(cardHeights.length, (int i) { cardModels = new List<CardModel>.generate(cardHeights.length, (int i) {
Color color = Color.lerp(Colors.red[300], Colors.blue[900], i / cardHeights.length); Color color = Color.lerp(Colors.red[300], Colors.blue[900], i / cardHeights.length);
...@@ -152,15 +152,18 @@ class OverlayGeometryAppState extends State<OverlayGeometryApp> { ...@@ -152,15 +152,18 @@ class OverlayGeometryAppState extends State<OverlayGeometryApp> {
}); });
} }
void handleScroll(double offset) { bool handleScrollNotification(ScrollNotification2 notification) {
setState(() { if (notification is ScrollUpdateNotification && notification.depth == 1) {
double dy = markersScrollOffset - offset; setState(() {
markersScrollOffset = offset; double dy = markersScrollOffset - notification.metrics.extentBefore;
for (MarkerType type in markers.keys) { markersScrollOffset = notification.metrics.extentBefore;
Point oldPosition = markers[type]; for (MarkerType type in markers.keys) {
markers[type] = new Point(oldPosition.x, oldPosition.y + dy); Point oldPosition = markers[type];
} markers[type] = new Point(oldPosition.x, oldPosition.y + dy);
}); }
});
}
return false;
} }
void handleTapUp(GlobalKey target, Point globalPosition) { void handleTapUp(GlobalKey target, Point globalPosition) {
...@@ -182,15 +185,17 @@ class OverlayGeometryAppState extends State<OverlayGeometryApp> { ...@@ -182,15 +185,17 @@ class OverlayGeometryAppState extends State<OverlayGeometryApp> {
appBar: new AppBar(title: new Text('Tap a Card')), appBar: new AppBar(title: new Text('Tap a Card')),
body: new Container( body: new Container(
padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0), padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0),
child: new LazyBlock( child: new NotificationListener<ScrollNotification2>(
onScroll: handleScroll, onNotification: handleScrollNotification,
delegate: new CardBuilder( child: new ListView.custom(
cardModels: cardModels, childrenDelegate: new CardBuilder(
onTapUp: handleTapUp cardModels: cardModels,
) onTapUp: handleTapUp,
) ),
) ),
) ),
),
),
]; ];
for (MarkerType type in markers.keys) for (MarkerType type in markers.keys)
layers.add(new Marker(type: type, position: markers[type])); layers.add(new Marker(type: type, position: markers[type]));
...@@ -203,9 +208,9 @@ void main() { ...@@ -203,9 +208,9 @@ void main() {
theme: new ThemeData( theme: new ThemeData(
brightness: Brightness.light, brightness: Brightness.light,
primarySwatch: Colors.blue, primarySwatch: Colors.blue,
accentColor: Colors.redAccent[200] accentColor: Colors.redAccent[200],
), ),
title: 'Cards', title: 'Cards',
home: new OverlayGeometryApp() home: new OverlayGeometryApp(),
)); ));
} }
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