Created
August 10, 2019 09:13
-
-
Save juliuscanute/7953e7152066ef57bf968b3a3ace147d to your computer and use it in GitHub Desktop.
[Creating Stateful Widgets] #flutter #dart #stateful #widget
This file contains hidden or 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
| class CategoryRoute extends StatefulWidget { | |
| const CategoryRoute(); | |
| @override | |
| _CategoryRouteState createState() => _CategoryRouteState(); | |
| } | |
| class _CategoryRouteState extends State<CategoryRoute> { | |
| final _categories = <Category>[]; | |
| static const _categoryNames = <String>[ | |
| 'Length', | |
| 'Area', | |
| 'Volume', | |
| 'Mass', | |
| 'Time', | |
| 'Digital Storage', | |
| 'Energy', | |
| 'Currency', | |
| ]; | |
| static const _baseColors = <Color>[ | |
| Colors.teal, | |
| Colors.orange, | |
| Colors.pinkAccent, | |
| Colors.blueAccent, | |
| Colors.yellow, | |
| Colors.greenAccent, | |
| Colors.purpleAccent, | |
| Colors.red, | |
| ]; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| for (var i = 0; i < _categoryNames.length; i++) { | |
| _categories.add(Category( | |
| name: _categoryNames[i], | |
| color: _baseColors[i], | |
| iconLocation: Icons.cake, | |
| units: _retrieveUnitList(_categoryNames[i]), | |
| )); | |
| } | |
| } | |
| Widget _buildCategoryWidgets() { | |
| return ListView.builder( | |
| itemBuilder: (BuildContext context, int index) => _categories[index], | |
| itemCount: _categories.length, | |
| ); | |
| } | |
| List<Unit> _retrieveUnitList(String categoryName) { | |
| return List.generate(10, (int i) { | |
| i += 1; | |
| return Unit( | |
| name: '$categoryName Unit $i', | |
| conversion: i.toDouble(), | |
| ); | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| final listView = Container( | |
| color: _backgroundColor, | |
| padding: EdgeInsets.symmetric(horizontal: 8.0), | |
| child: _buildCategoryWidgets(), | |
| ); | |
| final appBar = AppBar( | |
| elevation: 0.0, | |
| title: Text( | |
| 'Unit Converter', | |
| style: TextStyle( | |
| color: Colors.black, | |
| fontSize: 30.0, | |
| ), | |
| ), | |
| centerTitle: true, | |
| backgroundColor: _backgroundColor, | |
| ); | |
| return Scaffold( | |
| appBar: appBar, | |
| body: listView, | |
| ); | |
| } | |
| } |
This file contains hidden or 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
| class $2 extends State<$1> { | |
| final p1 = <T1>[]; | |
| const $2(); | |
| static const _listValues1 = <String>[ | |
| 'v1', | |
| 'v2', | |
| 'v3', | |
| ]; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| for (var i = 0; i < _listValues1.length; i++) { | |
| p1.add(T1(value: _listValues1[i])); | |
| } | |
| } | |
| _buildP1Widgets(List<Widget> p1) { | |
| return ListView.builder( | |
| itemBuilder: (BuildContext context, int index) => p1[index], | |
| itemCount: p1.length, | |
| ); | |
| } | |
| Widget build(BuildContext context) { | |
| return Container( | |
| padding: EdgeInsets.symmetric(horizontal: 8.0), | |
| child: _buildP1Widgets(p1), | |
| ); | |
| } | |
| } |
This file contains hidden or 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
| class T1 extends StatelessWidget { | |
| final T1 v1; | |
| const $1({ | |
| Key key, | |
| @required this.v1, | |
| }) : assert( v1 != null ), | |
| super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Widget(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment