Last updated:
0 purchases
moform
Moform #
Reactive, model-driven, and type-safe forms for Flutter without the overhead of managing a TextEditingController.
Getting Started #
➤ Installation #
Add the following to your pubspec.yaml:
dependencies:
flutter_localizations:
sdk: flutter
moform: <version>
copied to clipboard
➤ Usage #
In this example, we are using a StringField to manage the value of an email field.
Using a StatefulWidget:
class EmailField extends StatefulWidget {
@override
State<EmailField> createState() => _EmailFieldState();
}
class _EmailFieldState extends State<EmailField> {
String _email = '';
@override
Widget build(BuildContext context) {
return StringField(
value: _email,
onChanged: (value) {
setState(() => _email = value);
},
);
}
}
copied to clipboard
Using Riverpod:
final emailProvider = StateProvider<String>((ref) => '');
class EmailField extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final email = ref.watch(emailProvider);
return StringField(
value: email,
onChanged: (value) {
ref.read(emailProvider.notifier).state = value;
},
);
}
}
copied to clipboard
Features #
➤ Typed Fields #
String
StringField(
value: email,
onChanged: (value) {
setState(() => email = value);
},
);
copied to clipboard
Int
IntField(
value: age,
onChanged: (value) {
setState(() => age = value);
},
);
copied to clipboard
Double
DoubleField(
value: weight,
onChanged: (value) {
setState(() => weight = value);
},
);
copied to clipboard
DateTime
DateTimeField(
value: date,
onChanged: (value) {
setState(() => date = value);
},
);
copied to clipboard
DateTime (Date only)
DateField(
value: date,
onChanged: (value) {
setState(() => date = value);
},
);
copied to clipboard
TimeOfDay
TimeField(
value: time,
onChanged: (value) {
setState(() => time = value);
},
);
copied to clipboard
➤ Nullable Fields #
There are nullable versions of the fields above:
OptionalStringField, OptionalIntField, OptionalDoubleField.
These fields have onChanged callbacks that receive String?, int?, and double? respectively.
OptionalStringField(
value: email,
onChanged: (String? value) { // <-- null, when the field is empty
setState(() => email = value);
},
);
copied to clipboard
To clear a DateTimeField, DateField, or TimeField, add the onCleared callback:
DateTimeField(
value: date,
onChanged: (value) {
setState(() => date = value);
},
onCleared: () {
setState(() => date = null);
},
);
copied to clipboard
➤ Custom Styles #
Provide a custom widget with the builder parameter.
Be aware that parameters like label are ignored when using a custom widget.
StringField(
value: email,
onChanged: (value) {
setState(() {
email = value;
});
},
builder: (context, controller) {
return TextField(
controller: controller,
decoration: const InputDecoration(
labelText: 'Custom Field',
),
);
},
);
copied to clipboard
➤ Custom NumberFormat #
Available in IntField, DoubleField, and their nullable versions.
IntField(
value: age,
numberFormat: NumberFormat.decimalPattern(),
onChanged: (value) {
setState(() => age = value);
},
);
copied to clipboard
For more complex scenarios, you can provide a customNumberFormat.
Be aware, that the parser should be the exact inverse of the formatter.
IntField(
value: age,
customNumberFormat: CustomNumberFormat(
formatter: (i) => i.toString(),
parser: (s) => int.tryParse(s),
),
onChanged: (value) {
setState(() => age = value);
},
);
copied to clipboard
➤ Clear Button #
There is a convenience onCleared callback to clear the field.
If this callback is provided, a clear button will be displayed.
This parameter is ignored when decoration or builder is provided.
StringField(
value: email,
onChanged: (value) {
setState(() => email = value);
},
onCleared: () {
setState(() => email = '');
},
);
copied to clipboard
License #
MIT License
Copyright (c) 2024 Tien Do Nam
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.