gpt4 book ai didi

flutter - 如何使 BottomNavigationBarItem 不可点击并禁用点击飞溅效果?

转载 作者:IT王子 更新时间:2023-10-29 06:57:13 26 4
gpt4 key购买 nike

我有包含 5 个项目的 BottomNavigationBar 和应该更改第 3 个项目“照片”的 FloatingActionButton。

所以我需要如果用户按下中央 BottomNavigationBarItem“照片”,它不会影响切换到此选项卡。

如何禁止点击特定的 BottomNavigationBarItem?

BottomNavigationBar with FloatingActionButton

这是我的代码:

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;
});
}
}

但并没有完全解决。当我点击照片标签时,它仍然显示点击飞溅效果。我可以禁用点击飞溅效果吗?

enter image description here

最佳答案

我遇到了和你一样的问题。在我的例子中,我只想将它应用到 bottomNavigationBar,因为我在另一个小部件上有飞溅效果。

我使用下面的代码修复了它:

bottomNavigationBar: Theme(
data: ThemeData(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
),
child: BottomNavigationBar(),
),

关于flutter - 如何使 BottomNavigationBarItem 不可点击并禁用点击飞溅效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55949824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com