@override Widget build(BuildContext context) { return Scaffold( // Page widget. appBar: AppBar( // Page app bar with title and back button if user can return to previous screen. title: Text(widget.title), // Text to display page title. ), body: Center( // Widget to center child widget. child: Column( // Display children widgets in column. mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( // Static text. 'You have pushed the button this many times:', ), Text( // Text with our taps number. '$_counter', // $ sign allows us to use variables inside a string. style: Theme.of(context).textTheme.headline4,// Style of the text, “Theme.of(context)” takes our context and allows us to access our global app theme. ), ], ), ), // Floating action button to increment _counter number. floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: Icon(Icons.add), ), ); }
/// Class that stores list item info: /// [id] - unique identifier, number. /// [icon] - icon to display in UI. /// [title] - text title of the item. /// [description] - text description of the item. class ItemModel { // class constructor ItemModel(this.id, this.icon, this.title, this.description);
// class fields final int id; final IconData icon; final String title; final String description; }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: ListView.builder( // Widget which creates [ItemWidget] in scrollable list. itemCount: _items.length, // Number of widget to be created. itemBuilder: (context, itemIndex) => // Builder function for every item with index. ItemWidget(_items[itemIndex], () { _onItemTap(context, itemIndex); }), )); }
// Method which uses BuildContext to push (open) new MaterialPageRoute (representation of the screen in Flutter navigation model) with ItemDetailsPage (StateFullWidget with UI for page) in builder. _onItemTap(BuildContext context, int itemIndex) { Navigator.of(context).push(MaterialPageRoute( builder: (context) => ItemDetailsPage(_items[itemIndex]))); } }
// StatelessWidget with UI for our ItemModel-s in ListView. class ItemWidget extends StatelessWidget { const ItemWidget(this.model, this.onItemTap, {Key key}) : super(key: key);
final ItemModel model; final Function onItemTap;
@override Widget build(BuildContext context) { return InkWell( // Enables taps for child and add ripple effect when child widget is long pressed. onTap: onItemTap, child: ListTile( // Useful standard widget for displaying something in ListView. leading: Icon(model.icon), title: Text(model.title), ), ); } }