1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2026-08-24 16:50:47 +00:00
Files
flutter-examples/dropdown_button/lib/example.dart
Nishant Srivastava e143b4c3a2 refactor: split app wrapper from focused example code (pilot)
Apply a consistent structure to 5 example apps (simple_material_app,
stateful_widget, dropdown_button, using_gradient, using_tabs):

- lib/main.dart now contains only the app bootstrap: runApp + a const
  MyApp widget wrapping the app in a MaterialApp.
- lib/example.dart holds the full screen (Scaffold, AppBar, and the
  focused code that demonstrates the example feature), marked with a
  '// Example:' banner comment.

This makes it easy to pinpoint the code that teaches each concept while
keeping the wrapping/chrome code uniform and minimal. Review this pilot
before rolling the pattern out to the remaining apps.
2026-08-18 00:24:22 +02:00

71 lines
1.7 KiB
Dart

import 'package:flutter/material.dart';
// Example: the focused code for this app.
// A DropdownButton that lets the user pick a fruit from a list.
class Example extends StatefulWidget {
const Example({super.key});
@override
State<Example> createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
final List<String> _fruits = [
"Apple",
"Banana",
"Pineapple",
"Mango",
"Grapes"
];
late List<DropdownMenuItem<String>> _dropDownMenuItems;
late String _selectedFruit;
@override
void initState() {
_dropDownMenuItems = buildAndGetDropDownMenuItems(_fruits);
_selectedFruit = _dropDownMenuItems[0].value!;
super.initState();
}
List<DropdownMenuItem<String>> buildAndGetDropDownMenuItems(
List<String> fruits) {
List<DropdownMenuItem<String>> items = [];
for (String fruit in fruits) {
items.add(DropdownMenuItem(value: fruit, child: Text(fruit)));
}
return items;
}
void changedDropDownItem(String? selectedFruit) {
setState(() {
_selectedFruit = selectedFruit!;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("DropDown Button Example"),
),
body: Container(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Please choose a fruit: "),
DropdownButton<String>(
value: _selectedFruit,
items: _dropDownMenuItems,
onChanged: changedDropDownItem,
)
],
),
),
),
);
}
}