Git Product home page Git Product logo

cali0707 / radio_button_form_field Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 3.0 79 KB

A Flutter Package to create radio buttons as FormFields, each contained in a ListTile with the button as the Leading Widget.

License: BSD 3-Clause "New" or "Revised" License

Dart 93.65% Kotlin 1.43% Swift 4.50% Objective-C 0.42%
flutter flutter-form flutter-form-widgets flutter-formfield flutter-radio material-radio

radio_button_form_field's Introduction

radio_button_form_field

A Flutter Package to create radio buttons as FormFields, each contained in a ListTile with the button as the Leading Widget.

Usage

This widget should be used like any other FormField widget in a Form. This widget must have a MaterialWidget ancestor, such as a MaterialApp or Form widget. If it does not have a Form widget as an ancestor, it needs to be given a GlobalKey. The widget requires a List of the data you want built into the RadioButtonFormField. The key given to the value of each item should be passed to the value parameter, and the key given to the title should be passed to the display parameter. The widget will return a ListView containing an equal number of items as the List it was passed.

Example

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: ExampleForm(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class ExampleForm extends StatefulWidget {
  @override
  _ExampleFormState createState() => _ExampleFormState();
}

GlobalKey myFormKey = new GlobalKey();

class _ExampleFormState extends State<ExampleForm> {
  int myNumber;
  String myValue = 'No value saved yet.';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Radio Button Form Field Example'),
      ),
      body: SafeArea(
        child: Form(
          key: myFormKey,
          child: ExpansionTile(
            children: [
              RadioButtonFormField(
                toggleable: true,
                padding: EdgeInsets.all(8),
                context: context,
                value: 'value',
                display: 'display',
                data: data,
                onSaved: (value) {
                  setState(() {
                    myNumber = value;
                    myValue = value.toString();
                  });
                },
              ),
              FlatButton(
                onPressed: () {
                  _submitForm();
                },
                child: Text('Submit'),
              ),
              Text(myValue)
            ],
          ),
        ),
      ),
    );
  }

  void _submitForm() {
    final FormState formState = myFormKey.currentState;
    if (formState.validate()) {
      formState.save();
      print(myNumber);
    }
  }
}

final List<Map> data = [
  {'value': 1, 'display': 'One'},
  {'value': 2, 'display': 'Two'},
  {'value': 3, 'display': 'Three'}
];

Contributing

Contributions are welcome as pull requests to the github repo. Please open issues on the repo for feature requests or bug reports.

radio_button_form_field's People

Contributors

cali0707 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.