Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Created April 23, 2019 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmlongco/0989fa8f951d2ff87acc2c44c423f5c4 to your computer and use it in GitHub Desktop.
Save hmlongco/0989fa8f951d2ff87acc2c44c423f5c4 to your computer and use it in GitHub Desktop.
Flutter List Picker
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