[ flutter ] 动画的应用

import 'dart:async';

import 'package:flutter/material.dart';

class IndexTab extends StatefulWidget {
  const IndexTab({super.key});

  @override
  State<IndexTab> createState() => _IndexTabState();
}

class _IndexTabState extends State<IndexTab> {
  final _globalKey = GlobalKey<AnimatedListState>();
  final List<String> titles = [];
  bool lock = false;

  _buildItem(index) {
    return ListTile(
      title: Text(titles[index]),
      trailing: IconButton(
        icon: const Icon(Icons.delete_outline),
        onPressed: () {
          print(lock);
          if (lock == true) return;
          lock = true;
          Timer(const Duration(milliseconds: 500), () {
            lock = false;
          });
          var removeItem = _buildItem(index);
          titles.removeAt(index);
          _globalKey.currentState!.removeItem(index, (context, animation) {
            return FadeTransition(
              opacity: animation,
              child: removeItem,
            );
          });
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: FloatingActionButton(
            onPressed: () {
              titles.add("我了个去 太TM复杂了");
              _globalKey.currentState!.insertItem(titles.length - 1);
            },
            child: const Icon(Icons.add)),
        appBar: AppBar(
          title: const Text("首页"),
        ),
        body: AnimatedList(
            key: _globalKey,
            initialItemCount: titles.length,
            itemBuilder:
                (BuildContext context, int index, Animation<double> animation) {
              return FadeTransition(
                opacity: animation,
                child: _buildItem(index),
              );
            }));
  }
}

 

posted @ 2025-07-22 22:36  深海里的星星i  阅读(5)  评论(0)    收藏  举报