Created
March 27, 2018 21:28
-
-
Save chris84948/ab11ac3d6ba130234994521eafb2d28c to your computer and use it in GitHub Desktop.
Full example of dart.main from the flutter list + dialog example on InsertBreakpoint.Wordpress.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:async'; | |
import 'package:numberpicker/numberpicker.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: 'List Tutorial'), | |
); | |
} | |
} | |
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> { | |
List _items = []; | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(widget.title), | |
), | |
body: new ListView.builder( | |
itemCount: _items.length, | |
itemBuilder: (BuildContext context, int position) { | |
//return new ListItem(_items[position]); | |
return getRow(position); | |
}), | |
floatingActionButton: new FloatingActionButton( | |
tooltip: 'Add Item', | |
child: new Icon(Icons.add), | |
onPressed: () => _openDialogAddItem(), | |
), | |
); | |
} | |
Widget getRow(int position) { | |
return new FlatButton( | |
child: new ListTile( | |
title: new Text(_items[position].text), | |
trailing: new Text(_items[position].number.toString()), | |
), | |
onPressed: () { | |
setState(() { | |
_items.removeAt(position); | |
}); | |
}, | |
); | |
} | |
Future _openDialogAddItem() async { | |
ModelData data = await Navigator.of(context).push( | |
new MaterialPageRoute<ModelData>( | |
builder: (BuildContext context) { | |
return new DialogAddItem(); | |
}, | |
fullscreenDialog: true)); | |
setState(() { | |
_items.add(data); | |
}); | |
} | |
} | |
class DialogAddItem extends StatefulWidget { | |
@override | |
_DialogAddItemState createState() => new _DialogAddItemState(); | |
} | |
class _DialogAddItemState extends State<DialogAddItem> { | |
bool _canSave = false; | |
ModelData _data = new ModelData.empty(); | |
void _setCanSave(bool save) { | |
if (save != _canSave) | |
setState(() => _canSave = save); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final ThemeData theme = Theme.of(context); | |
return new Scaffold( | |
appBar: new AppBar( | |
title: const Text('Add New Item'), | |
actions: <Widget> [ | |
new FlatButton( | |
child: new Text('ADD', style: theme.textTheme.body1.copyWith(color: _canSave ? Colors.white : new Color.fromRGBO(255, 255, 255, 0.5))), | |
onPressed: _canSave ? () { Navigator.of(context).pop(_data); } : null | |
) | |
] | |
), | |
body: new Form( | |
child: new ListView( | |
padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 16.0), | |
children: <Widget>[ | |
new TextField( | |
decoration: const InputDecoration( | |
labelText: "Text", | |
), | |
onChanged: (String value) { | |
_data.text = value; | |
_setCanSave(value.isNotEmpty); | |
}, | |
), | |
new Padding( | |
padding: const EdgeInsets.symmetric(vertical: 30.0, horizontal: 0.0), | |
child: new NumberPicker.integer( | |
initialValue: _data.number, | |
minValue: 0, | |
maxValue: 100, | |
onChanged: (newValue) => | |
setState(() => _data.number = newValue)), | |
) | |
].toList(), | |
), | |
), | |
); | |
} | |
} | |
class ModelData | |
{ | |
String text; | |
int number; | |
ModelData(this.text, this.number); | |
ModelData.empty() { | |
text = ""; | |
number = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add
.dart
to the filename so syntax highlighting kicks inlooks nicer w/ color