Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _selectedId;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text("Test"),
),
body: new ListView(padding: const EdgeInsets.all(32.0), children: [
new Container(
padding: const EdgeInsets.all(10.0),
child: new DropdownButton<String>(
hint: const Text("Pick a thing"),
value: _selectedId,
onChanged: (String value) {
setState(() {
_selectedId = value;
});
},
items:
<String>['One', 'Two', 'Three', 'Four'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
)),
]),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.add),
tooltip: "New Dialog",
onPressed: () {
showDialog(
context: context,
child: new MyDialog(
onValueChange: _onValueChange,
initialValue: _selectedId,
));
},
));
}
void _onValueChange(String value) {
setState(() {
_selectedId = value;
});
}
}
class MyDialog extends StatefulWidget {
const MyDialog({this.onValueChange, this.initialValue});
final String initialValue;
final void Function(String) onValueChange;
@override
State createState() => new MyDialogState();
}
class MyDialogState extends State<MyDialog> {
String _selectedId;
@override
void initState() {
super.initState();
_selectedId = widget.initialValue;
}
Widget build(BuildContext context) {
return new SimpleDialog(
title: new Text("New Dialog"),
children: <Widget>[
new Container(
padding: const EdgeInsets.all(10.0),
child: new DropdownButton<String>(
hint: const Text("Pick a thing"),
value: _selectedId,
onChanged: (String value) {
setState(() {
_selectedId = value;
});
widget.onValueChange(value);
},
items: <String>['One', 'Two', 'Three', 'Four'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
)),
],
);
}
}
@gregko

This comment has been minimized.

Copy link

@gregko gregko commented May 16, 2018

Thank you, appreciate this gist! Helped me a lot today to get job done and learn Flutter better.

@pizzalbj

This comment has been minimized.

Copy link

@pizzalbj pizzalbj commented Jun 26, 2018

3Q~

@soragui

This comment has been minimized.

Copy link

@soragui soragui commented Sep 11, 2018

3Q~

@fikurniawan

This comment has been minimized.

Copy link

@fikurniawan fikurniawan commented Mar 21, 2019

Thank you, you've solved my problem

@SilenceCodder

This comment has been minimized.

Copy link

@SilenceCodder SilenceCodder commented May 9, 2019

Thank you so, so, so much, You really save my time After battling it for some hours..

@balfaz

This comment has been minimized.

Copy link

@balfaz balfaz commented Aug 22, 2020

thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment