Skip to content

Commit

Permalink
Merge pull request #35 from playmoweb/feature/button-multitask
Browse files Browse the repository at this point in the history
✨ (additionalButton) : Add possiblity to add custom buttons
  • Loading branch information
clemenceroumy authored Mar 24, 2023
2 parents e761ee5 + 13a4d23 commit 9fc3d85
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 67 deletions.
10 changes: 10 additions & 0 deletions lib/format_markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ enum MarkdownType {
image,
}
class ActionButton {
final Widget widget;
final String title;
final Function() action;
final List<ActionButton> children;
final Key key;
ActionButton(this.key, {required this.widget, required this.action, this.children = const [], this.title = ""});
}
/// Add data to [MarkdownType] enum
extension MarkownTypeExtension on MarkdownType {
/// Get String used in widget's key
Expand Down
158 changes: 91 additions & 67 deletions lib/markdown_text_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MarkdownTextInput extends StatefulWidget {
/// List of action the component can handle
final List<MarkdownType> actions;

final List<ActionButton> optionnalActionButtons;

/// Optional controller to manage the input
final TextEditingController? controller;

Expand Down Expand Up @@ -77,7 +79,8 @@ class MarkdownTextInput extends StatefulWidget {
this.imageDialogLinkDecoration,
this.imageDialogTextDecoration,
this.customCancelDialogText,
this.customSubmitDialogText});
this.customSubmitDialogText,
this.optionnalActionButtons = const []});

@override
_MarkdownTextInputState createState() => _MarkdownTextInputState(controller ?? TextEditingController());
Expand Down Expand Up @@ -160,72 +163,78 @@ class _MarkdownTextInputState extends State<MarkdownTextInput> {
borderRadius: const BorderRadius.only(bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10)),
child: ListView(
scrollDirection: Axis.horizontal,
children: widget.actions.map((type) {
if (type == MarkdownType.title) {
return ExpandableNotifier(
child: Expandable(
key: Key('H#_button'),
collapsed: ExpandableButton(
child: const Center(
child: Padding(
padding: EdgeInsets.all(10),
child: Text(
'H#',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
children: <Widget>[
...widget.actions.map((type) {
if (type == MarkdownType.title) {
return ExpandableNotifier(
child: Expandable(
key: Key('H#_button'),
collapsed: ExpandableButton(
child: const Center(
child: Padding(
padding: EdgeInsets.all(10),
child: Text(
'H#',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
),
),
),
),
),
expanded: Container(
color: Colors.white10,
child: Row(
children: [
for (int i = 1; i <= 6; i++)
InkWell(
key: Key('H${i}_button'),
onTap: () => onTap(MarkdownType.title, titleSize: i),
child: Padding(
padding: const EdgeInsets.all(10),
child: Text(
'H$i',
style: TextStyle(fontSize: (18 - i).toDouble(), fontWeight: FontWeight.w700),
expanded: Container(
color: Colors.white10,
child: Row(
children: [
for (int i = 1; i <= 6; i++)
InkWell(
key: Key('H${i}_button'),
onTap: () => onTap(MarkdownType.title, titleSize: i),
child: Padding(
padding: const EdgeInsets.all(10),
child: Text(
'H$i',
style: TextStyle(fontSize: (18 - i).toDouble(), fontWeight: FontWeight.w700),
),
),
),
),
ExpandableButton(
child: const Padding(
padding: EdgeInsets.all(10),
child: Icon(
Icons.close,
ExpandableButton(
child: const Padding(
padding: EdgeInsets.all(10),
child: Icon(
Icons.close,
),
),
),
),
],
],
),
),
),
),
);
}
else if (type == MarkdownType.link || type == MarkdownType.image) {
return _basicInkwell(
type,
customOnTap: (type == MarkdownType.link ? !widget.insertLinksByDialog : !widget.insertImageByDialog)
? null
: () async {
var text = _controller.text.substring(textSelection.baseOffset, textSelection.extentOffset);

var textController = TextEditingController()..text = text;
var linkController = TextEditingController();

var color = Theme.of(context).colorScheme.secondary;

await _basicDialog(textController, linkController, color, text, type);
},
);
} else {
return _basicInkwell(type);
}
}).toList(),
);
} else if (type == MarkdownType.link || type == MarkdownType.image) {
return _basicInkwell(
type,
customOnTap: (type == MarkdownType.link ? !widget.insertLinksByDialog : !widget.insertImageByDialog)
? null
: () async {
var text = _controller.text.substring(textSelection.baseOffset, textSelection.extentOffset);

var textController = TextEditingController()..text = text;
var linkController = TextEditingController();

var color = Theme.of(context).colorScheme.secondary;

await _basicDialog(textController, linkController, color, text, type);
},
);
} else {
return _basicInkwell(type);
}
}).toList(),


...widget.optionnalActionButtons.map((ActionButton optionActionButton) {
return _basicInkwell(optionActionButton, customOnTap: optionActionButton.action);
}).toList()
],
),
),
)
Expand All @@ -234,15 +243,30 @@ class _MarkdownTextInputState extends State<MarkdownTextInput> {
);
}

Widget _basicInkwell(MarkdownType type, {Function? customOnTap}) {
return InkWell(
key: Key(type.key),
onTap: () => customOnTap != null ? customOnTap() : onTap(type),
child: Padding(
padding: EdgeInsets.all(10),
child: Icon(type.icon),
),
);
Widget _basicInkwell(dynamic item, {Function? customOnTap}) {
Widget widgetToReturn = SizedBox.shrink();

if (item is MarkdownType) {
return InkWell(
key: Key(item.key),
onTap: () => customOnTap != null ? customOnTap() : onTap(item),
child: Padding(
padding: EdgeInsets.all(10),
child: Icon(item.icon),
),
);
} else if (item is ActionButton) {
return InkWell(
key: item.key,
onTap: item.action,
child: Padding(
padding: EdgeInsets.all(10),
child: item.widget,
),
);
}

return widgetToReturn;
}

Future<void> _basicDialog(
Expand Down

0 comments on commit 9fc3d85

Please sign in to comment.