Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ utilised by the flutter version_widget package.

## 0.1 Stable beta release

+ Add the ability to pass a custom onSubmit function [0.0.8 20251216 anushkavid]
+ Fix new flutter RadioGroup conflict [0.0.7 20250825 tonypioneer]
+ Lint cleanup [0.0.6 20250805 tonypioneer]
+ Use interfaces to pass parameters to the package by default. [0.0.5 20250409 tonypioneer]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ For example, the following snippet from the markdown file:

`%% Button-Begin(label,type,path)` and `%% Button-End` will be recognised as a
button. The parameter `label` is the text displayed on the button, `type` is
the type of the button (0 - Save to JSON file (default), 1 - Submit to URL),
and `path` is the path to redirect to or save to when the button is clicked. The
default file name is `result.json`. The button will be displayed on the
`Survey Details` page.
the type of the button (0 - Save to JSON file (default), 1 - Submit to URL,
2 - Run the custom function set by user), and `path` is the path to redirect
to or save to when the button is clicked. The default file name is
`result.json`. The button will be displayed on the `Survey Details` page.

Between the `Button-Begin` and `Button-End` tags is a list of widget IDs
containing all required fields. When the button is clicked, the widget will
Expand Down
4 changes: 2 additions & 2 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:example/main.dart';
// import 'package:example/main.dart';

void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// await tester.pumpWidget(const MyApp());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/utils/command_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CommandParser {
final String content;
final String fullContent;
final void Function(String title, String content)? onMenuItemSelected;
final Function? onSubmit;
final Map<String, dynamic> state;
final VoidCallback setStateCallback;
final String surveyTitle;
Expand Down Expand Up @@ -91,6 +92,7 @@ class CommandParser {
/// - [fullContent]: The full original content, used when menu blocks
/// reference the whole.
/// - [onMenuItemSelected]: A callback when a menu item is selected.
/// - [onSubmit]: A custom function to be called when save button is pressed.
/// - [state]: A shared state map holding data for all widgets.
/// - [setStateCallback]: A callback to update the state.
/// - [surveyTitle]: Title of the survey or form.
Expand All @@ -102,6 +104,7 @@ class CommandParser {
required this.content,
required this.fullContent,
this.onMenuItemSelected,
this.onSubmit,
required this.state,
required this.setStateCallback,
required this.surveyTitle,
Expand Down Expand Up @@ -548,6 +551,7 @@ class CommandParser {
requiredWidgets: [],
state: state,
surveyTitle: surveyTitle,
onSubmit: onSubmit,
),
);
} else if (command
Expand Down Expand Up @@ -576,6 +580,7 @@ class CommandParser {
requiredWidgets: requiredWidgets,
state: state,
surveyTitle: surveyTitle,
onSubmit: onSubmit,
),
);
} else if (command
Expand All @@ -600,6 +605,7 @@ class CommandParser {
content: hiddenContent,
fullContent: fullContent,
onMenuItemSelected: onMenuItemSelected,
onSubmit: onSubmit,
state: state,
setStateCallback: setStateCallback,
surveyTitle: surveyTitle,
Expand Down
6 changes: 6 additions & 0 deletions lib/src/widgets/button_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ class ButtonWidget extends StatefulWidget {
final List<String> requiredWidgets;
final Map<String, dynamic> state;
final String surveyTitle;
final Function? onSubmit;

const ButtonWidget({
super.key,
required this.command,
required this.requiredWidgets,
required this.state,
required this.surveyTitle,
this.onSubmit,
});

@override
Expand Down Expand Up @@ -301,6 +303,10 @@ class _ButtonWidgetState extends State<ButtonWidget> {
// Submit the data to a URL.

await _submitDataToUrl(data);
} else if (actionType == 2) {
// Call the custom onSubmit function

widget.onSubmit!(data);
} else {
// Invalid action type.

Expand Down
12 changes: 12 additions & 0 deletions lib/src/widgets/markdown_widget_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,23 @@ class MarkdownWidgetBuilder extends StatefulWidget {

final void Function(String title, String content)? onMenuItemSelected;

/// A custom function to be called when submit/save button is pressed
/// Note that the package assumes this function has one input argument
/// to pass the response Map. An example function is below
/// ignore: unintended_html_in_doc_comment
/// void onSubmit (Map<String, dynamic> responseMap) {
/// print (responseMap.toString());
/// }

final Function? onSubmit;

const MarkdownWidgetBuilder({
super.key,
required this.content,
required this.title,
this.submitUrl,
this.onMenuItemSelected,
this.onSubmit,
});

@override
Expand Down Expand Up @@ -141,6 +152,7 @@ class _MarkdownWidgetBuilderState extends State<MarkdownWidgetBuilder> {
content: widget.content,
fullContent: widget.content,
onMenuItemSelected: widget.onMenuItemSelected,
onSubmit: widget.onSubmit,
state: {
'_inputValues': _inputValues,
'_sliderValues': _sliderValues,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: markdown_widget_builder
description: "Widget builder for surveys defined using markdown-like syntax."
version: 0.0.7
version: 0.0.8
homepage: https://github.com/anusii/markdown_widget_builder

environment:
Expand Down