|
| 1 | +import 'package:faker/faker.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter/services.dart'; |
| 4 | +import 'package:pluto_grid_plus/pluto_grid_plus.dart'; |
| 5 | + |
| 6 | +import '../../widget/pluto_example_screen.dart'; |
| 7 | + |
| 8 | +class CheckVisibleColumnsScreen extends StatefulWidget { |
| 9 | + static const routeName = 'check-visible-columns'; |
| 10 | + |
| 11 | + const CheckVisibleColumnsScreen({super.key}); |
| 12 | + |
| 13 | + @override |
| 14 | + _CheckVisibleColumnsScreenState createState() => |
| 15 | + _CheckVisibleColumnsScreenState(); |
| 16 | +} |
| 17 | + |
| 18 | +class _CheckVisibleColumnsScreenState extends State<CheckVisibleColumnsScreen> { |
| 19 | + final List<PlutoColumn> columns = []; |
| 20 | + |
| 21 | + final List<PlutoRow> rows = []; |
| 22 | + |
| 23 | + late PlutoGridStateManager stateManager; |
| 24 | + |
| 25 | + @override |
| 26 | + void initState() { |
| 27 | + super.initState(); |
| 28 | + |
| 29 | + columns.addAll([ |
| 30 | + for (var i = 1; i < 50; i++) |
| 31 | + PlutoColumn( |
| 32 | + title: 'Column $i', |
| 33 | + field: 'column$i', |
| 34 | + type: PlutoColumnType.text(), |
| 35 | + ), |
| 36 | + ]); |
| 37 | + |
| 38 | + rows.addAll([ |
| 39 | + for (var i = 1; i < 50; i++) |
| 40 | + PlutoRow(cells: { |
| 41 | + for (var column in columns) |
| 42 | + column.field: PlutoCell(value: faker.lorem.sentence()), |
| 43 | + }), |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + Widget build(BuildContext context) { |
| 49 | + return PlutoExampleScreen( |
| 50 | + title: 'Check visible columns', |
| 51 | + topTitle: 'Check visible columns', |
| 52 | + topContents: const [ |
| 53 | + Text('You can check visible columns.'), |
| 54 | + ], |
| 55 | + body: PlutoGrid( |
| 56 | + columns: columns, |
| 57 | + rows: rows, |
| 58 | + onLoaded: (PlutoGridOnLoadedEvent event) { |
| 59 | + stateManager = event.stateManager; |
| 60 | + }, |
| 61 | + createHeader: (stateManager) => _Header(stateManager: stateManager), |
| 62 | + ), |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class _Header extends StatefulWidget { |
| 68 | + const _Header({ |
| 69 | + required this.stateManager, |
| 70 | + }); |
| 71 | + |
| 72 | + final PlutoGridStateManager stateManager; |
| 73 | + |
| 74 | + @override |
| 75 | + State<_Header> createState() => _HeaderState(); |
| 76 | +} |
| 77 | + |
| 78 | +class _HeaderState extends State<_Header> { |
| 79 | + PlutoGridSelectingMode gridSelectingMode = PlutoGridSelectingMode.row; |
| 80 | + |
| 81 | + @override |
| 82 | + void initState() { |
| 83 | + super.initState(); |
| 84 | + } |
| 85 | + |
| 86 | + void _showVisibleColumns() { |
| 87 | + showDialog( |
| 88 | + context: context, |
| 89 | + builder: (context) => SimpleDialog( |
| 90 | + title: const Text('Visible columns'), |
| 91 | + children: [ |
| 92 | + for (var element in widget.stateManager.getVisibleColumns()) |
| 93 | + SimpleDialogOption( |
| 94 | + onPressed: () {}, |
| 95 | + child: Text(element.title), |
| 96 | + ) |
| 97 | + ], |
| 98 | + )); |
| 99 | + } |
| 100 | + |
| 101 | + void _isColumnVisible() { |
| 102 | + int? columnIndex; |
| 103 | + |
| 104 | + showDialog( |
| 105 | + context: context, |
| 106 | + builder: (context) => AlertDialog( |
| 107 | + title: const Text('Is column visible?'), |
| 108 | + content: StatefulBuilder(builder: (context, setState) { |
| 109 | + return Column( |
| 110 | + mainAxisSize: MainAxisSize.min, |
| 111 | + children: [ |
| 112 | + Row( |
| 113 | + children: [ |
| 114 | + SizedBox( |
| 115 | + width: 200, |
| 116 | + child: TextField( |
| 117 | + onChanged: (value) { |
| 118 | + columnIndex = int.tryParse(value); |
| 119 | + }, |
| 120 | + keyboardType: TextInputType.number, |
| 121 | + inputFormatters: [ |
| 122 | + FilteringTextInputFormatter.digitsOnly |
| 123 | + ], |
| 124 | + decoration: const InputDecoration( |
| 125 | + labelText: 'Column index', |
| 126 | + ), |
| 127 | + ), |
| 128 | + ), |
| 129 | + const SizedBox(width: 10), |
| 130 | + ElevatedButton( |
| 131 | + onPressed: () { |
| 132 | + setState(() {}); |
| 133 | + }, |
| 134 | + child: const Text('Check'), |
| 135 | + ) |
| 136 | + ], |
| 137 | + ), |
| 138 | + const SizedBox(height: 20), |
| 139 | + if (columnIndex != null) |
| 140 | + Text(widget.stateManager.isColumnVisible( |
| 141 | + widget.stateManager.refColumns[columnIndex!]) |
| 142 | + ? 'Visible' |
| 143 | + : 'Hidden') |
| 144 | + ], |
| 145 | + ); |
| 146 | + }))); |
| 147 | + } |
| 148 | + |
| 149 | + @override |
| 150 | + Widget build(BuildContext context) { |
| 151 | + return SingleChildScrollView( |
| 152 | + scrollDirection: Axis.horizontal, |
| 153 | + child: Padding( |
| 154 | + padding: const EdgeInsets.symmetric(horizontal: 8.0), |
| 155 | + child: Wrap( |
| 156 | + spacing: 10, |
| 157 | + crossAxisAlignment: WrapCrossAlignment.center, |
| 158 | + children: [ |
| 159 | + ElevatedButton( |
| 160 | + onPressed: _showVisibleColumns, |
| 161 | + child: const Text('Show visible columns')), |
| 162 | + ElevatedButton( |
| 163 | + onPressed: _isColumnVisible, |
| 164 | + child: const Text('Is column visible?')), |
| 165 | + ], |
| 166 | + ), |
| 167 | + ), |
| 168 | + ); |
| 169 | + } |
| 170 | +} |
0 commit comments