英文:
how to eliminate the white space between DrawerHeader and ListTile
问题
要去除DrawerHeader
和ListTile
之间的空白空间,您可以尝试以下方法:
-
在
DrawerHeader
的child
属性中,将Column
的mainAxisAlignment
和crossAxisAlignment
都设置为MainAxisAlignment.center
和CrossAxisAlignment.center
,以确保内容在垂直和水平方向都居中对齐。 -
在
ListTile
之前或之后添加一个Divider
小部件,以创建分隔线,从而在视觉上分隔DrawerHeader
和ListTile
。
这些更改应该可以帮助您减少DrawerHeader
和ListTile
之间的空白空间。
英文:
how to remove the white space between DrawerHeader
and ListTile
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class NavDrawer extends StatelessWidget {
Map<int, String> myMap = {
0: '主頁',
1: '工作資訊',
2: '個人資料',
3: '關於我們',
4: '救生員需知',
5: '拯溺考試資訊',
6: '登入',
7: '登出',
8: '設置',
9: '意見',
};
final int selectedTileIndex;
final Function(int) onTileTap;
NavDrawer({required this.selectedTileIndex, required this.onTileTap});
//check user login
final bool signedIn = FirebaseAuth.instance.currentUser != null;
@override
Widget build(BuildContext context) {
return Drawer(
child: Column(
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS2R1n85q9_Brx6MlNSKHWFc49qwCFkYGKFsQt0x6uL&s'),
),
),
child: Center( // Updated
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CircleAvatar(
radius: 40,
backgroundImage: NetworkImage('https://i.guim.co.uk/img/media/26392d05302e02f7bf4eb143bb84c8097d09144b/446_167_3683_2210/master/3683.jpg?width=1200&quality=85&auto=format&fit=max&s=a52bbe202f57ac0f5ff7f47166906403'),
),
SizedBox(height: 10),
Text(
'name',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
SizedBox(height: 5),
Text(
'phone',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
fontSize: 13,
),
),
],
),
),
),
ListTile(
leading: Icon(Icons.home),
title: Text(myMap[0]!),
tileColor: selectedTileIndex == 0 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(0),
),
ListTile(
leading: Icon(Icons.work),
title: Text(myMap[1]!),
tileColor: selectedTileIndex == 1 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(1),
),
ListTile(
leading: Icon(Icons.person),
title: Text(myMap[2]!),
tileColor: selectedTileIndex == 2 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(2),
),
ListTile(
leading: Icon(Icons.info),
title: Text(myMap[3]!),
tileColor: selectedTileIndex == 3 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(3),
),
ListTile(
leading: Icon(Icons.warning),
title: Text(myMap[4]!),
tileColor: selectedTileIndex == 4 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(4),
),
ListTile(
leading: Icon(Icons.description),
title: Text(myMap[5]!),
tileColor: selectedTileIndex == 5 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(5),
),
signedIn?
ListTile(
leading: Icon(Icons.logout),
title: Text(myMap[7]!),
onTap: () => onTileTap(7),
)
: ListTile(
leading: Icon(Icons.login),
title: Text(myMap[6]!),
onTap: () => onTileTap(6),
),
Expanded(
child: Container(),
),
ListTile(
leading: Icon(Icons.settings),
title: Text(myMap[8]!),
tileColor: selectedTileIndex == 8 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(8),
),
ListTile(
leading: Icon(Icons.border_color),
title: Text(myMap[9]!),
tileColor: selectedTileIndex == 9 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(9),
),
],
),
);
}
}
答案1
得分: 0
在DrawerHeader
中有一个margin
属性,默认值为EdgeInsets.only(bottom: 8)
,所以你只需分配:
margin: EdgeInsets.zero,
以下是更新后的代码:
class NavDrawer extends StatelessWidget {
Map<int, String> myMap = {
0: '主頁',
1: '工作資訊',
2: '個人資料',
3: '關於我們',
4: '救生員需知',
5: '拯溺考試資訊',
6: '登入',
7: '登出',
8: '設置',
9: '意見',
};
final int selectedTileIndex;
final Function(int) onTileTap;
NavDrawer({required this.selectedTileIndex, required this.onTileTap});
//检查用户是否已登录
final bool signedIn = true;
@override
Widget build(BuildContext context) {
return Drawer(
child: Column(
children: <Widget>[
DrawerHeader(
margin: EdgeInsets.zero,
decoration: BoxDecoration(
color: Colors.blue,
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS2R1n85q9_Brx6MlNSKHWFc49qwCFkYGKFsQt0x6uL&s'),
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CircleAvatar(
radius: 40,
backgroundImage: NetworkImage(
'https://i.guim.co.uk/img/media/26392d05302e02f7bf4eb143bb84c8097d09144b/446_167_3683_2210/master/3683.jpg?width=1200&quality=85&auto=format&fit=max&s=a52bbe202f57ac0f5ff7f47166906403'),
),
SizedBox(height: 10),
Text(
'name',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
SizedBox(height: 5),
Text(
'phone',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
fontSize: 13,
),
),
],
),
),
),
ListTile(
leading: Icon(Icons.home),
title: Text(myMap[0]!),
tileColor:
selectedTileIndex == 0 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(0),
),
ListTile(
leading: Icon(Icons.work),
title: Text(myMap[1]!),
tileColor:
selectedTileIndex == 1 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(1),
),
ListTile(
leading: Icon(Icons.person),
title: Text(myMap[2]!),
tileColor:
selectedTileIndex == 2 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(2),
),
ListTile(
leading: Icon(Icons.info),
title: Text(myMap[3]!),
tileColor:
selectedTileIndex == 3 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(3),
),
ListTile(
leading: Icon(Icons.warning),
title: Text(myMap[4]!),
tileColor:
selectedTileIndex == 4 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(4),
),
ListTile(
leading: Icon(Icons.description),
title: Text(myMap[5]!),
tileColor:
selectedTileIndex == 5 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(5),
),
signedIn
? ListTile(
leading: Icon(Icons.logout),
title: Text(myMap[7]!),
onTap: () => onTileTap(7),
)
: ListTile(
leading: Icon(Icons.login),
title: Text(myMap[6]!),
onTap: () => onTileTap(6),
),
Expanded(
child: Container(),
),
ListTile(
leading: Icon(Icons.settings),
title: Text(myMap[8]!),
tileColor:
selectedTileIndex == 8 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(8),
),
ListTile(
leading: Icon(Icons.border_color),
title: Text(myMap[9]!),
tileColor:
selectedTileIndex == 9 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(9),
),
],
),
);
}
}
在线检查您的代码:
Zapp.run
提示: 在使用任何小部件之前,务必确保并检查属性,如果在#flutter中遇到任何问题,请专门研究该小部件。
英文:
There's property margin
in DrawerHeader
which value is by default `
> EdgeInsetsGeometry? margin = const EdgeInsets.only(bottom: 8)
So, you've to just assign
> margin: EdgeInsets.zero,
`Below is the updated code:
class NavDrawer extends StatelessWidget {
Map<int, String> myMap = {
0: '主頁',
1: '工作資訊',
2: '個人資料',
3: '關於我們',
4: '救生員需知',
5: '拯溺考試資訊',
6: '登入',
7: '登出',
8: '設置',
9: '意見',
};
final int selectedTileIndex;
final Function(int) onTileTap;
NavDrawer({required this.selectedTileIndex, required this.onTileTap});
//check user login
final bool signedIn = true;
@override
Widget build(BuildContext context) {
return Drawer(
child: Column(
children: <Widget>[
DrawerHeader(
margin: EdgeInsets.zero,
decoration: BoxDecoration(
color: Colors.blue,
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS2R1n85q9_Brx6MlNSKHWFc49qwCFkYGKFsQt0x6uL&s'),
),
),
child: Center(
// Updated
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CircleAvatar(
radius: 40,
backgroundImage: NetworkImage(
'https://i.guim.co.uk/img/media/26392d05302e02f7bf4eb143bb84c8097d09144b/446_167_3683_2210/master/3683.jpg?width=1200&quality=85&auto=format&fit=max&s=a52bbe202f57ac0f5ff7f47166906403'),
),
SizedBox(height: 10),
Text(
'name',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
SizedBox(height: 5),
Text(
'phone',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
fontSize: 13,
),
),
],
),
),
),
ListTile(
leading: Icon(Icons.home),
title: Text(myMap[0]!),
tileColor:
selectedTileIndex == 0 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(0),
),
ListTile(
leading: Icon(Icons.work),
title: Text(myMap[1]!),
tileColor:
selectedTileIndex == 1 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(1),
),
ListTile(
leading: Icon(Icons.person),
title: Text(myMap[2]!),
tileColor:
selectedTileIndex == 2 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(2),
),
ListTile(
leading: Icon(Icons.info),
title: Text(myMap[3]!),
tileColor:
selectedTileIndex == 3 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(3),
),
ListTile(
leading: Icon(Icons.warning),
title: Text(myMap[4]!),
tileColor:
selectedTileIndex == 4 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(4),
),
ListTile(
leading: Icon(Icons.description),
title: Text(myMap[5]!),
tileColor:
selectedTileIndex == 5 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(5),
),
signedIn
? ListTile(
leading: Icon(Icons.logout),
title: Text(myMap[7]!),
onTap: () => onTileTap(7),
)
: ListTile(
leading: Icon(Icons.login),
title: Text(myMap[6]!),
onTap: () => onTileTap(6),
),
Expanded(
child: Container(),
),
ListTile(
leading: Icon(Icons.settings),
title: Text(myMap[8]!),
tileColor:
selectedTileIndex == 8 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(8),
),
ListTile(
leading: Icon(Icons.border_color),
title: Text(myMap[9]!),
tileColor:
selectedTileIndex == 9 ? Colors.blue.withOpacity(0.5) : null,
onTap: () => onTileTap(9),
),
],
),
);
}
}
Check Online Your Code:
Zapp.run
Tip: Always make sure and check the properties before using any widget and specifically study that widget if you're facing any issue in #flutter
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论