Skip to content

Instantly share code, notes, and snippets.

@m00zi
Forked from jonahwilliams/example.dart
Created August 6, 2020 09:08
Show Gist options
  • Save m00zi/445e7f3d1f57012a13bbcfdd5b1d31f3 to your computer and use it in GitHub Desktop.
Save m00zi/445e7f3d1f57012a13bbcfdd5b1d31f3 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(),
)),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment