mengle_forms 0.1.0 copy "mengle_forms: ^0.1.0" to clipboard
mengle_forms: ^0.1.0 copied to clipboard

A new form generator package for Flutter.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:forms/forms.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const MyHomePage(title: 'Forms Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<GroupedListSingleSelectItems> getList() {
    var groupedListItems = <GroupedListSingleSelectItems>[];
    var item = GroupedListSingleSelectItems(
      key: '1',
      label: 'title',
      description: 'desc',
      value: 1,
      icon: null,
      group: 'Existing List',
      isOther: false,
    );

    groupedListItems.add(item);

    var item2 = GroupedListSingleSelectItems(
      key: '2',
      label: 'Category Name',
      description: '',
      value: 2,
      icon: null,
      isOther: true,
      group: 'Existing List',
    );

    groupedListItems.add(item2);

    return groupedListItems;
  }

  Future<bool> _save(GlobalKey<FormState> form) async {
    if (inputValue.isEmpty) return false;
    return true;
  }

  dynamic response;
  bool? isTextInputValid;
  String inputValue = "Value 1";
  bool? isGroupedListValid;
  dynamic groupedListValue;
  bool switchValue = false;
  String email = '[email protected]';
  String? username;

  List<FormControls> formElements = [];
  List<FormControls> addFormElements() {
    formElements.clear();

    formElements.add(
      FormControls(
        FormControlTypes.password,
        ControlConfig(
          key: 'secretCode',
          label: 'Secret Code',
          placeholder: 'Enter that secret',
          maxLines: 1,
          isRequired: true,
          value: inputValue,
          isValid: isTextInputValid,
          alreadyExists: ["secret", "Value 1"],
          onChange: (ControlConfig config) => {
            setState(() => {
                  inputValue = config.value.toString().trim(),
                  isTextInputValid = config.isValid,
                })
          },
        ),
      ),
    );

    formElements.add(
      FormControls(
        FormControlTypes.textInput,
        ControlConfig(
          key: 'email',
          value: email,
          label: 'Email',
          isRequired: true,
          isValid: isEmailValid(email),
          inputType: TextInputType.emailAddress,
          placeholder: 'Enter the email Address',
          alreadyExists: ["[email protected]"],
          onChange: (ControlConfig config) => {
            setState(() => {
                  email = config.value,
                  config.isValid = isEmailValid(email),
                }),
          },
        ),
      ),
    );

    formElements.add(
      FormControls(
        FormControlTypes.textInput,
        ControlConfig(
          key: 'username',
          label: 'Name',
          placeholder: 'Enter your user name',
          isRequired: true,
          isValid: username != null,
          value: username,
          alreadyExists: ["user", "user 1"],
          onChange: (ControlConfig config) =>
              {setState(() => username = config.value.toString().trim())},
        ),
      ),
    );

    formElements.add(FormControls(
        FormControlTypes.groupedListSubForm,
        ControlConfig(
          showMainHeader: false,
          label: 'List',
          key: 'list',
          helpText: 'Select from the list',
          value: groupedListValue,
          isRequired: true,
          isValid: isGroupedListValid,
          isGrouped: true,
          isMultiselect: false,
          onChange: (config) {},
          onSave: (config) {
            groupedListValue = config.value;
            config.isValid = isGroupedListValid;
          },
          groupedListItems: getList(),
        )));

    formElements.add(
      FormControls(
        FormControlTypes.question,
        ControlConfig(
          label: 'Is this form amazing',
          hideModalTitle: false,
          isRequired: true,
          isValid: true,
          key: 'question1',
          placeholder: 'Please select Yes or Hell Yes.',
          icon: Icons.question_mark,
          questionConfig: QuestionConfig(
            value: null,
            message: 'It even has more information here, how amazing that is?',
            cancelButtonText: 'Hell Yes',
            continueButtonText: 'Yes',
            onConfirm: (String value) async => {},
          ),
        ),
      ),
    );

    formElements.add(
      FormControls(
        FormControlTypes.toggleSwitch,
        ControlConfig(
          label: 'Is it raining outside',
          key: 'raningkey',
          value: switchValue,
          onChange: (value) {
            switchValue = value;
          },
        ),
      ),
    );

    return formElements;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Forms(
                formElements: addFormElements(),
                onChanged: (dynamic response) {
                  this.response = response;
                },
                actionSave: (form) async => {await _save(form)},
              ),
            ],
          ),
        ),
      ),
    );
  }
}
2
likes
110
points
9
downloads

Publisher

verified publisherdaniscript.com

Weekly Downloads

A new form generator package for Flutter.

Homepage

Documentation

API reference

License

unknown (license)

Dependencies

flutter

More

Packages that depend on mengle_forms