How To Implement Flutter Dropdown OnChange

png 20220702 133201 0000

Flutter dropdown onChange implementation. In this post, I will be implementing Flutter dropdown onChange with a proper Flutter code example. I will be explaining everything about Flutter dropdown onChange step by step so you don’t have any queries left when you finish reading this post. So let’s not waste anymore time and start implementing Flutter dropdown onChange in Flutter dropdown button.

What is Flutter Dropdown OnChange?

Flutter dropdown onChange is used to select an option from the list of dropdown items and show it in the Flutter dropdown button. Don’t worry in the next section, I will implement Flutter dropdown onChange using an easy Flutter example so you can have a better idea.

Implementing Flutter Dropdown OnChange(All Steps)

To implement Flutter dropdown onChange, we have to use the on changed constructor of the Flutter dropdown button widget. See the below code:

Step 1
DropdownMenuItem(
                 value: 'Sports',
                    child: Text('Sports',
                        style: TextStyle(fontSize: 15, color: Colors.black45))),

The value constructor of the drop down menu item is like an id of that item. This is the value that is being fetched when we select any item from the dropdown list.

Step 2
String? categoryValue;

In step 1, we have to define a string variable so we can store the value of selected item in it.

Step 3
 onChanged: (val) {
                setState(() {
                  categoryValue = val.toString();
                });
              }
In step 2, we have to use the on changed constructor which has a object parameter which have the selected item value so in this we set that value to the string variable we defined above. Use the setstate to update the UI and also use the toString with the value object to convert it to string.
Step 4
value: categoryValue,

Now we have to pass the string variable to the value constructor. By using this, the selected value will be shown in the Flutter dropdown button.

 These are the steps that you should follow to properly use the Flutter dropdown onChange in the Flutter dropdown button widget. Now you have a complete understanding of how to use Flutter dropdown onChange. If you still have any queries then comment them down. I would love to clear all your doubts regarding Flutter dropdown onChange implementation.
Now as a treat, I have prepared a beautiful custom Flutter dropdown button design for you in which Flutter dropdown onChange is also implemented. Hope you will like it. The complete source code is available in the next section.

Custom Flutter Dropdown Button Design Source Code

Flutter dropdown onChange

Flutter dropdown onChange

import 'package:flutter/material.dart';
void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Homepage(),
    );
  }
}
class Homepage extends StatefulWidget {
  @override
  State<Homepage> createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
  String? categoryValue;
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
            body: Center(
                child: Padding(
      padding: EdgeInsets.all(20),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(50).copyWith(
                bottomRight: Radius.circular(0), topLeft: Radius.circular(0)),
            color: Colors.green.shade100),
        child: DropdownButtonHideUnderline(
          child: DropdownButton(
              iconSize: 30,
              borderRadius: BorderRadius.circular(20)
                  .copyWith(topLeft: Radius.circular(0)),
              isExpanded: true,
              hint: Text('Select any item', style: TextStyle(fontSize: 15)),
              value: categoryValue,
              items: [
                DropdownMenuItem(
                    value: 'Sports',
                    child: Text('Sports',
                        style: TextStyle(fontSize: 15, color: Colors.black45))),
                DropdownMenuItem(
                    value: 'Tech',
                    child: Text('Tech',
                        style: TextStyle(fontSize: 15, color: Colors.black45))),
              ],
              onChanged: (val) {
                setState(() {
                  categoryValue = val.toString();
                });
              }),
        ),
      ),
    ))));
  }
}

Conclusion

In conclusion, now you have a practical understanding of how Flutter dropdown onChange works. I would love to hear your feedback regarding this post. I would also want you to visit my other posts on Flutter app development, Flutter widgets and many more. That’s all for this post. Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *