Flutter DropDown Widget Tutorial

import 'package:flutter/material.dart';

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

class MyDropDownWidget extends StatefulWidget {
  @override
  _MyDropDownWidgetState createState() => _MyDropDownWidgetState();
}

class _MyDropDownWidgetState extends State<MyDropDownWidget> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Body(),
    );
  }
}

class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  String defaultName;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Drop Down Tutorial"),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        padding: const EdgeInsets.all(10.0),
        margin: EdgeInsets.only(top: 10),
        child: Column(
          children: [
            defaultDropDownButton(),
            SizedBox(
              height: 10,
            ),
            formFieldDropDownButton(),
            SizedBox(
              height: 10,
            ),
            dropDownButtonHiddenUnderLine(),
          ],
        ),
      ),
    );
  }

  Widget defaultDropDownButton() {
    return DropdownButton<String>(
      value: defaultName,
      elevation: 5,
      isExpanded: true,
      style: TextStyle(color: Colors.blue),
      items: <String>[
        'India',
        'America',
        'Nepal',
        'Russia',
        'Germany',
      ].map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(value),
              //Icon(Icons.ac_unit),
            ],
          ),
        );
      }).toList(),
      hint: Text(
        "Please select country",
        style: TextStyle(
          color: Colors.blue,
          fontSize: 15,
        ),
      ),
      onChanged: (String value) {
        setState(() {
          defaultName = value;
        });
      },
    );
  }

  Widget formFieldDropDownButton() {
    return DropdownButtonFormField(
      items: <String>[
        'India',
        'America',
        'Nepal',
        'Russia',
        'Germany',
      ].map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(value),
              //Icon(Icons.ac_unit),
            ],
          ),
        );
      }).toList(),
      hint: Text("Select Country"),
      value: defaultName,
      onChanged: (value) {
        setState(() {
          defaultName = value;
        });
      },
      decoration: InputDecoration(
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(5)),
      ),
    );
  }

  Widget dropDownButtonHiddenUnderLine() {
    return DropdownButtonHideUnderline(
        child: DropdownButton(
      value: defaultName,
      isExpanded: true,
      items: <String>[
        'India',
        'America',
        'Nepal',
        'Russia',
        'Germany',
      ].map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(value),
              //Icon(Icons.ac_unit),
            ],
          ),
        );
      }).toList(),
      hint: Text(
        "Please select country",
        style: TextStyle(
          color: Colors.blue,
          fontSize: 15,
        ),
      ),
      onChanged: (String value) {
        setState(() {
          defaultName = value;
        });
      },
    ));
  }
}

  

No comments: