Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for custom cell editor. #41

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MyApp extends StatelessWidget {
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

Expand Down Expand Up @@ -72,13 +72,13 @@ class _MyHomePageState extends State<MyHomePage> {
/// Access the current state of Editable
void _addNewRow() {
setState(() {
_editableKey.currentState.createRow();
_editableKey.currentState!.createRow();
});
}

///Print only edited rows.
void _printEditedRows() {
List editedRows = _editableKey.currentState.editedRows;
List editedRows = _editableKey.currentState!.editedRows;
print(editedRows);
}

Expand Down Expand Up @@ -110,8 +110,8 @@ class _MyHomePageState extends State<MyHomePage> {
columns: cols,
rows: rows,
zebraStripe: true,
stripeColor1: Colors.blue[50],
stripeColor2: Colors.grey[200],
stripeColor1: Colors.blue[50]!,
stripeColor2: Colors.grey[200]!,
onRowSaved: (value) {
print(value);
},
Expand All @@ -137,6 +137,46 @@ class _MyHomePageState extends State<MyHomePage> {
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blue),
borderRadius: BorderRadius.all(Radius.circular(0))),
//cellEditorWidget: getCellEditorWithInvertedStripesWidget,
),
);
}

Widget getCellEditorWithInvertedStripesWidget(
cellData,
int rowNumber,
columnIndex,
TextAlign tdAlignment,
TextStyle? tdStyle,
double tdPaddingLeft,
double tdPaddingTop,
double tdPaddingBottom,
double tdPaddingRight,
int tdEditableMaxLines,
Color stripeColor1,
Color stripeColor2,
bool zebraStripe,
InputBorder? focusedBorder,
ValueChanged<String>? onSubmitted,
ValueChanged<String> onChanged) {
return TextFormField(
textAlign: tdAlignment,
style: tdStyle,
initialValue: cellData.toString(),
onFieldSubmitted: onSubmitted,
onChanged: onChanged,
textAlignVertical: TextAlignVertical.center,
maxLines: tdEditableMaxLines,
decoration: InputDecoration(
filled: zebraStripe,
fillColor: rowNumber % 2 == 1.0 ? stripeColor1 : stripeColor2,
contentPadding: EdgeInsets.only(
left: tdPaddingLeft,
right: tdPaddingRight,
top: tdPaddingTop,
bottom: tdPaddingBottom),
border: InputBorder.none,
focusedBorder: focusedBorder,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand Down
141 changes: 140 additions & 1 deletion lib/editable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class Editable extends StatefulWidget {
this.stripeColor1 = Colors.white,
this.stripeColor2 = Colors.black12,
this.zebraStripe = false,
this.focusedBorder})
this.focusedBorder,
this.cellEditorWidget})
: super(key: key);

/// A data set to create headers
Expand Down Expand Up @@ -269,6 +270,50 @@ class Editable extends StatefulWidget {
/// returns only values if row is edited, otherwise returns a string ['no edit']
final ValueChanged<dynamic>? onRowSaved;

/// Function called to render the editable cell's. If not provided then
/// editable will use the defulat one.
/// Importan parameters are:
/// Example (this is the default cell editor widget):
/// Widget getCellEditorWidget(
/// cellData,
/// int rowNumber,
/// columnIndex
/// TextAlign tdAlignment,
/// TextStyle? tdStyle,
/// double tdPaddingLeft,
/// double tdPaddingTop,
/// double tdPaddingBottom,
/// double tdPaddingRight,
/// int tdEditableMaxLines,
/// Color stripeColor1,
/// Color stripeColor2,
/// bool zebraStripe,
/// InputBorder? focusedBorder,
/// ValueChanged<String>? onSubmitted,
/// ValueChanged<String> onChanged) {
/// return TextFormField(
/// textAlign: tdAlignment,
/// style: tdStyle,
/// initialValue: cellData.toString(),
/// onFieldSubmitted: onSubmitted,
/// onChanged: onChanged,
/// textAlignVertical: TextAlignVertical.center,
/// maxLines: tdEditableMaxLines,
/// decoration: InputDecoration(
/// filled: zebraStripe,
/// fillColor: rowNumber % 2 == 1.0 ? stripeColor2 : stripeColor1,
/// contentPadding: EdgeInsets.only(
/// left: tdPaddingLeft,
/// right: tdPaddingRight,
/// top: [tdPaddingTop],
/// bottom: tdPaddingBottom),
/// border: InputBorder.none,
/// focusedBorder: focusedBorder,
/// ),
/// );
/// }
final Function? cellEditorWidget;

@override
EditableState createState() => EditableState(
rows: this.rows,
Expand Down Expand Up @@ -391,6 +436,7 @@ class EditableState extends State<Editable> {
focusedBorder: widget.focusedBorder,
stripeColor1: widget.stripeColor1,
stripeColor2: widget.stripeColor2,
cellEditorWidget: getCellEditorWidgetCreator,
onChanged: (value) {
///checks if row has been edited previously
var result = editedRows.indexWhere((element) {
Expand Down Expand Up @@ -477,4 +523,97 @@ class EditableState extends State<Editable> {
),
);
}

Widget getCellEditorWidgetCreator(
cellData,
int rowNumber,
columnIndex,
TextAlign tdAlignment,
TextStyle? tdStyle,
double tdPaddingLeft,
double tdPaddingTop,
double tdPaddingBottom,
double tdPaddingRight,
int tdEditableMaxLines,
Color stripeColor1,
Color stripeColor2,
bool zebraStripe,
InputBorder? focusedBorder,
ValueChanged<String>? onSubmitted,
ValueChanged<String> onChanged) {
return widget.cellEditorWidget == null
? getCellEditorWidget(
cellData,
rowNumber,
columnIndex,
tdAlignment,
tdStyle,
tdPaddingLeft,
tdPaddingTop,
tdPaddingBottom,
tdPaddingRight,
tdEditableMaxLines,
stripeColor1,
stripeColor2,
zebraStripe,
focusedBorder,
onSubmitted,
onChanged)
: widget.cellEditorWidget!(
cellData,
rowNumber,
columnIndex,
tdAlignment,
tdStyle,
tdPaddingLeft,
tdPaddingTop,
tdPaddingBottom,
tdPaddingRight,
tdEditableMaxLines,
stripeColor1,
stripeColor2,
zebraStripe,
focusedBorder,
onSubmitted,
onChanged);
}

Widget getCellEditorWidget(
cellData,
int rowNumber,
columnIndex,
TextAlign tdAlignment,
TextStyle? tdStyle,
double tdPaddingLeft,
double tdPaddingTop,
double tdPaddingBottom,
double tdPaddingRight,
int tdEditableMaxLines,
Color stripeColor1,
Color stripeColor2,
bool zebraStripe,
InputBorder? focusedBorder,
ValueChanged<String>? onSubmitted,
ValueChanged<String> onChanged) {
return TextFormField(
textAlign: tdAlignment,
style: tdStyle,
initialValue: cellData.toString(),
onFieldSubmitted: onSubmitted,
onChanged: onChanged,
textAlignVertical: TextAlignVertical.center,
maxLines: tdEditableMaxLines,
decoration: InputDecoration(
filled: zebraStripe,
fillColor: rowNumber % 2 == 1.0 ? stripeColor2 : stripeColor1,
contentPadding: EdgeInsets.only(
left: tdPaddingLeft,
right: tdPaddingRight,
top: tdPaddingTop,
bottom: tdPaddingBottom),
border: InputBorder.none,
focusedBorder: focusedBorder,
),
);
}
}
41 changes: 19 additions & 22 deletions lib/widgets/table_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class RowBuilder extends StatefulWidget {
required this.stripeColor2,
required this.zebraStripe,
required this.focusedBorder,
required this.cellEditorWidget,
}) : _trHeight = trHeight,
_borderColor = borderColor,
_borderWidth = borderWidth,
Expand Down Expand Up @@ -53,6 +54,7 @@ class RowBuilder extends StatefulWidget {
final InputBorder? focusedBorder;
final ValueChanged<String>? onSubmitted;
final ValueChanged<String> onChanged;
final Function cellEditorWidget;

@override
_RowBuilderState createState() => _RowBuilderState();
Expand All @@ -77,28 +79,23 @@ class _RowBuilderState extends State<RowBuilder> {
border: Border.all(
color: widget._borderColor, width: widget._borderWidth)),
child: widget.isEditable
? TextFormField(
textAlign: widget.tdAlignment,
style: widget.tdStyle,
initialValue: widget.cellData.toString(),
onFieldSubmitted: widget.onSubmitted,
onChanged: widget.onChanged,
textAlignVertical: TextAlignVertical.center,
maxLines: widget.tdEditableMaxLines,
decoration: InputDecoration(
filled: widget.zebraStripe,
fillColor: widget.index % 2 == 1.0
? widget.stripeColor2
: widget.stripeColor1,
contentPadding: EdgeInsets.only(
left: widget.tdPaddingLeft,
right: widget.tdPaddingRight,
top: widget.tdPaddingTop,
bottom: widget.tdPaddingBottom),
border: InputBorder.none,
focusedBorder: widget.focusedBorder,
),
)
? widget.cellEditorWidget(
widget.cellData,
widget.index,
widget.col,
widget.tdAlignment,
widget.tdStyle,
widget.tdPaddingLeft,
widget.tdPaddingTop,
widget.tdPaddingBottom,
widget.tdPaddingRight,
widget.tdEditableMaxLines,
widget.stripeColor1,
widget.stripeColor2,
widget.zebraStripe,
widget.focusedBorder,
widget.onSubmitted,
widget.onChanged)
: Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(
Expand Down