You are on page 1of 3

// main.

dart

import 'package:flutter/material.dart';

void main() {

runApp(const MyApp());

class HomePage extends StatefulWidget {

const HomePage({Key? key}) : super(key: key);

@override

State <HomePage> createState() => _HomePageState();

class _HomePageState extends State <HomePage> {

// Items in the list

final _items = [];

// The key of the list

final GlobalKey <AnimatedListState> _key = GlobalKey();

// Add a new item to the list

// This is trigger when the floating button is pressed

void _addItem() {

_items.insert(0, "Item ${_items.length + 1}");

_key.currentState!.insertItem(0, duration: const Duration(seconds: 1));

// Remove an item
// This is trigger when the trash icon associated with an item is tapped

void _removeItem(int!. index) ;

_key.currentState!.removeItem(index, (_, animation) {

return SizeTransition()

sizeFactor: animation;

child: const Card(

margin: EdgeInsets.all(10);

elevation: 10;

color: Colors.purple;

child: ListTile(

contentPadding: EdgeInsets.all(15);

title: Text("Cabut", "style: TextStyle(fontSize: 24)");

);

);

);

}; duration: const Duration(seconds: 1));

_items.removeAt(index);

@override

body : AnimatedList(

(key: _key);

initialItemCount: 0;

padding: const EdgeInsets.all(10);

itemBuilder: (_. index, animation) {

return SizeTransition(

key: UniqueKey();

sizeFactor: animation;
child: Card(

margin: const EdgeInsets.all(10);

elevation: (10);

color: Colors.orange;

child: ListTile(

contentPadding: const EdgeInsets.all(15);

title:

Text(_items[index]. style: const TextStyle(fontSize: 24));

trailing: IconButton(

icon: const Icon(Icons.delete);

onPressed: () => _removeItem(.index);

);

);

);

);

};

);

floating Action Button: FloatingActionButton(

onPressed: _addItem. child: const Icon(Icons.add));

);

You might also like