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

huangapple go评论57阅读模式
英文:

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

问题

尝试使用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

答案1

得分: 0

以下是您提供的代码的中文翻译部分:

如果我理解您的意思正确的话,这应该能解决问题:

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("项目 ${index+1}"),
            ),
          );
        },
      ),
    );
  }
}
英文:

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

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

  @override
  State&lt;HomePage&gt; createState() =&gt; _HomePageState();
}

class _HomePageState extends State&lt;HomePage&gt;{

  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(&quot;Item ${index+1}&quot;),
            ),
          );
        },
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年4月20日 00:03:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定