如何在flutter中忽略特定小部件上的布局?



我遇到了一个小问题,每当一个小部件激活时,底部导航栏中的小部件就会掉下来。这是因为每次图标激活时,我都希望在图标下方有一个小点

正如你所看到的,(这张截图是在相同的位置拍摄的)图标变得更高了一点。我只是在设置图标下面放了一个点来说明这个问题。

输入图片描述

输入图片描述

下面是底部导航栏小部件的代码:
return BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
items: <BottomNavigationBarItem>[
const BottomNavigationBarItem(
icon: Icon(MaterialSymbols.home_outlined),
activeIcon: Icon(MaterialSymbols.home_filled_outlined),
label: 'Home',
),
const BottomNavigationBarItem(
icon: Icon(Icons.quiz_outlined),
activeIcon: Icon(Icons.quiz),
label: 'Business',
),
BottomNavigationBarItem(
icon: const Icon(Icons.settings_outlined),
activeIcon: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: const [
Icon(Icons.settings),
Icon(
Icons.circle,
size: 5,
)
],
),
label: 'School',
),
],
backgroundColor: Colors.transparent,
selectedItemColor: const Color(0xff00B2FF),
unselectedItemColor: const Color(0xff0171A1),
elevation: 0.0,
currentIndex: _selectedIndex,
onTap: _onItemTapped,
);

我已经尝试了很多东西,比如添加填充,但没有任何东西适合我这里。我只是一个初学者,所以在这个话题上我不是很先进。

由于BottomNavigationBar中的icon参数可以是任何小部件,因此使其成为满足您需求的自定义小部件。你有Icon()作为iconColumn(),有两个子activeIcon。它们有不同的高度,因此你的图标是跳跃的。考虑一下这个Widget,它复制了您的活动图标,但是使用了"。"如果项目未被选中,则图标下方为透明:

class NavBarIcon extends StatelessWidget {
final Icon icon;
final bool isActive;
const NavBarIcon({required this.icon, required this.isActive});
@override
Widget build(BuildContext context) {
return Column(children: [
icon,
Icon(
Icons.circle,
size: 5,
color: isActive ? null : Colors.transparent,
)
]);
}
}

和可以在Dartpad中运行的完整代码片段:

import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const List<Widget> _widgetOptions = <Widget>[
Text('Index 0'),
Text('Index 1'),
Text('Index 2'),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: NavBarIcon(
isActive: _selectedIndex == 0, icon: const Icon(Icons.home)),
label: 'Home',
),
BottomNavigationBarItem(
icon: NavBarIcon(
isActive: _selectedIndex == 1,
icon: const Icon(Icons.quiz_outlined)),
label: 'Business',
),
BottomNavigationBarItem(
icon: NavBarIcon(
isActive: _selectedIndex == 2,
icon: const Icon(Icons.settings)),
label: 'School',
),
],
backgroundColor: Colors.transparent,
selectedItemColor: const Color(0xff00B2FF),
unselectedItemColor: const Color(0xff0171A1),
elevation: 0.0,
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
}
}
class NavBarIcon extends StatelessWidget {
final Icon icon;
final bool isActive;
const NavBarIcon({required this.icon, required this.isActive});
@override
Widget build(BuildContext context) {
return Column(children: [
icon,
Icon(
Icons.circle,
size: 5,
color: isActive ? null : Colors.transparent,
)
]);
}
}

最新更新