Skip to content

Instantly share code, notes, and snippets.

@shihaohong
Created October 8, 2019 22:45
Show Gist options
  • Save shihaohong/56a4104c94c9b1b36810a425ca5bdd07 to your computer and use it in GitHub Desktop.
Save shihaohong/56a4104c94c9b1b36810a425ca5bdd07 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('DropdownButton'),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: TestStatefulWidget(),
),
),
));
}
class TestStatefulWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => TestStatefulWidgetState();
}
class TestStatefulWidgetState extends State<TestStatefulWidget> {
static const List<double> differentHeights = const [100, 200, 300, 400];
double height = differentHeights[0];
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Center(
child: DropdownButton(
value: height,
itemHeight: null,
onChanged: (newHeight) {
setState(() {
height = newHeight;
});
},
items: differentHeights.map((height) {
return DropdownMenuItem(
value: height,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
color: Colors.blue,
height: height,
width: 100,
child: Center(child: Text('height: $height')),
),
),
);
}).toList(),
),
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment