我试着在列表视图上长按显示浮动按钮,但浮动按钮没有显示. 知道为什么吗?
我使用以下代码:
return GestureDetector(
onLongPress: () {
print('test');
FloatingActionButton.extended(
onPressed: () {
print('test1');
},
icon: const Icon(Icons.add),
label: const Text('Usuń'),
);
更新1
我已经更新了帖子与更大的代码视图,以更好地了解构建过程… 我正在添加"Dodaj"按钮,到目前为止工作正常,并想添加另一个,"usuń"显示后,列表视图项长按. 你能看一下这段代码并指出我哪里做错了吗?
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[100],
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
//addItem();
},
icon: const Icon(Icons.add),
label: const Text('Dodaj'),
),
appBar: AppBar(
backgroundColor: Colors.grey[900],
title: const Text('Lista zakupowa'),
actions: [
IconButton(
onPressed: signUserOut,
icon: const Icon(Icons.logout),
)
],
),
body: Center(
child: FutureBuilder<List<PostListDetails>>(
future: postsFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasData) {
return buildPosts(snapshot.data!);
} else {
return const Text("Brak danych...");
}
},
),
),
);
}
// function to display fetched data on screen
@override
Widget buildPosts(List<PostListDetails> posts) {
return ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
final post = posts[index];
int customCompare(PostListDetails a, PostListDetails b) {
if (a.status != b.status) {
return a.status!.toInt() - b.status!.toInt();
}
final int categoryCompare = a.kategoria.toString().compareTo(b.kategoria.toString());
if (categoryCompare != 0) {
return categoryCompare;
}
return a.nazwa.toString().compareTo(b.nazwa.toString());
}
return GestureDetector(
onLongPress: () {
print('test1');
FloatingActionButton.extended(
onPressed: () {
print('test2');
},
icon: const Icon(Icons.add),
label: const Text('Usuń'),
);
},
child: Container(
color: post.status! % 2 == 1 ? Colors.grey[100] : Colors.white,
margin: const EdgeInsets.symmetric(vertical: 0.5, horizontal: 0),
padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 15),
height: 50,
width: double.maxFinite,
child: Row(
children: [
Expanded(
flex: 8,
child: Column(
...
),
),
Expanded(
flex: 2,
child: Column(
...
),
),
],
),
),
);
},
);
}
更新2
我已经相应地修改了代码,但这次没有运气...长时间按下Listview项目后,OnLong Press按钮不显示. 这段代码应该创建一个Listview,其中包含一个项目列表和一个始终可见的浮动按钮"+Dodaj"(这部分工作得很好). 我也想创建一个项目后,长按一个"usuń"按钮…此部件不起作用,我的意思是按钮不可见.(未打印"测试1"和"测试2").
代码现在如下所示:
bool showButton = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[100],
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
//addList();
},
icon: const Icon(Icons.add),
label: const Text('Dodaj'),
),
appBar: AppBar(
backgroundColor: Colors.grey[900],
title: const Text('Lista zakupowa'),
actions: [
IconButton(
onPressed: signUserOut,
icon: const Icon(Icons.logout),
)
],
),
body: Center(
child: FutureBuilder<List<PostListDetails>>(
future: postsFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasData) {
return buildPosts(snapshot.data!);
} else {
return const Text("Brak danych...");
}
},
),
),
);
}
// function to display fetched data on screen
@override
Widget buildPosts(List<PostListDetails> posts) {
return ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
final post = posts[index];
int customCompare(PostListDetails a, PostListDetails b) {
if (a.status != b.status) {
return a.status!.toInt() - b.status!.toInt();
}
final int categoryCompare = a.kategoria.toString().compareTo(b.kategoria.toString());
if (categoryCompare != 0) {
return categoryCompare;
}
return a.nazwa.toString().compareTo(b.nazwa.toString());
}
GestureDetector(
onLongPress: () {
print('test1');
setState(() => showButton = true);
},
);
showButton ? FloatingActionButton.extended(
onPressed: () {
print('test2');
//removeList();
},
icon: const Icon(Icons.add),
label: const Text('Usuń'),
) : null;
return Container(
color: post.status! % 2 == 1 ? Colors.grey[100] : Colors.white,
margin: const EdgeInsets.symmetric(vertical: 0.5, horizontal: 0),
padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 15),
height: 50,
width: double.maxFinite,
child: Row(
children: [
Expanded(
flex: 8,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(post.nazwa!, textAlign: TextAlign.left, style: const TextStyle(fontWeight: FontWeight.bold,fontSize: 15,color: Colors.black)),
Text('${post.waga!.toString()} g.', textAlign: TextAlign.left, style: const TextStyle(fontWeight: FontWeight.normal,fontSize: 15,color: Colors.grey))
]
),
),
Expanded(
flex: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Expanded(
flex: 0,
child: Row(
),
),
Expanded(
flex: 10,
child: Row(
children: [
CatIcon(imagePath: (Cat2Ico(post.kategoria!.toString()))),
Checkbox(
value: Int2Bool(post.status!.toInt()),
onChanged: (value) {
setState(() {post.status = Bool2Int(value!);});
saveStatus(post.nazwa!, post.status!, index);
posts.sort(customCompare);
},
),
]
),
)
]
),
),
],
),
);
},
);
}