Created
April 23, 2019 13:18
-
-
Save hmlongco/0989fa8f951d2ff87acc2c44c423f5c4 to your computer and use it in GitHub Desktop.
Flutter List Picker
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 'package:test_app/models/commodities_model.dart'; | |
class CommodityPicker extends StatelessWidget { | |
CommodityPicker(this.commodityMap, this.selected); | |
final Map<String, Commodity> commodityMap; // VM | |
final String selected; // VM | |
@override | |
Widget build(BuildContext context) { | |
final _names = commodityMap.values.map((c) => c.name).toList(); // VM | |
final _symbols = commodityMap.values.map((c) => c.symbol).toList(); // VM | |
return Scaffold( | |
backgroundColor: Colors.white, | |
appBar: AppBar(title: Text("Commodity")), // VM | |
body: Column(children: <Widget>[ | |
SizedBox(height: 20), | |
Expanded( | |
child: ListView.separated( | |
itemCount: _names.length, // VM | |
itemBuilder: (BuildContext ctxt, int index) { | |
final commodity = _names[index]; // VM | |
final isSelected = _symbols[index] == selected; // VM | |
return FlatButton( | |
child: SafeArea( | |
child: Row( | |
children: <Widget>[Expanded(child: Text(commodity)), Text(isSelected ? "√" : "")], | |
)), | |
padding: EdgeInsets.symmetric(vertical: 0, horizontal: 20.0), | |
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, | |
onPressed: () => Navigator.pop(ctxt, _symbols[index]), // COORDINATOR | |
); | |
}, | |
separatorBuilder: (BuildContext ctxt, int index) { | |
return Divider(); | |
} | |
) | |
), | |
])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment