Created
May 26, 2022 05:11
-
-
Save ivanauliaa/c85930d95328b648e94cb0bca77f16db to your computer and use it in GitHub Desktop.
Membuat tambahan widget di akhir sebuah list of widgets
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'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
State<MyApp> createState() => _MyAppState(); | |
} | |
List<Map<String, dynamic>> cards = [ | |
{ | |
'name': 'BRI', | |
'amount': 2500, | |
}, | |
{ | |
'name': 'BNI', | |
'amount': 3000, | |
}, | |
]; | |
class _MyAppState extends State<MyApp> { | |
Widget plus() { | |
return GestureDetector( | |
onTap: () { | |
cards.add({ | |
'name': 'Tambah', | |
'amount': 2000, | |
}); | |
setState(() {}); | |
}, | |
child: Container( | |
height: 50, | |
width: 100, | |
child: Center( | |
child: Icon(Icons.plus_one), | |
), | |
), | |
); | |
} | |
List<Widget> cardWidgets() { | |
List<Widget> widgets = List<Widget>.generate( | |
cards.length, | |
(index) => Container( | |
height: 50, | |
width: 200, | |
child: Column( | |
children: [ | |
Text('Name: ${cards[index]['name']}'), | |
Text('Amount: ${cards[index]['amount']}'), | |
], | |
), | |
decoration: BoxDecoration( | |
color: Colors.blueGrey, | |
borderRadius: BorderRadius.circular(10), | |
), | |
), | |
); | |
widgets.add(plus()); | |
return widgets; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: Container( | |
height: 200, | |
width: double.infinity, | |
child: ListView( | |
scrollDirection: Axis.horizontal, | |
children: cardWidgets(), | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment