diff --git a/example/lib/main.dart b/example/lib/main.dart index 260b5266f..999a2ea43 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,3 +1,5 @@ +import 'dart:ffi'; + import 'package:flutter/material.dart'; import 'package:pluto_grid/pluto_grid.dart'; @@ -50,11 +52,20 @@ class _PlutoGridExamplePageState extends State { PlutoColumn( title: 'Role', field: 'role', - type: PlutoColumnType.select([ - 'Programmer', - 'Designer', - 'Owner', - ]), + type: PlutoColumnType.select( + [ + 'Programmer', + 'Designer', + 'Owner', + ], + onItemSelected: (PlutoGridOnSelectedEvent event) { + if (event.cell!.value == "Programmer") { + print("Hello Programmer"); + } else { + print("Hello Developer"); + } + }, + ), ), PlutoColumn( title: 'Joined', diff --git a/lib/src/model/pluto_column_type.dart b/lib/src/model/pluto_column_type.dart index 12203c48b..f4c6f73b6 100644 --- a/lib/src/model/pluto_column_type.dart +++ b/lib/src/model/pluto_column_type.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:intl/intl.dart' as intl; +import 'package:pluto_grid/pluto_grid.dart'; abstract class PlutoColumnType { dynamic get defaultValue; @@ -87,17 +88,26 @@ abstract class PlutoColumnType { /// Provides a selection list and sets it as a selection column. /// + ///The function[onItemSelected] is callback function, which is called + ///when selecting an item from the items list. + /// + ///when selecting an item the [onItemSelected] function will be called + ///immediately and it return [PlutoGridOnSelectedEvent] event, so you + ///you can use it make decisions. + /// /// If [enableColumnFilter] is true, column filtering is enabled in the selection popup. /// /// Set the suffixIcon in the [popupIcon] cell. Tapping this icon will open a selection popup. /// The default icon is displayed, and if this value is set to null , the icon does not appear. factory PlutoColumnType.select( List items, { + final Function(PlutoGridOnSelectedEvent event)? onItemSelected, dynamic defaultValue = '', bool enableColumnFilter = false, IconData? popupIcon = Icons.arrow_drop_down, }) { return PlutoColumnTypeSelect( + onItemSelected: onItemSelected ?? (event) {}, defaultValue: defaultValue, items: items, enableColumnFilter: enableColumnFilter, @@ -360,10 +370,13 @@ class PlutoColumnTypeSelect final bool enableColumnFilter; + final Function(PlutoGridOnSelectedEvent event) onItemSelected; + @override final IconData? popupIcon; const PlutoColumnTypeSelect({ + required this.onItemSelected, this.defaultValue, required this.items, required this.enableColumnFilter, diff --git a/lib/src/ui/cells/pluto_select_cell.dart b/lib/src/ui/cells/pluto_select_cell.dart index b5b6ecd2d..cfb4c69d6 100644 --- a/lib/src/ui/cells/pluto_select_cell.dart +++ b/lib/src/ui/cells/pluto_select_cell.dart @@ -84,6 +84,12 @@ class PlutoSelectCellState extends State }).toList(); } + @override + void onSelected(PlutoGridOnSelectedEvent event) { + widget.column.type.select.onItemSelected(event); + super.onSelected(event); + } + @override void onLoaded(PlutoGridOnLoadedEvent event) { super.onLoaded(event);