|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:dynamic_layouts/dynamic_layouts.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + runApp(const StaggeredExample()); |
| 10 | +} |
| 11 | + |
| 12 | +/// A staggered layout example. Clicking the upper-right button will change |
| 13 | +/// between a grid with a fixed cross axis count and one with a main axis |
| 14 | +/// extent. |
| 15 | +class StaggeredExample extends StatefulWidget { |
| 16 | + /// Creates a [StaggeredExample]. |
| 17 | + const StaggeredExample({super.key}); |
| 18 | + |
| 19 | + @override |
| 20 | + State<StaggeredExample> createState() => _StaggeredExampleState(); |
| 21 | +} |
| 22 | + |
| 23 | +class _StaggeredExampleState extends State<StaggeredExample> { |
| 24 | + final List<Widget> children = List<Widget>.generate( |
| 25 | + 50, |
| 26 | + (int index) => _DynamicSizedTile(index: index), |
| 27 | + ); |
| 28 | + |
| 29 | + bool fixedCrossAxisCount = true; |
| 30 | + |
| 31 | + @override |
| 32 | + Widget build(BuildContext context) { |
| 33 | + return Scaffold( |
| 34 | + appBar: AppBar( |
| 35 | + title: const Text('Staggered Layout Example'), |
| 36 | + actions: <Widget>[ |
| 37 | + Padding( |
| 38 | + padding: const EdgeInsets.only(right: 50.0), |
| 39 | + child: TextButton( |
| 40 | + onPressed: () { |
| 41 | + setState(() { |
| 42 | + fixedCrossAxisCount = !fixedCrossAxisCount; |
| 43 | + }); |
| 44 | + }, |
| 45 | + child: Text( |
| 46 | + fixedCrossAxisCount ? 'FIXED' : 'MAX', |
| 47 | + style: const TextStyle(color: Colors.white), |
| 48 | + ), |
| 49 | + ), |
| 50 | + ), |
| 51 | + ], |
| 52 | + ), |
| 53 | + floatingActionButton: FloatingActionButton( |
| 54 | + onPressed: () { |
| 55 | + setState(() { |
| 56 | + children.add(_DynamicSizedTile(index: children.length)); |
| 57 | + }); |
| 58 | + }, |
| 59 | + child: const Icon(Icons.plus_one), |
| 60 | + ), |
| 61 | + body: fixedCrossAxisCount |
| 62 | + ? DynamicGridView.staggered( |
| 63 | + crossAxisCount: 4, |
| 64 | + children: <Widget>[...children], |
| 65 | + ) |
| 66 | + : DynamicGridView.staggered( |
| 67 | + maxCrossAxisExtent: 100, |
| 68 | + children: <Widget>[...children], |
| 69 | + ), |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +class _DynamicSizedTile extends StatelessWidget { |
| 75 | + const _DynamicSizedTile({required this.index}); |
| 76 | + |
| 77 | + final int index; |
| 78 | + |
| 79 | + @override |
| 80 | + Widget build(BuildContext context) { |
| 81 | + return Container( |
| 82 | + height: index % 3 * 50 + 20, |
| 83 | + color: Colors.amber[(index % 8 + 1) * 100], |
| 84 | + child: Text('Index $index'), |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments