英文:
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
答案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<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}"),
),
);
},
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论