|
| 1 | +import 'dart:math'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; |
| 6 | + |
| 7 | +class ExampleTests extends StatelessWidget { |
| 8 | + ExampleTests() : products = List.generate(50, (i)=> Product('test $i')); |
| 9 | + |
| 10 | + final List<Product> products; |
| 11 | + |
| 12 | + @override |
| 13 | + Widget build(BuildContext context) { |
| 14 | + return new Scaffold( |
| 15 | + appBar: new AppBar( |
| 16 | + title: new Text('random dynamic tile sizes'), |
| 17 | + ), |
| 18 | + body: CustomScrollView( |
| 19 | + slivers: <Widget>[ |
| 20 | + SliverStaggeredGrid.countBuilder( |
| 21 | + crossAxisCount: 2, |
| 22 | + staggeredTileBuilder: (_) => StaggeredTile.fit(1), |
| 23 | + itemBuilder: (context, index) => ProductGridItem(products[index],), |
| 24 | + itemCount: products.length, |
| 25 | + ), |
| 26 | + ], |
| 27 | + ) |
| 28 | + ); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class Leaf extends StatefulWidget { |
| 33 | + const Leaf({ Key key, this.child }) : super(key: key); |
| 34 | + final Widget child; |
| 35 | + @override |
| 36 | + _LeafState createState() => new _LeafState(); |
| 37 | +} |
| 38 | + |
| 39 | +class _LeafState extends State<Leaf> { |
| 40 | + bool _keepAlive = false; |
| 41 | + KeepAliveHandle _handle; |
| 42 | + |
| 43 | + @override |
| 44 | + void deactivate() { |
| 45 | + _handle?.release(); |
| 46 | + _handle = null; |
| 47 | + super.deactivate(); |
| 48 | + } |
| 49 | + |
| 50 | + void setKeepAlive(bool value) { |
| 51 | + _keepAlive = value; |
| 52 | + if (_keepAlive) { |
| 53 | + if (_handle == null) { |
| 54 | + _handle = new KeepAliveHandle(); |
| 55 | + new KeepAliveNotification(_handle).dispatch(context); |
| 56 | + } |
| 57 | + } else { |
| 58 | + _handle?.release(); |
| 59 | + _handle = null; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + Widget build(BuildContext context) { |
| 65 | + if (_keepAlive && _handle == null) { |
| 66 | + _handle = new KeepAliveHandle(); |
| 67 | + new KeepAliveNotification(_handle).dispatch(context); |
| 68 | + } |
| 69 | + return widget.child; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class Product { |
| 74 | + const Product(this.name); |
| 75 | + final String name; |
| 76 | +} |
| 77 | + |
| 78 | +class ProductGridItem extends StatelessWidget { |
| 79 | + const ProductGridItem(this.product); |
| 80 | + |
| 81 | + final Product product; |
| 82 | + |
| 83 | + @override |
| 84 | + Widget build(BuildContext context) { |
| 85 | + return new Card( |
| 86 | + child: Container( |
| 87 | + color: Colors.blue, |
| 88 | + height: 80.0, |
| 89 | + child: Center( |
| 90 | + child: Text(product.name), |
| 91 | + ), |
| 92 | + ) |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
0 commit comments