|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; |
| 3 | + |
| 4 | +class DeviceTypeList { |
| 5 | + String deviceTypeName = 'test'; |
| 6 | +} |
| 7 | + |
| 8 | +class DeviceTypeItem extends StatefulWidget { |
| 9 | + final List<DeviceTypeList> deviceTypes; |
| 10 | + final Function onSelected; |
| 11 | + |
| 12 | + DeviceTypeItem(this.deviceTypes, this.onSelected); |
| 13 | + |
| 14 | + @override |
| 15 | + _DeviceTypeItemState createState() => _DeviceTypeItemState(); |
| 16 | +} |
| 17 | + |
| 18 | +class _DeviceTypeItemState extends State<DeviceTypeItem> { |
| 19 | + int _selectIndex = -1; |
| 20 | + |
| 21 | + _DeviceTypeItemState(); |
| 22 | + |
| 23 | + _selectDevice(int index) { |
| 24 | + setState(() { |
| 25 | + _selectIndex = index; |
| 26 | + }); |
| 27 | +// onSelected(deviceTypes[_selectIndex].deviceTypeName); |
| 28 | + } |
| 29 | + |
| 30 | + _tmpData() { |
| 31 | + return List.generate( |
| 32 | + 10, |
| 33 | + (int index) => GestureDetector( |
| 34 | + child: Container( |
| 35 | + child: Text(widget.deviceTypes[0].deviceTypeName), |
| 36 | + color: _selectIndex == index ? Colors.blue : Colors.brown, |
| 37 | + padding: EdgeInsets.only(top: 4, bottom: 4), |
| 38 | + alignment: Alignment.center, |
| 39 | + ), |
| 40 | + onTap: () => _selectDevice(index), |
| 41 | + )); |
| 42 | + } |
| 43 | + |
| 44 | + @override |
| 45 | + Widget build(BuildContext context) { |
| 46 | + return Container( |
| 47 | + padding: EdgeInsets.all(12), |
| 48 | +// child: GridView.count( |
| 49 | +// crossAxisCount: 3, |
| 50 | +// children: _tmpData(), |
| 51 | +// shrinkWrap: true, |
| 52 | +// ), |
| 53 | + |
| 54 | + child: StaggeredGridView.count( |
| 55 | + crossAxisCount: 3, |
| 56 | + children: _tmpData(), |
| 57 | + staggeredTiles: List.generate(10, (int index) => StaggeredTile.fit(1)), |
| 58 | + shrinkWrap: true, |
| 59 | + ), |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// import 'package:flutter/material.dart'; |
| 65 | +// import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; |
| 66 | + |
| 67 | +// void main() => runApp(MyApp()); |
| 68 | + |
| 69 | +// class MyApp extends StatelessWidget { |
| 70 | +// @override |
| 71 | +// Widget build(BuildContext context) { |
| 72 | +// return MaterialApp( |
| 73 | +// title: 'StaggeredGridView Demo', |
| 74 | +// theme: ThemeData( |
| 75 | +// primarySwatch: Colors.blue, |
| 76 | +// ), |
| 77 | +// home: MyScreen(), |
| 78 | +// ); |
| 79 | +// } |
| 80 | +// } |
| 81 | + |
| 82 | +// class MyScreen extends StatefulWidget { |
| 83 | +// @override |
| 84 | +// _MyScreenState createState() => new _MyScreenState(); |
| 85 | +// } |
| 86 | + |
| 87 | +// class _MyScreenState extends State<MyScreen> { |
| 88 | +// int _count = 0; |
| 89 | + |
| 90 | +// @override |
| 91 | +// Widget build(BuildContext context) { |
| 92 | +// return Scaffold( |
| 93 | +// body: GridTest(_count, _count), |
| 94 | +// floatingActionButton: FloatingActionButton( |
| 95 | +// child: Icon(Icons.add), |
| 96 | +// onPressed: () { |
| 97 | +// setState(() { |
| 98 | +// _count++; |
| 99 | +// }); |
| 100 | +// }, |
| 101 | +// ), |
| 102 | +// ); |
| 103 | +// } |
| 104 | +// } |
| 105 | + |
| 106 | +// class GridTest extends StatelessWidget { |
| 107 | +// GridTest(this.count, this.value); |
| 108 | +// final int count; |
| 109 | +// final int value; |
| 110 | + |
| 111 | +// @override |
| 112 | +// Widget build(BuildContext context) { |
| 113 | +// return GridView.builder( |
| 114 | +// gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( |
| 115 | +// crossAxisCount: 3, |
| 116 | +// crossAxisSpacing: 2, |
| 117 | +// mainAxisSpacing: 2, |
| 118 | +// ), |
| 119 | +// itemCount: count, |
| 120 | +// itemBuilder: (context, index) { |
| 121 | +// return Container( |
| 122 | +// color: Colors.blue, |
| 123 | +// child: Text('$value'), |
| 124 | +// ); |
| 125 | +// }, |
| 126 | +// ); |
| 127 | +// } |
| 128 | +// } |
| 129 | + |
| 130 | +// class StaggeredTest extends StatelessWidget { |
| 131 | +// StaggeredTest(this.count, this.value); |
| 132 | +// final int count; |
| 133 | +// final int value; |
| 134 | + |
| 135 | +// @override |
| 136 | +// Widget build(BuildContext context) { |
| 137 | +// return StaggeredGridView.countBuilder( |
| 138 | +// itemCount: count, |
| 139 | +// crossAxisCount: 3, |
| 140 | +// crossAxisSpacing: 2, |
| 141 | +// mainAxisSpacing: 2, |
| 142 | +// addAutomaticKeepAlives: false, |
| 143 | +// staggeredTileBuilder: (index) => StaggeredTile.extent(1, 30), |
| 144 | +// itemBuilder: (context, index) { |
| 145 | +// return Container( |
| 146 | +// color: Colors.green, |
| 147 | +// child: Text('$value'), |
| 148 | +// ); |
| 149 | +// }, |
| 150 | +// ); |
| 151 | +// } |
| 152 | +// } |
0 commit comments