Skip to content

Instantly share code, notes, and snippets.

@IsmailAlamKhan
Created March 27, 2021 10:37
Show Gist options
  • Save IsmailAlamKhan/07d1fa2ea980b242bd072b6efd3b6dc2 to your computer and use it in GitHub Desktop.
Save IsmailAlamKhan/07d1fa2ea980b242bd072b6efd3b6dc2 to your computer and use it in GitHub Desktop.
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