我在Flutter应用中使用CupertinoTabBar。 问题是在键盘出现时,bottomBar会显示,而应该不会。它是一个底部栏,应该始终位于底部。
我还没有找到实现此目的的方法或技巧。 也许有人可以引导我获得正确的行为。
这是我扑扑的医生的输出
医生摘要(要查看所有详细信息,请运行flutter doctor -v):
[✓] Flutter(Channel master,v1.6.1-pre.50,在Linux上,语言环境为en_US.UTF-8)
[✓] Android工具链-为Android设备开发(Android SDK版本28.0.3)
[✓] Android Studio(3.3版)
[✓] VS代码(1.33.1版)
[✓]已连接的设备(1个可用)
这是cupertinoTabBar唯一允许的选项:
const CupertinoTabBar({
Key key,
@required this.items,
this.onTap,
this.currentIndex = 0,
this.backgroundColor,
this.activeColor,
this.inactiveColor = CupertinoColors.inactiveGray,
this.iconSize = 30.0,
this.border = const Border(
top: BorderSide(
color: _kDefaultTabBarBorderColor,
width: 0.0, // One physical pixel.
style: BorderStyle.solid,
),
),
})
当键盘出现时,这是我的CupertinoTabBar上升的方式:
更新:
我试图验证键盘的状态,以不显示cupertinoTabBar,但它位于CupertinoTabScaffold内部:
return Scaffold(
body: CupertinoTabScaffold(
tabBuilder: (BuildContext context, int index) {
switch (index) {
case 0: // Home
return CupertinoTabView(
navigatorKey: Undoc3Keys.homeNavigator,
routes: getRoutes(context, store),
onGenerateRoute: handleRoutes,
builder: (BuildContext context) => FeedScreen(),
);
break;
case 1: // Preguntar
return CupertinoTabView(
navigatorKey: Undoc3Keys.askNavigator,
routes: getRoutes(context, store),
onGenerateRoute: handleRoutes,
builder: (BuildContext context) => SearchResultScreen(
arguments: {"askScreen": ""},
),
);
break;
case 2: // Perfil
return CupertinoTabView(
navigatorKey: Undoc3Keys.perfilNavigator,
routes: getRoutes(context, store),
onGenerateRoute: handleRoutes,
builder: (BuildContext context) => ProfileScreen(),
);
break;
default:
}
},
tabBar: Undoc3Keys.keyboardStatus // Here is validation of keyboard.
? CupertinoTabBar( // A try for making invisible bar.
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.radio_button_unchecked,
color: Colors.transparent,
),
backgroundColor: Colors.transparent),
BottomNavigationBarItem(
icon: Icon(
Icons.radio_button_unchecked,
color: Colors.transparent,
),
backgroundColor: Colors.transparent)
],
backgroundColor: Colors.transparent.withOpacity(0.0),
inactiveColor: Colors.transparent,
activeColor: Colors.transparent,
border: Border(
top: BorderSide(
color: Colors.transparent,
width: 0.0, // One physical pixel.
style: BorderStyle.none,
),
),
)
: _buildTabBar(),
),
);
这是如何显示键盘出现时的情况:
答案 0 :(得分:1)
如果将CupertinoTabBar
放在bottomNavigationBar
的属性Scaffold
内,则打开键盘后它应该能够自动消失,所以我假设无法根据您的情况进行操作。
首先,您必须了解是否未打开键盘。插件keyboard_visibility可以帮助您。
掌握这些信息后,您必须确定是否要显示CupertinoTabBar
。
class Foo extends StatefulWidget {
@override
_FooState createState() => _FooState();
}
class _FooState extends State<Foo> {
bool isKeyboardVisible;
@override
void initState() {
super.initState();
isKeyboardVisible = false;
KeyboardVisibilityNotification().addNewListener(
onChange: (bool isVisible) {
setState(() => isKeyboardVisible = isVisible);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextField(),
SizedBox(height: 50),
isKeyboardVisible ? SizedBox() : CupertinoTabBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.airline_seat_flat_angled),
title: Text('Item #1'),
),
BottomNavigationBarItem(
icon: Icon(Icons.airline_seat_flat),
title: Text('Item #2'),
),
BottomNavigationBarItem(
icon: Icon(Icons.airline_seat_individual_suite),
title: Text('Item #3'),
),
],
),
],
),
);
}
}
您的情况有点棘手。在这种情况下,我认为更好的选择是创建不可见的CupertinoTabBar
:
class InvisibleCupertinoTabBar extends CupertinoTabBar {
static const dummyIcon = Icon(IconData(0x0020));
InvisibleCupertinoTabBar()
: super(
items: [
BottomNavigationBarItem(icon: dummyIcon),
BottomNavigationBarItem(icon: dummyIcon),
],
);
@override
Size get preferredSize => const Size.square(0);
@override
Widget build(BuildContext context) => SizedBox();
@override
InvisibleCupertinoTabBar copyWith({
Key key,
List<BottomNavigationBarItem> items,
Color backgroundColor,
Color activeColor,
Color inactiveColor,
Size iconSize,
Border border,
int currentIndex,
ValueChanged<int> onTap,
}) => InvisibleCupertinoTabBar();
}
当键盘可见时,只需显示此小部件即可。
isKeyboardVisible ? InvisibleCupertinoTabBar() : CupertinoTabBar(
...
),