This app shows a dropdown with three fruits. When you pick one, it updates the selected fruit.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: Center(
child: FruitDropdown(),
),
),
);
}
}
class FruitDropdown extends StatefulWidget {
const FruitDropdown({super.key});
@override
State<FruitDropdown> createState() => _FruitDropdownState();
}
class _FruitDropdownState extends State<FruitDropdown> {
String selectedFruit = 'apple';
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: selectedFruit,
items: const [
DropdownMenuItem(value: 'apple', child: Text('Apple')),
DropdownMenuItem(value: 'banana', child: Text('Banana')),
DropdownMenuItem(value: 'orange', child: Text('Orange')),
],
onChanged: (String? newValue) {
if (newValue != null) {
setState(() {
selectedFruit = newValue;
});
}
},
);
}
}