Created
March 27, 2021 10:37
-
-
Save IsmailAlamKhan/07d1fa2ea980b242bd072b6efd3b6dc2 to your computer and use it in GitHub Desktop.
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 StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Material App', | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({Key? key}) : super(key: key); | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
List<String> items = <String>[]; | |
int counter = 0; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Column( | |
children: [ | |
...List.generate( | |
items.length, | |
(index) => TextField( | |
decoration: InputDecoration( | |
hintText: items[index], | |
suffixIcon: IconButton( | |
icon: Icon(Icons.delete), | |
onPressed: () { | |
items.remove(items[index]); | |
setState(() {}); | |
}, | |
), | |
), | |
), | |
), | |
ButtonBar( | |
children: [ | |
ElevatedButton( | |
onPressed: () { | |
items.add('TextField ${counter + 1}'); | |
counter++; | |
setState(() {}); | |
}, | |
child: Text('Add'), | |
), | |
ElevatedButton( | |
onPressed: () { | |
items.clear(); | |
counter = 0; | |
setState(() {}); | |
}, | |
child: Text('Delete all'), | |
), | |
], | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment