Flutter BottomNavigationBar 在单独文件中被视为子小部件时不可点击
Flutter BottomNavigationBar not clickable when treated as a child widget in a separate file
我正在开发这个 flutter 应用程序,并试图分离一些代码以使事情变得整洁。所以我有一个主页,其中包含一个带有标签栏的应用栏(完美运行)、标签视图主体(正常运行)和底部导航栏(不工作:不知何故不可点击)。
我的footBar.dart:
import 'package:flutter/material.dart';
class FootBar extends StatefulWidget {
@override
_FootBarState createState() => _FootBarState();
}
class _FootBarState extends State<FootBar> {
int _currentIndex = 0;
Widget build(BuildContext context) {
return Container(
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
backgroundColor: Colors.white,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.black.withOpacity(.3),
selectedFontSize: 14,
unselectedFontSize: 14,
showSelectedLabels: false,
showUnselectedLabels: false,
onTap: (value) {
// Respond to item press.
setState(() => _currentIndex = value);
},
items: [
BottomNavigationBarItem(
label: '',
icon: Text('a'),
),
BottomNavigationBarItem(
label: '',
icon: Text('b'),
),
BottomNavigationBarItem(
label: '',
icon: Text('c'),
),
BottomNavigationBarItem(
label: '',
icon: Text('d'),
),
],
),
);
}
}
我在 homepage.dart 中导入它是这样的:
import 'package:flutter/material.dart';
import 'appbar/appBar.dart';
import 'footbar/footBar.dart';
void main() => runApp(HomePage());
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: TopAppBar(),
body: TabBarView(
children: [
Center(child: Text('DOGS')),
Center(child: Text('CATS')),
],
),
bottomNavigationBar: FootBar(),
),
);
}
}
感谢任何帮助。提前谢谢大家!
好的,我刚刚发现出了什么问题。所以在我的 footBar.dart 文件中,我不应该有这个:
showSelectedLabels: false,
showUnselectedLabels: false,
我把这两行注释掉,然后把文字放到label
,然后用0.0高度的容器把图标隐藏起来(icons: Container(height: 0.0,),
),终于如愿以偿
我正在开发这个 flutter 应用程序,并试图分离一些代码以使事情变得整洁。所以我有一个主页,其中包含一个带有标签栏的应用栏(完美运行)、标签视图主体(正常运行)和底部导航栏(不工作:不知何故不可点击)。
我的footBar.dart:
import 'package:flutter/material.dart';
class FootBar extends StatefulWidget {
@override
_FootBarState createState() => _FootBarState();
}
class _FootBarState extends State<FootBar> {
int _currentIndex = 0;
Widget build(BuildContext context) {
return Container(
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _currentIndex,
backgroundColor: Colors.white,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.black.withOpacity(.3),
selectedFontSize: 14,
unselectedFontSize: 14,
showSelectedLabels: false,
showUnselectedLabels: false,
onTap: (value) {
// Respond to item press.
setState(() => _currentIndex = value);
},
items: [
BottomNavigationBarItem(
label: '',
icon: Text('a'),
),
BottomNavigationBarItem(
label: '',
icon: Text('b'),
),
BottomNavigationBarItem(
label: '',
icon: Text('c'),
),
BottomNavigationBarItem(
label: '',
icon: Text('d'),
),
],
),
);
}
}
我在 homepage.dart 中导入它是这样的:
import 'package:flutter/material.dart';
import 'appbar/appBar.dart';
import 'footbar/footBar.dart';
void main() => runApp(HomePage());
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: TopAppBar(),
body: TabBarView(
children: [
Center(child: Text('DOGS')),
Center(child: Text('CATS')),
],
),
bottomNavigationBar: FootBar(),
),
);
}
}
感谢任何帮助。提前谢谢大家!
好的,我刚刚发现出了什么问题。所以在我的 footBar.dart 文件中,我不应该有这个:
showSelectedLabels: false,
showUnselectedLabels: false,
我把这两行注释掉,然后把文字放到label
,然后用0.0高度的容器把图标隐藏起来(icons: Container(height: 0.0,),
),终于如愿以偿