Skip to content

Instantly share code, notes, and snippets.

@shihaohong
Created October 10, 2019 21:26
Show Gist options
  • Save shihaohong/a014ed2a8bb817616c46da235daf3ab2 to your computer and use it in GitHub Desktop.
Save shihaohong/a014ed2a8bb817616c46da235daf3ab2 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sample',
home: Scaffold(
appBar: AppBar(title: const Text('Sample')),
body: MyStatefulWidget(),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
final List<String> items = <String>['1', '2', '3'];
String selectedItem = '1';
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Enabled DropdownButton:'),
DropdownButton<String>(
value: null,
hint: Text('Hint text here'),
items: items.map((item) {
return DropdownMenuItem(
value: item,
child: Text(item),
);
}).toList(),
onChanged: (String value) {
setState(() { selectedItem = value; });
},
),
Divider(),
Text('Disabled DropdownButton:'),
DropdownButton<String>(
value: selectedItem,
hint: Text('Hint text here'),
items: items.map((item) {
return DropdownMenuItem(
value: item,
child: Text(item),
);
}).toList(),
onChanged: null,
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment