Last active
February 20, 2020 15:27
-
-
Save esstein/b37b08cc25e0ccdba680090e9ef4b3c1 to your computer and use it in GitHub Desktop.
Flutter complete example to create dynamic buttons widgets with filter and different colors properties
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
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
List item = [ | |
{"title": "Button One", "color": 50}, | |
{"title": "Button Two", "color": 100}, | |
{"title": "Button Three", "color": 200}, | |
{"title": "No show", "color": 0, "hide": '1'}, | |
]; | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: Text("Dynamic Widget - List<Widget>"),backgroundColor: Colors.blue), | |
body: Column( | |
children: <Widget>[ | |
Center(child: buttonBar()), | |
Text('Click the buttons to hide it'), | |
] | |
) | |
) | |
); | |
} | |
Widget buttonBar() { | |
return Column( | |
children: item.where((e) => e['hide'] != '1').map<Widget>((document) { | |
return new FlatButton( | |
child: new Text(document['title']), | |
color: Color.fromARGB(document['color'], 0, 100, 0), | |
onPressed: () { | |
setState(() { | |
print("click on ${document['title']} lets hide it"); | |
final tile = item.firstWhere((e) => e['title'] == document['title']); | |
tile['hide'] = '1'; | |
}); | |
}, | |
); | |
} | |
).toList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment