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评论99阅读模式
英文:

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

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

  1. 如果我理解您的意思正确的话,这应该能解决问题:
  2. class HomePage extends StatefulWidget {
  3. const HomePage({Key? key}) : super(key: key);
  4. @override
  5. State<HomePage> createState() => _HomePageState();
  6. }
  7. class _HomePageState extends State<HomePage>{
  8. int currentIndex = 0;
  9. @override
  10. void initState() {
  11. super.initState();
  12. timerMethod();
  13. }
  14. void timerMethod(){
  15. Timer(const Duration(seconds: 3), (){
  16. setState(() {
  17. currentIndex++;
  18. });
  19. if(currentIndex == 4) currentIndex = 0;
  20. timerMethod();
  21. });
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: AppBar(
  27. backgroundColor: Colors.black,
  28. ),
  29. body: ListView.builder(
  30. itemCount: 4,
  31. itemBuilder: (context, index) {
  32. return Padding(
  33. padding: const EdgeInsets.all(8.0),
  34. child: ListTile(
  35. shape: RoundedRectangleBorder(
  36. borderRadius: BorderRadius.circular(50)
  37. ),
  38. tileColor: currentIndex == index ? Colors.teal : Colors.tealAccent,
  39. leading: const CircleAvatar(
  40. backgroundColor: Colors.white,
  41. child: Icon(Icons.person,color: Colors.teal,),
  42. ),
  43. title: Text("项目 ${index+1}"),
  44. ),
  45. );
  46. },
  47. ),
  48. );
  49. }
  50. }
英文:

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

  1. class HomePage extends StatefulWidget {
  2. const HomePage({Key? key}) : super(key: key);
  3. @override
  4. State&lt;HomePage&gt; createState() =&gt; _HomePageState();
  5. }
  6. class _HomePageState extends State&lt;HomePage&gt;{
  7. int currentIndex = 0;
  8. @override
  9. void initState() {
  10. super.initState();
  11. timerMethod();
  12. }
  13. void timerMethod(){
  14. Timer(const Duration(seconds: 3), (){
  15. setState(() {
  16. currentIndex++;
  17. });
  18. if(currentIndex == 4) currentIndex = 0;
  19. timerMethod();
  20. });
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. appBar: AppBar(
  26. backgroundColor: Colors.black,
  27. ),
  28. body: ListView.builder(
  29. itemCount: 4,
  30. itemBuilder: (context, index) {
  31. return Padding(
  32. padding: const EdgeInsets.all(8.0),
  33. child: ListTile(
  34. shape: RoundedRectangleBorder(
  35. borderRadius: BorderRadius.circular(50)
  36. ),
  37. tileColor: currentIndex == index ? Colors.teal : Colors.tealAccent,
  38. leading: const CircleAvatar(
  39. backgroundColor: Colors.white,
  40. child: Icon(Icons.person,color: Colors.teal,),
  41. ),
  42. title: Text(&quot;Item ${index+1}&quot;),
  43. ),
  44. );
  45. },
  46. ),
  47. );
  48. }
  49. }

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:

确定