Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 17c676d

Browse files
committed
added Get Visible Columns Feature
1 parent fe74e10 commit 17c676d

6 files changed

Lines changed: 218 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [8.4.8] - 2025. 03. 06
4+
5+
* Added getVisibleColumns method & isColumnVisible
6+
37
## [8.4.7] - 2025. 03. 04
48

59
* Fix pagination bug

demo/lib/main.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:demo/screen/feature/check_visible_columns_screen.dart';
12
import 'package:flutter/foundation.dart';
23
import 'package:flutter/material.dart';
34

@@ -65,6 +66,8 @@ class MyApp extends StatelessWidget {
6566
const AddRowsAsynchronouslyScreen(),
6667
CellRendererScreen.routeName: (context) => const CellRendererScreen(),
6768
CellSelectionScreen.routeName: (context) => const CellSelectionScreen(),
69+
CheckVisibleColumnsScreen.routeName: (context) =>
70+
const CheckVisibleColumnsScreen(),
6871
RTLScreen.routeName: (context) => const RTLScreen(),
6972
ColumnFilteringScreen.routeName: (context) =>
7073
const ColumnFilteringScreen(),
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
}

demo/lib/screen/home_screen.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:math';
22

33
import 'package:demo/screen/empty_screen.dart';
4+
import 'package:demo/screen/feature/check_visible_columns_screen.dart';
45
import 'package:flutter/material.dart';
56
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
67

@@ -493,6 +494,16 @@ class PlutoFeatures extends StatelessWidget {
493494
Navigator.pushNamed(context, PagesListScreen.routeName);
494495
},
495496
),
497+
PlutoListTile(
498+
title: 'Check visible columns',
499+
description: 'Check visible columns.',
500+
onTapLiveDemo: () {
501+
Navigator.pushNamed(
502+
context,
503+
CheckVisibleColumnsScreen.routeName,
504+
);
505+
},
506+
),
496507
PlutoListTile.dark(
497508
title: 'Dark mode',
498509
description: 'Change the entire theme of the grid to Dark.',

lib/src/manager/pluto_grid_state_manager.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,35 @@ class PlutoGridStateManager extends PlutoGridStateChangeNotifier {
275275
);
276276
}
277277

278+
/// Returns a list of columns that are currently visible in the viewport
279+
List<PlutoColumn> getVisibleColumns() {
280+
if (refColumns.isEmpty) return [];
281+
282+
return refColumns.where((column) => isColumnVisible(column)).toList();
283+
}
284+
285+
/// Checks if a specific column is currently visible in the viewport
286+
bool isColumnVisible(PlutoColumn column) {
287+
if (column.hide) return false;
288+
289+
final RenderBox? gridRenderBox =
290+
gridKey.currentContext?.findRenderObject() as RenderBox?;
291+
292+
if (gridRenderBox == null) {
293+
return false;
294+
}
295+
296+
final scrollPosition = scroll.horizontal?.offset ?? 0;
297+
final viewportWidth = gridRenderBox.size.width;
298+
final viewportEnd = scrollPosition + viewportWidth;
299+
300+
final columnStart = column.startPosition;
301+
final columnEnd = columnStart + column.width;
302+
303+
// Column is visible if any part of it is in the viewport
304+
return (columnStart <= viewportEnd && columnEnd > scrollPosition);
305+
}
306+
278307
/// It handles the necessary settings when [rows] are first set or added to the [PlutoGrid].
279308
///
280309
/// {@template initialize_rows_params}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: pluto_grid_plus
22
description: PlutoGrid plus is a maintained version of PlutoGrid, PlutoGrid is a dataGrid that can be controlled by the keyboard on desktop and web. Of course, it works well on Android and IOS. (DataGrid, DataTable, Data Grid, Data Table, Sticky)
3-
version: 8.4.7
3+
version: 8.4.8
44
homepage: https://pluto.weblaze.dev
55
repository: https://github.com/doonfrs/pluto_grid_plus
66

0 commit comments

Comments
 (0)