|
| 1 | +/// Package imports |
| 2 | +
|
| 3 | +// ignore_for_file: depend_on_referenced_packages |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +/// DataGrid import |
| 8 | +import 'package:syncfusion_flutter_datagrid/datagrid.dart'; |
| 9 | + |
| 10 | +/// Local import |
| 11 | +import '../../../model/sample_view.dart'; |
| 12 | +import '../datagridsource/product_datagridsource.dart'; |
| 13 | + |
| 14 | +/// Renders column drag and drop data grid sample |
| 15 | +class DataGridColumnDragAndDrop extends SampleView { |
| 16 | + /// Creates column drag and drop data grid sample |
| 17 | + const DataGridColumnDragAndDrop({Key? key}) : super(key: key); |
| 18 | + |
| 19 | + @override |
| 20 | + _DataGridColumnDragAndDropState createState() => |
| 21 | + _DataGridColumnDragAndDropState(); |
| 22 | +} |
| 23 | + |
| 24 | +class _DataGridColumnDragAndDropState extends SampleViewState { |
| 25 | + GlobalKey key = GlobalKey(); |
| 26 | + |
| 27 | + /// DataGridSource required for SfDataGrid to obtain the row data. |
| 28 | + late ProductDataGridSource source; |
| 29 | + |
| 30 | + /// Collection of GridColumn and it required for SfDataGrid |
| 31 | + late List<GridColumn> columns; |
| 32 | + |
| 33 | + /// Determine which device the sample loaded |
| 34 | + late bool isWebOrDesktop; |
| 35 | + |
| 36 | + @override |
| 37 | + void initState() { |
| 38 | + super.initState(); |
| 39 | + isWebOrDesktop = model.isWeb || model.isDesktop; |
| 40 | + columns = getColumns(); |
| 41 | + source = ProductDataGridSource('Column Drag and Drop', |
| 42 | + productDataCount: 20, columns: columns); |
| 43 | + } |
| 44 | + |
| 45 | + @override |
| 46 | + Widget build(BuildContext context) { |
| 47 | + return SfDataGrid( |
| 48 | + key: key, |
| 49 | + source: source, |
| 50 | + columns: columns, |
| 51 | + allowColumnsDragging: true, |
| 52 | + onColumnDragging: (details) { |
| 53 | + if (details.action == DataGridColumnDragAction.dropped) { |
| 54 | + if (details.to == null) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + final GridColumn rearrangeColumn = columns[details.from]; |
| 58 | + columns.removeAt(details.from); |
| 59 | + columns.insert(details.to!, rearrangeColumn); |
| 60 | + } |
| 61 | + |
| 62 | + source.updateDataSource(); |
| 63 | + return true; |
| 64 | + }, |
| 65 | + gridLinesVisibility: GridLinesVisibility.both, |
| 66 | + headerGridLinesVisibility: GridLinesVisibility.both, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + List<GridColumn> getColumns() { |
| 71 | + List<GridColumn> columns; |
| 72 | + columns = <GridColumn>[ |
| 73 | + GridColumn( |
| 74 | + columnName: 'id', |
| 75 | + width: 160, |
| 76 | + label: Container( |
| 77 | + alignment: Alignment.centerRight, |
| 78 | + padding: const EdgeInsets.all(8), |
| 79 | + child: const Text( |
| 80 | + 'Order ID', |
| 81 | + overflow: TextOverflow.ellipsis, |
| 82 | + ), |
| 83 | + )), |
| 84 | + GridColumn( |
| 85 | + columnName: 'productId', |
| 86 | + width: 170, |
| 87 | + label: Container( |
| 88 | + alignment: Alignment.centerRight, |
| 89 | + padding: const EdgeInsets.all(8), |
| 90 | + child: const Text( |
| 91 | + 'Product ID', |
| 92 | + overflow: TextOverflow.ellipsis, |
| 93 | + ), |
| 94 | + )), |
| 95 | + GridColumn( |
| 96 | + columnName: 'name', |
| 97 | + width: 190, |
| 98 | + label: Container( |
| 99 | + alignment: Alignment.centerLeft, |
| 100 | + padding: const EdgeInsets.all(8), |
| 101 | + child: const Text( |
| 102 | + 'Customer Name', |
| 103 | + overflow: TextOverflow.ellipsis, |
| 104 | + ), |
| 105 | + )), |
| 106 | + GridColumn( |
| 107 | + columnName: 'product', |
| 108 | + width: 140, |
| 109 | + label: Container( |
| 110 | + alignment: Alignment.centerLeft, |
| 111 | + padding: const EdgeInsets.all(8), |
| 112 | + child: const Text( |
| 113 | + 'Product', |
| 114 | + overflow: TextOverflow.ellipsis, |
| 115 | + ), |
| 116 | + )), |
| 117 | + GridColumn( |
| 118 | + columnName: 'orderDate', |
| 119 | + width: 150, |
| 120 | + label: Container( |
| 121 | + alignment: Alignment.centerRight, |
| 122 | + padding: const EdgeInsets.all(8), |
| 123 | + child: const Text( |
| 124 | + 'Order Date', |
| 125 | + overflow: TextOverflow.ellipsis, |
| 126 | + ), |
| 127 | + )), |
| 128 | + GridColumn( |
| 129 | + columnName: 'quantity', |
| 130 | + width: 150, |
| 131 | + label: Container( |
| 132 | + alignment: Alignment.centerRight, |
| 133 | + padding: const EdgeInsets.all(8), |
| 134 | + child: const Text( |
| 135 | + 'Quantity', |
| 136 | + overflow: TextOverflow.ellipsis, |
| 137 | + ), |
| 138 | + )), |
| 139 | + GridColumn( |
| 140 | + columnName: 'city', |
| 141 | + width: 140, |
| 142 | + label: Container( |
| 143 | + alignment: Alignment.centerLeft, |
| 144 | + padding: const EdgeInsets.all(8), |
| 145 | + child: const Text( |
| 146 | + 'City', |
| 147 | + overflow: TextOverflow.ellipsis, |
| 148 | + ), |
| 149 | + )), |
| 150 | + GridColumn( |
| 151 | + columnName: 'unitPrice', |
| 152 | + width: 140, |
| 153 | + label: Container( |
| 154 | + alignment: Alignment.centerRight, |
| 155 | + padding: const EdgeInsets.all(8), |
| 156 | + child: const Text( |
| 157 | + 'Unit Price', |
| 158 | + overflow: TextOverflow.ellipsis, |
| 159 | + ), |
| 160 | + )), |
| 161 | + ]; |
| 162 | + return columns; |
| 163 | + } |
| 164 | +} |
0 commit comments