flutter_object_form_field 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

flutter_object_form_field #

Working with TextFormField is nice until you want to work with something other than String. For example, if you want invoke date timer picker, you have to create a focus node, listen for focus, show the dialog when needed. If you do not want to use some special object to store the selected value, you will end up with parsing the data before save just because TextFormField only works with String.

This library provides ObjectFormField<T> that works much better for these cases. You provide it with a function that turns your object into String and a picker async function that returns newly picked object (DateTime from date picker).

Usage #

To use this plugin, add flutter_object_form_field as a dependency in your pubspec.yaml file.

Examples #


//
// USAGE WITH CUSTOM OBJECT
//
ObjectFormField<Emotion>(
  objectToString: (emotion) => emotion?.name,
  pickValue: (oldEmotion) async {
    // returns an emotion object
    return await Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => EmotionsPage()));
  },
  decoration: InputDecoration(labelText: 'Emotion'),
  validator: (value) {
    if (value == null) {
      return 'You have to pick an emotion';

    if (value.name == 'Sadness') {
      return 'Show me your smile!';
    }
  },
),
//
// USAGE WITH DATE TIME
//
ObjectFormField<DateTime>(
  // can use some kind of formatter
  objectToString: (dateTime) => dateTime?.toString(),
  pickValue: (oldDate) async {
    return await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(1900),
      lastDate: DateTime(2100),
    );
  },
  decoration: InputDecoration(labelText: 'Date'),
  validator: (value) {
    if (value == null) {
      return 'You have to select date';
    }
  },
),

Contribution and Support #

Contributions are welcome!
If you want to contribute code please create a PR
If you find a bug or want a feature, please fill an issue

[1.0.1] - 01.11.2018 #

  • Minor bug fix

[1.0.0] - 01.11.2018 #

  • No changes

[0.0.1] - 01.11.2018 #

  • Initial implementation

example/lib/main.dart

import 'package:example/emotions_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_object_form_field/flutter_object_form_field.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(title: 'Demo'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<FormState> _formKey = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: Form(
            key: _formKey,
            child: ListView(
              children: <Widget>[
                //
                // USAGE WITH CUSTOM OBJECT
                //
                ObjectFormField<Emotion>(
                  objectToString: (emotion) => emotion?.name,
                  pickValue: (oldEmotion) async {
                    // returns an emotion object
                    return await Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => EmotionsPage()));
                  },
                  decoration: InputDecoration(labelText: 'Emotion'),
                  validator: (value) {
                    if (value == null) {
                      return 'You have to pick an emotion';
                    }

                    if (value.name == 'Sadness') {
                      return 'Show me your smile!';
                    }
                  },
                ),
                //
                // USAGE WITH DATE TIME
                //
                ObjectFormField<DateTime>(
                  // can use some kind of formatter
                  objectToString: (dateTime) => dateTime?.toString(),
                  pickValue: (oldDate) async {
                    return await showDatePicker(
                      context: context,
                      initialDate: DateTime.now(),
                      firstDate: DateTime(1900),
                      lastDate: DateTime(2100),
                    );
                  },
                  decoration: InputDecoration(labelText: 'Date'),
                  validator: (value) {
                    if (value == null) {
                      return 'You have to select date';
                    }

                  },
                ),
              ],
            )),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _validateForm,
        tooltip: 'Validate',
        child: Icon(Icons.done),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void _validateForm() {
    _formKey.currentState.validate();
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_object_form_field: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_object_form_field/flutter_object_form_field.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
46
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
57
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Fix lib/flutter_object_form_field.dart. (-1 points)

Analysis of lib/flutter_object_form_field.dart reported 2 hints:

line 9 col 9: The class 'Future' wasn't exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

line 49 col 20: This function has a return type of 'Widget', but doesn't end with a return statement.

Maintenance suggestions

Package is getting outdated. (-43.29 points)

The package was last published 74 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test