Created
November 1, 2019 08:57
-
-
Save iapicca/1308a02f5b3fa8ae930ddf92904a0201 to your computer and use it in GitHub Desktop.
DropDown Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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