如何禁用按钮(图标按钮)点击?
How can I disable Button(IconButton) click?
我尝试将 null 分配给点击功能,但禁用颜色不起作用。我试过这个代码部分。
IconButton(
disabledColor: Colors.redAccent,
icon: Icon(
Icons.hdr_enhanced_select,
color: Colors.white,
),
iconSize: 30,
onPressed: selectionIsActive
? null
: () {
setState(() {
selectionIsActive = true;
});
}),
Icon/Color 覆盖 disabledColor 属性。在 IconButton
的情况下使用 color
属性作为按钮颜色:
IconButton(
disabledColor: Colors.redAccent,
color: Colors.white,
icon: Icon(
Icons.hdr_enhanced_select
),
iconSize: 30,
onPressed: selectionIsActive
? null
: () {
setState(() {
selectionIsActive = true;
});
}),
您可以复制粘贴 运行 下面的完整代码
您可以在Icon
的color
中添加条件color: selectionIsActive ? null: Colors.white
代码片段
IconButton(
disabledColor: Colors.redAccent,
icon: Icon(
Icons.hdr_enhanced_select,
color: selectionIsActive ? null: Colors.white,
),
iconSize: 30,
onPressed: selectionIsActive
? null
: () {
setState(() {
selectionIsActive = true;
});
}),
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool selectionIsActive = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
disabledColor: Colors.redAccent,
icon: Icon(
Icons.hdr_enhanced_select,
color: selectionIsActive ? null: Colors.white,
),
iconSize: 30,
onPressed: selectionIsActive
? null
: () {
setState(() {
selectionIsActive = true;
});
}),
],
),
),
);
}
}
我尝试将 null 分配给点击功能,但禁用颜色不起作用。我试过这个代码部分。
IconButton(
disabledColor: Colors.redAccent,
icon: Icon(
Icons.hdr_enhanced_select,
color: Colors.white,
),
iconSize: 30,
onPressed: selectionIsActive
? null
: () {
setState(() {
selectionIsActive = true;
});
}),
Icon/Color 覆盖 disabledColor 属性。在 IconButton
的情况下使用 color
属性作为按钮颜色:
IconButton(
disabledColor: Colors.redAccent,
color: Colors.white,
icon: Icon(
Icons.hdr_enhanced_select
),
iconSize: 30,
onPressed: selectionIsActive
? null
: () {
setState(() {
selectionIsActive = true;
});
}),
您可以复制粘贴 运行 下面的完整代码
您可以在Icon
的color
中添加条件color: selectionIsActive ? null: Colors.white
代码片段
IconButton(
disabledColor: Colors.redAccent,
icon: Icon(
Icons.hdr_enhanced_select,
color: selectionIsActive ? null: Colors.white,
),
iconSize: 30,
onPressed: selectionIsActive
? null
: () {
setState(() {
selectionIsActive = true;
});
}),
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool selectionIsActive = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
disabledColor: Colors.redAccent,
icon: Icon(
Icons.hdr_enhanced_select,
color: selectionIsActive ? null: Colors.white,
),
iconSize: 30,
onPressed: selectionIsActive
? null
: () {
setState(() {
selectionIsActive = true;
});
}),
],
),
),
);
}
}