作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有包含 5 个项目的 BottomNavigationBar 和应该更改第 3 个项目“照片”的 FloatingActionButton。
所以我需要如果用户按下中央 BottomNavigationBarItem“照片”,它不会影响切换到此选项卡。
如何禁止点击特定的 BottomNavigationBarItem?
这是我的代码:
import 'package:flutter/material.dart';
class MainPage extends StatefulWidget {
@override
_MainPageState createState() => new _MainPageState();
}
class PageInfoData {
final String title;
final IconData iconData;
const PageInfoData(this.title, this.iconData);
}
class _MainPageState extends State<MainPage> {
int _selectedIndex = 0;
static const List<PageInfoData> pageInfo = [
PageInfoData('History', Icons.swap_horiz),
PageInfoData('Download', Icons.file_download),
PageInfoData('Photo ', null),
PageInfoData('Upload', Icons.file_upload),
PageInfoData('Settings', Icons.settings)
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text("Menu"),
actions: <Widget>[IconButton(icon: Icon(Icons.search, color: Colors.white,),)],
),
body: Center(child: Text('Index$_selectedIndex: ${pageInfo[_selectedIndex].title}')),
backgroundColor: Colors.white,
bottomNavigationBar: BottomNavigationBar(
items: new List<BottomNavigationBarItem>.generate(pageInfo.length, (int index) {
PageInfoData curPageInfo = pageInfo[index];
return BottomNavigationBarItem(icon: Icon(curPageInfo.iconData, size: 30.0), title: Text(curPageInfo.title, style: TextStyle(fontSize: 9.0),));
}),
type: BottomNavigationBarType.fixed,
unselectedItemColor: const Color(0xff797979),
fixedColor: Theme.of(context).primaryColor,
backgroundColor: const Color(0xffe2e2e2),
currentIndex: _selectedIndex,
showUnselectedLabels: true,
onTap: _onItemTapped,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
backgroundColor: Color(0xffa2a5a4),
child: Icon(Icons.photo_camera, size: 40.0,),
onPressed: null),
);
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
}
更新:
通过更改 _onItemTapped
函数,问题几乎解决了(谢谢 @ibhavikmakwana)
void _onItemTapped(int index) {
if (index != 2) {
setState(() {
_selectedIndex = index;
});
}
}
但并没有完全解决。当我点击照片标签时,它仍然显示点击飞溅效果。我可以禁用点击飞溅效果吗?
最佳答案
我遇到了和你一样的问题。在我的例子中,我只想将它应用到 bottomNavigationBar,因为我在另一个小部件上有飞溅效果。
我使用下面的代码修复了它:
bottomNavigationBar: Theme(
data: ThemeData(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
),
child: BottomNavigationBar(),
),
关于flutter - 如何使 BottomNavigationBarItem 不可点击并禁用点击飞溅效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55949824/
我是一名优秀的程序员,十分优秀!