Flutter OutlinedButton:设置禁用颜色
Flutter OutlinedButton: set disabled color
在我的 Flutter 应用程序中,我有一个带圆角的 OutlinedButton:
OutlinedButton(
child: Text(
"a label",
),
onPressed: () {
// do something
},
style: OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: MyColor.colorAccent,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
)
现在我需要为禁用状态设置按钮样式,但我不需要如何知道这个小部件(在 EevatedButton 上我使用 MaterialStateProperty.resolveWith 作为backgroundColor,但不适合 OutlinedButton)
这不是最佳做法,但您可以这样做
bool? _isdisabled ;
//null check , if u are on an older version of dart of you want to declare it just do this :
bool _isdisabled = false;
OutlinedButton(
child: Text(
"a label",
),
onPressed: () {
// do something
},
style:_isdisabled==false? OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: MyColor.colorAccent,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
): //another style ,
)
我不知道这个代码示例是否可以帮助回答您的问题,先生。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool isEnableButton = true;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: Container(
child: OutlinedButton(
child: Text(
"a label",
),
onPressed: () {
setState(() {
if (isEnableButton) {
isEnableButton = false;
} else {
isEnableButton = true;
}
});
},
style:
isEnableButton ? enableButtonStyle() : disableButtonStyle(),
),
),
),
),
);
}
dynamic disableButtonStyle() {
return OutlinedButton.styleFrom(
primary: Colors.black,
backgroundColor: Colors.grey,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
);
}
dynamic enableButtonStyle() {
return OutlinedButton.styleFrom(
primary: Colors.black,
backgroundColor: Colors.green,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
);
}
}
在我的 Flutter 应用程序中,我有一个带圆角的 OutlinedButton:
OutlinedButton(
child: Text(
"a label",
),
onPressed: () {
// do something
},
style: OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: MyColor.colorAccent,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
)
现在我需要为禁用状态设置按钮样式,但我不需要如何知道这个小部件(在 EevatedButton 上我使用 MaterialStateProperty.resolveWith 作为backgroundColor,但不适合 OutlinedButton)
这不是最佳做法,但您可以这样做
bool? _isdisabled ;
//null check , if u are on an older version of dart of you want to declare it just do this :
bool _isdisabled = false;
OutlinedButton(
child: Text(
"a label",
),
onPressed: () {
// do something
},
style:_isdisabled==false? OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: MyColor.colorAccent,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
): //another style ,
)
我不知道这个代码示例是否可以帮助回答您的问题,先生。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool isEnableButton = true;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: Container(
child: OutlinedButton(
child: Text(
"a label",
),
onPressed: () {
setState(() {
if (isEnableButton) {
isEnableButton = false;
} else {
isEnableButton = true;
}
});
},
style:
isEnableButton ? enableButtonStyle() : disableButtonStyle(),
),
),
),
),
);
}
dynamic disableButtonStyle() {
return OutlinedButton.styleFrom(
primary: Colors.black,
backgroundColor: Colors.grey,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
);
}
dynamic enableButtonStyle() {
return OutlinedButton.styleFrom(
primary: Colors.black,
backgroundColor: Colors.green,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
);
}
}