flutter - How to show vertical ToggleButtons

Flutter - ToggleButtons Vertical
The ToggleButtons class represents a set of toggle buttons. The ToggleButton’s children are laid out along direction. The state of each button in a ToggleButton widget is controlled by the isSelected property, which is a list of bools that determine if a button is in an unselected or selected state. The flutter developers can configure the ToggleButtons widget’s each toggle behavior by the onPressed callback, which can update the isSelected list however it wants to. The ToggleButtons widget has a solid, 1 logical pixel border surrounding itself by default and separating each button.

The following flutter app development tutorial will demonstrate how to display ToggleButtons vertically aligned. Here we used the ToggleButtons widget’s direction property to lay out buttons in the vertical direction.

The ToggleButtons class’s direction property value is an Axis enum value which is the direction along which the buttons are rendered. The default value of the ToggleButtons class’s direction property is Axis.horizontal. So by default, the ToggleButtons widget’s buttons are aligned in the horizontal direction.

The Axis enum represents the two cardinal directions in two dimensions. The axis is always relative to the current coordinate space. The Axis.horizontal value determines the left and right direction. And the Axis.vertical value determines the up and down direction.

In this flutter tutorial, we will display the ToggleButtons widget’s buttons in the vertical direction. We can simply do it by using the ToggleButtons class’s direction property. We just have to set this property value to Axis.vertical.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample> {
final List<bool> _selections = List.generate(3, (index) => false);

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFEDEAE0),
appBar: AppBar(
title: const Text("Flutter - ToggleButtons Vertical")
),
body: bodyContent()
);
}


bodyContent() {
return Center(
child: ToggleButtons(
children: const [
Icon(Icons.share),
Icon(Icons.add_location),
Icon(Icons.bookmark),
],
onPressed: (int index){
setState(() {
_selections[index] = !_selections[index];
});
},
isSelected: _selections,
constraints: const BoxConstraints(
minWidth: 70,
minHeight: 70
),
borderRadius: BorderRadius.circular(20),
borderWidth: 3,
borderColor: Colors.black,
selectedBorderColor: const Color(0xFF4B3621),
fillColor: const Color(0xFFFF0800),
selectedColor: Colors.white,
direction: Axis.vertical,
verticalDirection: VerticalDirection.down
)
);
}
}

flutter - How to use ToggleButtons onPressed

Flutter - ToggleButtons onPressed
The ToggleButtons class represents a set of toggle buttons. The ToggleButton’s children are laid out along direction. The state of each button in a ToggleButton widget is controlled by the isSelected property, which is a list of bools that determine if a button is in an unselected or selected state. The ToggleButton widget has a solid, 1 logical pixel border surrounding itself by default and separating each button. The flutter app developers can completely remove the ToggleButtons border by setting the renderBorder property value to false.

The following flutter app development tutorial will demonstrate how to set the pressed callback for the ToggleButtons widget. Here we used the ToggleButtons widget’s onPressed property to set the pressed callback for the ToggleButtons widget’s buttons.

The flutter developers can configure the ToggleButtons widget’s each toggle behavior by the onPressed callback, which can update the isSelected list however it wants to.

The ToggleButtons class’s onPressed property set the callback that is called when a button is tapped. The onPressed property value is a Void which is the callback that is called when a button is tapped. The index parameter of the callback is the index of the button that is tapped or otherwise activated. When the ToggleButton’s onPressed callback is null, all toggle buttons will be disabled.

In this flutter example code, we determine the user-pressed button from the ToggleButton widget and display the specified button’s selected and unselected state in a message on SnackBar. We also update the specified button’s selected state in this scope.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample> {
final List<bool> _selections = List.generate(3, (index) => false);

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFEDEAE0),
appBar: AppBar(
title: const Text("Flutter - ToggleButtons onPressed")
),
body: bodyContent()
);
}


bodyContent() {
return Center(
child: ToggleButtons(
children: const [
Icon(Icons.share),
Icon(Icons.add_location),
Icon(Icons.bookmark),
],
onPressed: (int index){
setState(() {
_selections[index] = !_selections[index];
if(index == 0){
if(_selections[index]){
showSnackBar("Share checked");
}else{
showSnackBar("Share unchecked");
}
}else if(index == 1){
if(_selections[index]){
showSnackBar("Location checked");
}else{
showSnackBar("Location unchecked");
}
}else if(index == 2){
if(_selections[index]){
showSnackBar("Bookmark checked");
}else{
showSnackBar("Bookmark unchecked");
}
}
});
},
isSelected: _selections,
constraints: const BoxConstraints(
minWidth: 75,
minHeight: 75
),
borderRadius: BorderRadius.circular(20),
borderWidth: 4,
fillColor: Colors.pink,
selectedColor: Colors.white,
)
);
}


showSnackBar(String message){
SnackBar snackBar = SnackBar(
content: Text(message)
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}

flutter - How to create rounded corners ToggleButtons

Flutter - ToggleButtons Rounded Corners
The ToggleButtons class represents a set of toggle buttons. The ToggleButton’s children are laid out along direction. The state of each button in a ToggleButton widget is controlled by the isSelected property, which is a list of bools that determine if a button is in an unselected or selected state. The flutter developers can configure the ToggleButtons widget’s each toggle behavior by the onPressed callback, which can update the isSelected list however it wants to. The ToggleButtons widget has a solid, 1 logical pixel border surrounding itself by default and separating each button.

The following flutter app development tutorial will demonstrate how to create and show rounded corners ToggleButtons widget. Here we used the ToggleButtons widget’s borderRadius property to display the rounded corners ToggleButtons widget in a flutter app.

In this flutter example code, we will also specify the ToggleButtos widget’s enabled border color, disabled border color, selected border color, and border thickness by using its corresponding properties.

The ToggleButtons class’s borderRadius property value is a BorderRadius instance that specifies the radii of the border's corners.

The BorderRadius class represents an immutable set of radii for each corner of a rectangle. The BorderRadius class specifies offsets in terms of visual corners, e.g. topLeft.

The BorderRadius.circular(double radius) constructor creates a border radius where all radii are Radius.circular(radius). Here we used this constructor to create a rounded corners border for the ToggleButtons widget.

The ToggleButtons class’s borderWidth property determines the width of the border surrounding each toggle button. The borderColor property specifies the border color to display when the toggle button is enabled and not selected.

The disabledBorderColor property determines the border color to display when the toggle button is disabled. And the selectedBorderColor property helps us to set the border color to display when the toggle button is selected.

So finally, the flutter app developers can create a rounded corners ToggleButtons widget by using its borderRadius property. And they also can customize the border appearance by other border-related properties.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample> {
final List<bool> _selections = List.generate(4, (index) => false);

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFEDEAE0),
appBar: AppBar(
title: const Text("Flutter - ToggleButtons Rounded")
),
body: bodyContent()
);
}


bodyContent() {
return Center(
child: ToggleButtons(
children: const [
Icon(Icons.share),
Icon(Icons.add_location),
Icon(Icons.bookmark),
Icon(Icons.repeat)
],
onPressed: (int index){
setState(() {
_selections[index] = !_selections[index];
});
},
isSelected: _selections,
constraints: const BoxConstraints(
minWidth: 75,
minHeight: 75
),
borderRadius: BorderRadius.circular(20),
borderWidth: 4,
borderColor: Colors.black87,
selectedBorderColor: Colors.red,
disabledBorderColor: Colors.blueGrey
)
);
}
}

flutter - How to change ToggleButtons color

Flutter - ToggleButtons Color
The ToggleButtons class represents a set of toggle buttons. The ToggleButton’s children are laid out along direction. The state of each button in a ToggleButton widget is controlled by the isSelected property, which is a list of bools that determine if a button is in an unselected or selected state. The flutter developers can configure the ToggleButtons widget’s each toggle behavior by the onPressed callback, which can update the isSelected list however it wants to. The ToggleButtons widget has a solid, 1 logical pixel border surrounding itself by default and separating each button.

The following flutter app development tutorial will demonstrate how to set or change the ToggleButtons widget’s various colors such as text and icon color, selected toggle buttons fill color, splash color, selected toggle buttons text and icon color, enabled border color, disabled border color, and selected border color.

The ToggleButtons class’s color property value is a Color object that is the color for descendant Text and Icon widgets if the button is enabled and not selected. The selectedColor property determines the color for descendant Text and Icon widgets if the button is selected.

The fillColor property value specifies The fill color for selected toggle buttons. And the splashColor property value determines the splash color for the button's InkWell.

The ToggleButtons class’s borderColor property specifies the border color to display when the toggle button is enabled and not selected. The disabledBorderColor property determines the border color to display when the toggle button is disabled. And the selectedBorderColor property helps us to set the border color to display when the toggle button is selected.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample> {
final List<bool> _selections = List.generate(4, (index) => false);

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFEDEAE0),
appBar: AppBar(
title: const Text("Flutter - ToggleButtons Color")
),
body: bodyContent()
);
}


bodyContent() {
return Center(
child: ToggleButtons(
children: const [
Icon(Icons.share),
Icon(Icons.add_location),
Icon(Icons.bookmark),
Icon(Icons.repeat)
],
onPressed: (int index){
setState(() {
_selections[index] = !_selections[index];
});
},
isSelected: _selections,
constraints: const BoxConstraints(
minWidth: 80,
minHeight: 80
),
color: Colors.pink,
selectedColor: Colors.green,
fillColor: Colors.yellow,
disabledColor: Colors.blueGrey,
borderColor: Colors.black87,
selectedBorderColor: Colors.blue,
splashColor: Colors.orange,
)
);
}
}

flutter - How to use ToggleButtons

Flutter ToggleButtons
The ToggleButtons class represents a set of toggle buttons. The ToggleButton’s children are laid out along direction. The state of each button in a ToggleButton widget is controlled by the isSelected property, which is a list of bools that determine if a button is in an unselected or selected state. They are both correlated by their index in the list. The length of isSelected has to match the length of the ToggleButton’s children list.

The flutter developers can configure the ToggleButtons widget’s each toggle behavior by the onPressed callback, which can update the isSelected list however it wants to.

The ToggleButtons widget has a solid, 1 logical pixel border surrounding itself by default and separating each button. But the flutter developers can configure the ToggleButton’s borders' color, width, and corner radii.

The ToggleButtons class selectedBorderColor property value determines the border's color when the button is selected, while the disabledBorderColor property value determines the border's color when the button is disabled. The borderColor property value is used when the button is enabled.

The flutter app developers can completely remove the ToggleButton border by setting the renderBorder property value to false. When they set the borderWidth property value to 0.0 results in a hairline border.

The ToggleButton class has various properties to configure the button such as direction, children, borderColor, borderWidth, borderRadius, color, constraints, fillColor, focusColor, highlightColor, hoverColor, isSelected, onPressed, renderBorder, selectedColor, splashColor, tapTargetSize, and textStyle.

The ToggleButton class also has many useful methods as example build(), createLement(), toString(), toStringDeep(), toDiagnosticsNode(), noSuchMethod(), and toStringShort().
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.pink),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample> {
final List<bool> _selections = List.generate(4, (index) => false);

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFEDEAE0),
appBar: AppBar(
title: const Text("Flutter - ToggleButtons Example")
),
body: bodyContent()
);
}


bodyContent() {
return Center(
child: ToggleButtons(
children: const [
Icon(Icons.share),
Icon(Icons.add_location),
Icon(Icons.bookmark),
Icon(Icons.repeat)
],
onPressed: (int index){
setState(() {
_selections[index] = !_selections[index];
});
},
isSelected: _selections
),
);
}
}

flutter - How to change DropdownButton height

Flutter - DropdownButton Height
The DropdownButton class represents a material design button for selecting from a list of items. The DropdownButton allows the user to select from a number of items. The DropdownButton shows the currently selected item as well as an arrow that opens a menu for selecting another item. The DropdownButton’s all entries in a menu must represent values with consistent types. Commonly, the flutter developers use an enum. The DropdownButton class’s onChanged callback should update a state variable that defines the dropdown's value. When the flutter developers set the onChanged callback to null or the list of items to null then the dropdown button will be disabled.

The following flutter app development tutorial will demonstrate how to set or change the height of a DropdownButton widget. But there is no built-in property or method in DropdownButton class to set or change its height. There is a simple trick, flutter developers can wrap a DropdownButton widget with another widget and set the parent widget height to define the child DopdownButton height.

Flutter developers can wrap the DropdownButton widget with a SizedBox or a Container to specify the child DropdownButton widget’s width and height. The SizedBox class represents a box with a specified size. The SizedBox widget forces its child widget to have a specific width and/or height. If either the width or height is null, the SizedBox widget will try to size itself to match the child's size in that dimension. So we can set the SizedBox height to change the DropdownButton height. And we don’t set the width of SizedBox so that the DropdownButton width remains unchanged.

Here we wrapped the DropdownButton widget with a Container widget to specify the child DropdownButton widget’s height. When we set the Container widget’s height then it will also define the DropdownButton widget’s height.

The flutter Container class represents a Container widget which is a convenience widget that combines common painting, positioning, and sizing widgets. The Container class’s height property value is a double instance that specifies the height of the Container widget itself and its child widget.

In this example code, we also add a border to the Container widget so the border seems to draw around the DropdownButton widget. This border helps us to clearly visible the DropdownButton size (height and width). So when we set the DropdownButton new height then it will help us to compare it with the original size of the DropdownButton widget.

So finally, the flutter app developers can set or change the height of a DropdownButton widget by wrapping it with a Container widget or a SizedBox widget and setting its height property value to specify the DropdownButton widget height.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.red),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample>{
String dropdownValue = "Carmine";

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - DropdownButton Height")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: Container(
height: 30,
padding: const EdgeInsets.symmetric(horizontal: 12.0),
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
width: 1,
style: BorderStyle.solid
),
borderRadius: BorderRadius.circular(8)
),
child: DropdownButton<String>(
value: dropdownValue,
items: <String>[
'Carmine','Candy apple red','Cameo pink','Aqua'
]
.map<DropdownMenuItem<String>>((String value){
return DropdownMenuItem<String>(
value: value,
child:Text(value)
);
}).toList(),
onChanged: (String? newValue){
// do something here
setState(() {
dropdownValue = newValue??dropdownValue;
});
},
underline: DropdownButtonHideUnderline(child: Container())
)
)
);
}
}

flutter - How to add a border to DropdownButton

Flutter - DropdownButton Border
The DropdownButton class represents a material design button for selecting from a list of items. The DropdownButton allows the user to select from a number of items. The DropdownButton shows the currently selected item as well as an arrow that opens a menu for selecting another item. The DropdownButton’s all entries in a menu must represent values with consistent types. Commonly, the flutter developers use an enum. The DropdownButton class’s onChanged callback should update a state variable that defines the dropdown's value. When the flutter developers set the onChanged callback to null or the list of items to null then the dropdown button will be disabled.

The following flutter app development tutorial will demonstrate how to add a border to a DropdownButton widget. Here we will add a rounded corners border around the DropdownButton widget. But there is no built-in property or method in DropdownButton class to add a border to the DropdownButton widget.

So we have to apply a simple trick to add a border around the DropdownButton widget. Here we wrapped the DropdownButton widget with a Container widget to draw a border of a DropdownButton widget instance. When we set the Container widget’s border then it will display a border around the DropdownButton widget.

The flutter Container class represents a Container widget which is a convenience widget that combines common painting, positioning, and sizing widgets. The Container class decoration property specifies the decoration to paint behind the child.

The BoxDecoration class represents an immutable description of how to paint a box. The BoxDecoration class provides a variety of ways to draw a box. The box has a border. So we can draw a border around a Container widget by the BoxDecoration.

The BoxDecoration class border property allows us to draw a border to draw above the background color, gradient, or image. And the borderRadius property can define the border radius. We can make rounded corner borders using this borderRadius property. We also can set the border color, thickness, and sides.

So finally, the flutter app developers can add a border around a DropdownButton widget by wrapping it with a Container widget and setting a border to the Container widget by adding its decoration property value.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.red),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample>{
String dropdownValue = "Beige";

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - DropdownButton Border")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
decoration: BoxDecoration(
border: Border.all(
color: Colors.blueGrey,
width: 1,
style: BorderStyle.solid
),
borderRadius: BorderRadius.circular(8)
),
child: DropdownButton<String>(
value: dropdownValue,
items: <String>['Beige','Maroon','Blanched almond','Aqua']
.map<DropdownMenuItem<String>>((String value){
return DropdownMenuItem<String>(
value: value,
child:Text(value)
);
}).toList(),
onChanged: (String? newValue){
// do something here
setState(() {
dropdownValue = newValue??dropdownValue;
});
},
underline: DropdownButtonHideUnderline(child: Container())
)
)
);
}
}

flutter - How to change DropdownButton width

Flutter - DropdownButton Width
The DropdownButton class represents a material design button for selecting from a list of items. The DropdownButton allows the user to select from a number of items. The DropdownButton shows the currently selected item as well as an arrow that opens a menu for selecting another item. The DropdownButton’s all entries in a menu must represent values with consistent types. Commonly, the flutter developers use an enum. The DropdownButton class’s onChanged callback should update a state variable that defines the dropdown's value. When the flutter developers set the onChanged callback to null or the list of items to null then the dropdown button will be disabled.

The following flutter app development tutorial will demonstrate how to set or change the width of a DropdownButton widget. But there is no built-in property or method in DropdownButton class to set or change the width of the DropdownButton widget itself and its child items.

So we have to apply a simple trick to change the width of a DropdownButton widget. Here we wrapped the DropdownButton widget with a SizedBox widget to define the width of a DropdownButton widget instance. When we set or change the SizedBox widget’s width property value it also simultaneously changes the DropdownButton widget width.

The SizedBox class represents a box with a specified size. The SizedBox widget forces its child widget to have a specific width and/or height. If either the width or height is null, the SizedBox widget will try to size itself to match the child's size in that dimension. So here we set the sizedBox width to change the DropdownButton width. And we don’t set the height of SizedBox so that the DropdownButton height remains unchanged.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.red),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample>{
String dropdownValue = "Beige";

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - DropdownButton Width")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: SizedBox(
width: 250,
child: DropdownButton<String>(
isExpanded: true,
value: dropdownValue,
items: <String>['Beige','Maroon','Blue','Aqua','Pink']
.map<DropdownMenuItem<String>>((String value){
return DropdownMenuItem<String>(
value: value,
child:Text(value)
);
}).toList(),
onChanged: (String? newValue){
// do something here
setState(() {
dropdownValue = newValue??dropdownValue;
});
}
)
)
);
}
}

flutter - How to remove underline from DropdownButton

Flutter - DropdownButton Remove Underline
The DropdownButton class represents a material design button for selecting from a list of items. The DropdownButton allows the user to select from a number of items. The DropdownButton shows the currently selected item as well as an arrow that opens a menu for selecting another item. The DropdownButton’s all entries in a menu must represent values with consistent types. Commonly, the flutter developers use an enum. The DropdownButton class’s onChanged callback should update a state variable that defines the dropdown's value. When the flutter developers set the onChanged callback to null or the list of items to null then the dropdown button will be disabled.

The following flutter app development tutorial will demonstrate how to remove the underline from a DropdownButton widget. Here we will use the DropdownButton class’s underline property to hide/remove the underline of the DropdownButton widget.

The DropdownButton class’s underline property value is a widget that is widget to use for drawing the drop-down button's underline. For this property value, we pass a DropdownButtonHideUnderline instance to hide the underline from the specified DropdownButton widget.

The DropdownButtonHideUnderline class represents an inherited widget that causes any descendant DropdownButton widgets to not include their regular underline. The DropdownButtonHideUnderline is used by DataTable to remove the underline from any DropdownButton widgets placed within material data tables. This class child property value is the widget below this widget in the tree. Here we pass an empty Container instance for this child property value.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.red),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample>{
String dropdownValue = "Orange";

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("DropdownButton Remove Underline")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: DropdownButton<String>(
value: dropdownValue,
items: <String>['Orange','Magenta','Olive','Black','Pink']
.map<DropdownMenuItem<String>>((String value){
return DropdownMenuItem<String>(
value: value,
child:Text(value)
);
}).toList(),
onChanged: (String? newValue){
// do something here
setState(() {
dropdownValue = newValue??dropdownValue;
});
},
underline: DropdownButtonHideUnderline(child: Container()),
)
);
}
}

flutter - How to use DropdownButton

Flutter DropdownButton
The DropdownButton class represents a material design button for selecting from a list of items. The dropdown button allows the user to select from a number of items. The DropdownButton shows the currently selected item as well as an arrow that opens a menu for selecting another item.

The DropdownButton’s all entries in a menu must represent values with consistent types. Commonly, the flutter developers use an enum. Each DropdownMenuItem in items must be specialized with that same type of argument.

The DropdownButton class’s onChanged callback should update a state variable that defines the dropdown's value. This callback should also call State.setState to rebuild the dropdown menu with the new value.

When the flutter developers set the onChanged callback to null or the list of items to null then the dropdown button will be disabled. In a disabled DropdownButton its arrow will be displayed in grey and it will not respond to input.

The DropdownButton class has many properties to customize its look and behaviors such as alignment, autofocus, borderRadius, disabledHint, dropdownColor, elevation, focusColor, hint, icon, items, onChanged, onTap, style, and isExpanded.

The DropdownButton class also has several methods to enhance its usability for example createElement(), createState(), toString(), toStringDeep(), toStringShort(), and toStringShallow() methods.
main.dart

import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.red),
home: const StateExample()
);
}
}

class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);

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

class _StateExampleState extends State<StateExample>{
String dropdownValue = "Red";

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFEFEFA),
appBar: AppBar(
title: const Text("Flutter - DropdownButton Example")
),
body: bodyContent(),
);
}


bodyContent() {
return Center(
child: DropdownButton<String>(
value: dropdownValue,
items: <String>['Red','Green','Yellow','Black','Pink']
.map<DropdownMenuItem<String>>((String value){
return DropdownMenuItem<String>(
value: value,
child:Text(value)
);
}).toList(),
onChanged: (String? newValue){
setState(() {
dropdownValue = newValue??dropdownValue;
});
SnackBar snackBar = SnackBar(
content: Text("$newValue selected")
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
},
)
);
}
}