New post on /r/flutterdev subreddit:
<b>Pass Args to Widget Build</b>
Hey all, first post and started with flutter yesterday.I have created the below which I call using; ProductTile()The code renders a box that contains product information, an image etc, along with an increment counter which adjusts itself as buttons - or + are pressed. (this will eventually be quantity to add to basket).My problem is that it is all hard coded, I need to figure out how to make this dynamic.Am i not able to force this to take an argument and pass the argument when i call the function / builder (not sure). i.e..ProductTile(<Product>)<Product> Would be json format? Is this doable or am i barking up the wrong tree with Flutters workings?<pre>import 'package:flutter/material.dart'; class ProductTile extends StatefulWidget {
@override _ProductTileState createState() => _ProductTileState(); } class _ProductTileState extends State<ProductTile> { int _counter = 0; void _incrementAdd() { setState(() { _counter++; }); } void _incrementSubtract() { setState(() { if (_counter > 0) { _counter--; } }); } void _basketAdd() { setState(() { _counter = 0; }); }
@override Widget build(BuildContext context) { return Padding( padding: EdgeInsets.only(bottom: 10.0), child: Container( padding: const EdgeInsets.all(10), height: 350, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(15)), boxShadow: [ BoxShadow( color: Colors.grey, offset: Offset(-1, 1), blurRadius: 5, ), ], ), child: Column( children: <Widget>[ Container( height: 20, decoration: const BoxDecoration(color: Colors.transparent), child: Row( children: [ Container( child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Text( '£', style: TextStyle( fontSize: 12, ), ), Text( '199', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 16, ), ), Text( '99', style: TextStyle( fontSize: 12, ), ), ], ), ), SizedBox( width: 10, ), Container( child: Text( 'Product Offer', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 16, ), ), ), ], ), ), SizedBox(height: 5), Material( elevation: 2.0, borderRadius: BorderRadius.all(Radius.circular(10)), child: Container( height: 250, decoration: const BoxDecoration( color: Colors.deepOrange, borderRadius: BorderRadius.all(Radius.circular(10))), child: Image(image: AssetImage('assets/echo.jpg')), ), ), SizedBox(height: 7.25), SizedBox( width:double.infinity, height: 0.5, child: const DecoratedBox( decoration: const BoxDecoration( color: Colors.grey ), ), ), SizedBox(height: 7.25), Container( height: 40, decoration: const BoxDecoration(color: Colors.transparent), child: Row( mainAxisAlignment:
MainAxisAlignment.center, children: <Widget>[ Material( elevation: 2.0, color: Colors.deepOrange, child: InkWell( splashColor: Colors.white70, onTap: _incrementSubtract, child: Container( width: 30, height: 30, child: Padding( padding: EdgeInsets.all(1.0), child: const Icon( Icons.remove, color: Colors.white, ), ), ), ), ), SizedBox(width: 5.0), Material( elevation: 2.0, borderRadius: BorderRadius.all(Radius.circular(20)), child: Container( width: 50, height: 50, decoration: BoxDecoration( border: Border.all(width: 2, color: Colors.deepOrange), borderRadius: BorderRadius.all(Radius.circular(20)), color: Colors.white), child: Center( child: Text( '$_counter', style: TextStyle( fontWeight: FontWeight.bold, fontSize: 18, ), ), ), ), ), SizedBox(width: 5.0), Material( elevation: 2.0, color: Colors.deepOrange, child: InkWell( splashColor: Colors.white70, onTap: _incrementAdd, child: Container( width: 30, height: 30, child: Padding( padding: EdgeInsets.all(1.0), child: const Icon( Icons.add, color: Colors.white, )), ), ), ), SizedBox(width: 20.0), Material( elevation: 2.0, color: Colors.deepOrange, borderRadius: BorderRadius.all(Radius.circular(5)), child: InkWell( splashColor: Colors.white70, onTap: _basketAdd, child: Row( children: <Widget>[ Container( width: 20, height: 30, child: Padding( padding: EdgeInsets.all(1.0), child:…