Skip to content

Commit d16e280

Browse files
committed
Fix issue #23
1 parent d253261 commit d16e280

File tree

3 files changed

+176
-20
lines changed

3 files changed

+176
-20
lines changed

example/lib/example_5.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@ const List<Color> _kColors = const <Color>[
1414
];
1515

1616
List<StaggeredTile> _generateRandomTiles(int count) {
17-
Random rnd = new Random();
18-
return new List.generate(count,
19-
(i) => new StaggeredTile.count(rnd.nextInt(4) + 1, rnd.nextInt(6) + 1));
17+
Random rnd = Random();
18+
return List.generate(count,
19+
(i) => StaggeredTile.count(rnd.nextInt(4) + 1, rnd.nextInt(6) + 1));
20+
}
21+
22+
List<Color> _generateRandomColors(int count) {
23+
Random rnd = Random();
24+
return List.generate(count, (i) => _kColors[rnd.nextInt(_kColors.length)]);
2025
}
2126

2227
class Example05 extends StatelessWidget {
2328
Example05()
24-
: _random = new Random(),
25-
_tiles = _generateRandomTiles(_kItemCount).toList();
29+
: _tiles = _generateRandomTiles(_kItemCount).toList(),
30+
_colors = _generateRandomColors(_kItemCount).toList();
2631

2732
static const int _kItemCount = 1000;
28-
final Random _random;
2933
final List<StaggeredTile> _tiles;
34+
final List<Color> _colors;
3035

3136
@override
3237
Widget build(BuildContext context) {
@@ -50,7 +55,7 @@ class Example05 extends StatelessWidget {
5055
Widget _getChild(BuildContext context, int index) {
5156
return new Container(
5257
key: new ObjectKey('$index'),
53-
color: _kColors[_random.nextInt(_kColors.length)],
58+
color: _colors[index],
5459
child: new Center(
5560
child: new CircleAvatar(
5661
backgroundColor: Colors.white,

example/lib/main_issue_23.dart

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
5+
6+
void main() => runApp(MyApp());
7+
8+
class MyApp extends StatelessWidget {
9+
@override
10+
Widget build(BuildContext context) {
11+
return MaterialApp(
12+
title: 'StaggeredGridView Demo',
13+
theme: ThemeData(
14+
primarySwatch: Colors.blue,
15+
),
16+
home: BlocProvider<SimpleBloc>(
17+
bloc: SimpleBloc(),
18+
child: MyScreen(),
19+
),
20+
);
21+
}
22+
}
23+
24+
class MyScreen extends StatelessWidget {
25+
@override
26+
Widget build(BuildContext context) {
27+
return Scaffold(
28+
body: StreamBuilder<int>(
29+
stream: BlocProvider.of<SimpleBloc>(context).counter,
30+
initialData: 0,
31+
builder: (context, snapshot) =>
32+
StaggeredTest(snapshot.data, snapshot.data),
33+
),
34+
floatingActionButton: FloatingActionButton(
35+
child: Icon(Icons.add),
36+
onPressed: BlocProvider.of<SimpleBloc>(context).increment,
37+
),
38+
);
39+
}
40+
}
41+
42+
class GridTest extends StatelessWidget {
43+
GridTest(this.count, this.value);
44+
final int count;
45+
final int value;
46+
47+
@override
48+
Widget build(BuildContext context) {
49+
return GridView.builder(
50+
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
51+
crossAxisCount: 3,
52+
crossAxisSpacing: 2,
53+
mainAxisSpacing: 2,
54+
),
55+
itemCount: count,
56+
itemBuilder: (context, index) {
57+
return Container(
58+
color: Colors.blue,
59+
child: Text('$value'),
60+
);
61+
},
62+
);
63+
}
64+
}
65+
66+
class StaggeredTest extends StatelessWidget {
67+
StaggeredTest(this.count, this.value);
68+
final int count;
69+
final int value;
70+
71+
@override
72+
Widget build(BuildContext context) {
73+
return StaggeredGridView.countBuilder(
74+
itemCount: count,
75+
crossAxisCount: 3,
76+
crossAxisSpacing: 2,
77+
mainAxisSpacing: 2,
78+
addAutomaticKeepAlives: false,
79+
staggeredTileBuilder: (index) => StaggeredTile.extent(1, 30),
80+
itemBuilder: (context, index) {
81+
return Container(
82+
color: Colors.green,
83+
child: Text('$value'),
84+
);
85+
},
86+
);
87+
}
88+
}
89+
90+
abstract class Disposable {
91+
void dispose();
92+
}
93+
94+
class SimpleBloc implements Disposable {
95+
SimpleBloc._(this._counterController)
96+
: counter = _counterController.stream.asBroadcastStream() {
97+
_counter = 0;
98+
}
99+
100+
factory SimpleBloc() {
101+
return SimpleBloc._(StreamController<int>());
102+
}
103+
104+
final StreamController<int> _counterController;
105+
final Stream<int> counter;
106+
int _counter;
107+
108+
void dispose() {
109+
_counterController.close();
110+
}
111+
112+
void increment() {
113+
_counter++;
114+
_counterController.sink.add(_counter);
115+
}
116+
}
117+
118+
class BlocProvider<T extends Disposable> extends StatefulWidget {
119+
BlocProvider({
120+
Key key,
121+
@required this.child,
122+
@required this.bloc,
123+
}) : super(key: key);
124+
125+
final T bloc;
126+
final Widget child;
127+
128+
@override
129+
_BlocProviderState<T> createState() => _BlocProviderState<T>();
130+
131+
static T of<T extends Disposable>(BuildContext context) {
132+
final type = _typeOf<BlocProvider<T>>();
133+
BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
134+
return provider.bloc;
135+
}
136+
137+
static Type _typeOf<T>() => T;
138+
}
139+
140+
class _BlocProviderState<T> extends State<BlocProvider<Disposable>> {
141+
@override
142+
void dispose() {
143+
widget.bloc.dispose();
144+
super.dispose();
145+
}
146+
147+
@override
148+
Widget build(BuildContext context) {
149+
return widget.child;
150+
}
151+
}

lib/src/widgets/sliver.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import 'package:flutter_staggered_grid_view/src/widgets/staggered_tile.dart';
1111
/// A base class for sliver that have multiple variable size box children.
1212
///
1313
/// Helps subclasses build their children lazily using a [SliverVariableSizeChildDelegate].
14-
abstract class SliverVariableSizeBoxAdaptorWidget extends SliverWithKeepAliveWidget {
14+
abstract class SliverVariableSizeBoxAdaptorWidget
15+
extends SliverWithKeepAliveWidget {
1516
/// Initializes fields for subclasses.
1617
const SliverVariableSizeBoxAdaptorWidget({
1718
Key key,
1819
@required this.delegate,
19-
}) : super(key: key);
20+
}) : super(key: key);
2021

2122
/// The delegate that provides the children for this widget.
2223
///
@@ -48,12 +49,12 @@ abstract class SliverVariableSizeBoxAdaptorWidget extends SliverWithKeepAliveWid
4849
/// The default implementation defers to [delegate] via its
4950
/// [SliverChildDelegate.estimateMaxScrollOffset] method.
5051
double estimateMaxScrollOffset(
51-
SliverConstraints constraints,
52-
int firstIndex,
53-
int lastIndex,
54-
double leadingScrollOffset,
55-
double trailingScrollOffset,
56-
) {
52+
SliverConstraints constraints,
53+
int firstIndex,
54+
int lastIndex,
55+
double leadingScrollOffset,
56+
double trailingScrollOffset,
57+
) {
5758
assert(lastIndex >= firstIndex);
5859
return delegate.estimateMaxScrollOffset(
5960
firstIndex,
@@ -66,8 +67,8 @@ abstract class SliverVariableSizeBoxAdaptorWidget extends SliverWithKeepAliveWid
6667
@override
6768
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
6869
super.debugFillProperties(properties);
69-
properties.add(new DiagnosticsProperty<SliverChildDelegate>(
70-
'delegate', delegate));
70+
properties.add(
71+
new DiagnosticsProperty<SliverChildDelegate>('delegate', delegate));
7172
}
7273
}
7374

@@ -128,8 +129,7 @@ class SliverVariableSizeBoxAdaptorElement extends RenderObjectElement
128129
}
129130

130131
Widget _build(int index) {
131-
return _childWidgets.putIfAbsent(
132-
index, () => widget.delegate.build(this, index));
132+
return _childWidgets[index] = widget.delegate.build(this, index);
133133
}
134134

135135
@override
@@ -558,4 +558,4 @@ class SliverStaggeredGrid extends SliverVariableSizeBoxAdaptorWidget {
558558
trailingScrollOffset,
559559
);
560560
}
561-
}
561+
}

0 commit comments

Comments
 (0)