【发布时间】:2022-01-17 13:42:17
【问题描述】:
我正在构建一个 Flutter 应用程序,其中使用 ListView 显示列表。它返回一个带有用户信息的 ListTile。 ListTile 包含 leading、title 和 subtitle,其中 trailing 设置为 ElevatedButton。
我想点击“邀请按钮”并更改 ListTile 中的 color、text 和 subtitle
我该怎么做?这是我写的代码。但它正在改变每个列表项的状态。
class InviteFriends extends StatefulWidget {
const InviteFriends({Key? key}) : super(key: key);
@override
State<InviteFriends> createState() => _InviteFriendsState();
}
class _InviteFriendsState extends State<InviteFriends> {
bool isSelected = false;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
}
ListView.builder下的UI:
ListTile(
title: const Text('Haris'),
subtitle: const Text(
isSelected ? 'Invitation Sent' : 'Not Invited Yet',
),
leading: CircleAvatar(
backgroundImage: NetworkImage(
_userProfile!.profilePhoto.toString(),
),
),
trailing: ElevatedButton(
style: ElevatedButton.styleFrom(
elevation: 0.0,
primary: isSelected ? Colors.orange : Colors.green,
side: BorderSide.none,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
child: const Text(
isSelected ? 'Invited': 'Invite',
),
onPressed: () {
setState(() {
isSelected = !isSelected;
});
},
),
);
我也在 ListTile 中使用 ValueKey(index) 尝试了 Keys,但它也没有工作。我该怎么办?谢谢
【问题讨论】:
标签: flutter listview flutter-listview flutter-state