How To Create Number Field With Dropdown Field In Flutter

admin_img Posted By Bajarangi soft , Posted On 29-10-2020

A dropdown button lets the user select from a number of items. The button shows the currently selected item as well as an arrow that opens a menu for selecting another item.The type T is the type of the value that each dropdown item represents. All the entries in a given menu must represent values with consistent types. Typically, an enum is used. Each DropdownMenuItem in items must be specialized with that same type argument.

How To Create Number Field With Dropdown Field In Flutter

Number Field And DropDown Field 
Step 1:  We cannot directly remove the time stamp from Number Field And DropDown Field  but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code

dependencies:
  flutter:
    sdk: flutter
  flutter_masked_text: ^0.8.0


Step 2: After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 

flutter pub get

Step 3: Open your project’s main.dart file and import material.dart and flutter_masked_text: ^0.8.0. dart package.
import 'package:flutter_masked_text/flutter_masked_text.dart';

Complete Code For Number Field And DropDown Field In Flutter
Main.dart
import 'package:flutter/material.dart';
import 'package:flutter_masked_text/flutter_masked_text.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
//  static final List<String> _dropdownItems = <String>['India', 'USA'];
  static List<CountryModel> _dropdownItems = new List();
  final formKey = new GlobalKey<FormState>();

  var controller = new MaskedTextController(mask: '(000) 000 0000');
  CountryModel _dropdownValue;
  String _errorText;
  TextEditingController phoneController = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      _dropdownItems.add(CountryModel(country: 'India', countryCode: '+91'));
      _dropdownItems.add(CountryModel(country: 'USA', countryCode: '+1'));
      _dropdownValue = _dropdownItems[0];
      phoneController.text = _dropdownValue.countryCode;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.amber,
        title: Text('Number Field '),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(15.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              _buildCountry(),
              SizedBox(height: 10),
              _buildPhonefiled(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildCountry() {
    return FormField(
      builder: (FormFieldState state) {
        return DropdownButtonHideUnderline(
          child: new  Container(
            child: FormField(
              builder: (FormFieldState state) {
                return DropdownButtonHideUnderline(
                  child: new Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(0.0),
                        child: new InputDecorator(
                          decoration: InputDecoration(
                            enabledBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.all(Radius.circular(12.0)),
                              borderSide: BorderSide(color: Colors.black26),
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.all(Radius.circular(12.0)),
                              borderSide: BorderSide(color: Colors.grey),
                            ),
                            contentPadding: EdgeInsets.all(7.0),
                            filled: false,
                            prefixIcon: Icon(Icons.location_on,size: 15, color: Colors.black45,),
                            hintText: 'Choose Country',
                            hintStyle: TextStyle(color: Colors.black45,fontSize: 12),
                            labelText:
                            _dropdownValue == null ? 'Where are you from' : '',
                            errorText: _errorText,
                          ),
                          isEmpty: _dropdownValue == null,
                          child: new DropdownButton<CountryModel>(
                            value: _dropdownValue,
                            isDense: true,
                            onChanged: (CountryModel newValue) {
                              print('value change');
                              print(newValue);
                              setState(() {
                                _dropdownValue = newValue;
                                phoneController.text = _dropdownValue.countryCode;
                              });
                            },
                            items: _dropdownItems.map((CountryModel value) {
                              return DropdownMenuItem<CountryModel>(
                                value: value,
                                child: Text(value.country,
                                    style: TextStyle(
                                        color: Colors.black45,
                                        fontSize: 12
                                    )),
                              );
                            }).toList(),
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        );
      },
    );
  }

  Widget _buildPhonefiled() {
    return Row(
      children: <Widget>[
        new Expanded(
          child: new TextFormField(
            controller: controller,
            keyboardType: TextInputType.number,
            decoration: InputDecoration(
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(12.0)),
                borderSide: BorderSide(color: Colors.black26),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(12.0)),
                borderSide: BorderSide(color: Colors.grey),
              ),
              contentPadding: EdgeInsets.all(15),

              filled: false,
              labelText: 'mobile',
              labelStyle: TextStyle(fontSize: 10,color: Colors.black45),
              hintText: "Mobile number",
              hintStyle: TextStyle(fontSize: 12,color: Colors.black45),
              prefixIcon: new Icon(Icons.mobile_screen_share),
            ),
            onSaved: (String value) {},
          ),
          flex: 5,
        ),
      ],
    );
  }
}

class CountryModel {
  String country = '';
  String countryCode = '';

  CountryModel({
    this.country,
    this.countryCode,
  });
}

Related Post