Skip to content

Instantly share code, notes, and snippets.

@iapicca
Created November 1, 2019 08:57
Show Gist options
  • Save iapicca/1308a02f5b3fa8ae930ddf92904a0201 to your computer and use it in GitHub Desktop.
Save iapicca/1308a02f5b3fa8ae930ddf92904a0201 to your computer and use it in GitHub Desktop.
DropDown Example
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => Scaffold(
body: Center(
child:DropDownWidget(),),);
}
class DropDownWidget extends StatefulWidget {
@override
_DropDownWidgetState createState() => _DropDownWidgetState();
}
const _hintText = "hint";
Widget _hintWidget = Text(_hintText);
List<int> _values = [1, 2, 3, 4, 5];
List<DropdownMenuItem> _items = _values.map((v) => DropdownMenuItem(
value: v,
child: Text(v.toString()),
)).toList();
class _DropDownWidgetState extends State<DropDownWidget> {
int _selectedValue = _values[0];
void _setValue(int value) => setState(()=> _selectedValue = value);
@override
Widget build(BuildContext context) => DropdownButton<int>(
hint: _hintWidget,
value: _selectedValue,
items: _items,
onChanged: (value) => _setValue(value),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment