Search code examples
flutterflutter-dependenciesflutter-animation

How to make a list of items flashing on each item sequentially automatically with respect to knowing the index of the current item with flutter


trying to make a list of items flashing on each item sequentially automatically with respect to knowing the index of the current item with flutter

Like This Image


Solution

  • If I understood you correctly. This should do the trick:

    class HomePage extends StatefulWidget {
      const HomePage({Key? key}) : super(key: key);
    
      @override
      State<HomePage> createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage>{
    
      int currentIndex = 0;
    
      @override
      void initState() {
        super.initState();
        timerMethod();
      }
    
      void timerMethod(){
        Timer(const Duration(seconds: 3), (){
          setState(() {
            currentIndex++;
          });
          if(currentIndex == 4) currentIndex = 0;
          timerMethod();
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.black,
          ),
          body: ListView.builder(
            itemCount: 4,
            itemBuilder: (context, index) {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: ListTile(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(50)
                  ),
                  tileColor: currentIndex == index ? Colors.teal : Colors.tealAccent,
                  leading: const CircleAvatar(
                    backgroundColor: Colors.white,
                    child: Icon(Icons.person,color: Colors.teal,),
                  ),
                  title: Text("Item ${index+1}"),
                ),
              );
            },
          ),
        );
      }
    }