gpt4 book ai didi

android - Flutter - 多个图标改变了每个图标的颜色

转载 作者:行者123 更新时间:2023-11-29 11:26:48 25 4
gpt4 key购买 nike

我的应用程序底部栏中有 4 个图标,我需要将图标的颜色从灰色更改为白色,我遵循了这个 question ,但是当我按下任何图标时,所有图标同时改变颜色,我需要按下例如(主页)图标时只需按下(主页)图标。

我该怎么做?

以及如何在不按下的情况下将主页图标设置为始终在第一页? (我的意思是当用户打开应用程序时向他显示主页(白色图标))。

代码:

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

_HomePageState();

@override
void initState() {
// TODO: implement initState
super.initState();
}
bool isPressed = false;

_pressed() {
var newVal = true;
if(isPressed) {
newVal = false;
} else {
newVal = true;
}

setState((){
isPressed = newVal;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(

appBar: AppBar(
title: Image.asset('assets/logo.png', fit: BoxFit.cover,),
centerTitle: true,
backgroundColor: Colors.grey[900],
),
bottomNavigationBar: BottomAppBar(
color: Colors.grey[900],
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(icon: Icon(Icons.info), color: isPressed ? Colors.white:Colors.grey[600], iconSize: 30, onPressed: () {
setState(() {
_pressed();
});
}),
IconButton(icon: Icon(Icons.local_mall), color: isPressed ? Colors.white:Colors.grey[600], iconSize: 30, onPressed: () {
setState(() {
_pressed();
});
}),
IconButton(icon: Icon(Icons.bookmark), color: isPressed ? Colors.white:Colors.grey[600], iconSize: 30, onPressed: () {
setState(() {
_pressed();
});
}),
IconButton(icon: Icon(Icons.home), color: isPressed ? Colors.white:Colors.grey[600], iconSize: 30, onPressed: () {
setState(() {
_pressed();
});
}),
],
),
),

);
}
}

最佳答案

你可以这样做!!真的是切换图标颜色的最佳技巧

   IconButton( icon:  ImageIcon(AssetImage("images/edit.png",) ,size: 30,),
disabledColor: Color.fromRGBO(32, 127, 195, 1),
color: Colors.white,
onPressed: a == 1
? null
: () => setState(() => a = 1)),
IconButton( icon: ImageIcon(AssetImage("images/notify.png",) ,size: 30,),
disabledColor: Color.fromRGBO(32, 127, 195, 1),
color: Colors.white,
onPressed: a == 2
? null
: () => setState(() {
a = 2;
dateTemp = '';
})),

关于android - Flutter - 多个图标改变了每个图标的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58000073/

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