|
| 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 | +} |
0 commit comments