Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created March 7, 2018 03:33
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jonahwilliams/25fa99b6b73fdcbd58ac85585343bbd0 to your computer and use it in GitHub Desktop.
Save jonahwilliams/25fa99b6b73fdcbd58ac85585343bbd0 to your computer and use it in GitHub Desktop.
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
Copy link

gregko commented May 16, 2018

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

@pizzalbj
Copy link

3Q~

@soragui
Copy link

soragui commented Sep 11, 2018

3Q~

@fikurniawan
Copy link

Thank you, you've solved my problem

@SilenceCodder
Copy link

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

@balfaz
Copy link

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