Flutter 在按下按钮时更改按钮的背景颜色和文本颜色,并在未按下按钮时将其设置回默认值
我是个新手。我有两个按钮在一行,我想改变颜色和文字的第一个按钮时,它被按下的其他东西,如果第二个按钮被按下,第一个按钮应该回到默认的颜色和第二个按钮应该去新的颜色。有没有办法通过索引来实现这一点Flutter 在按下按钮时更改按钮的背景颜色和文本颜色,并在未按下按钮时将其设置回默认值,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我是个新手。我有两个按钮在一行,我想改变颜色和文字的第一个按钮时,它被按下的其他东西,如果第二个按钮被按下,第一个按钮应该回到默认的颜色和第二个按钮应该去新的颜色。有没有办法通过索引来实现这一点 Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisSize: MainAxisSize.max, cros
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: primaryColor,
onPressed: () {},
child: Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Column(
children: [
Text('Stunning Solo', style: TextStyle(fontSize: 15,color: Colors.white)),
Text('Current Plan', style: TextStyle(fontSize: 12,color: Colors.white)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("\$$dollars3",style: TextStyle(fontSize: 20,color: Colors.white)),
),
Text(
"Free forever",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: whiteColor),
),
],
),
),
),
),
SizedBox(height: 20),
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: primaryColor,
onPressed:
() {},
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text('Startup Pack', style: TextStyle(fontSize: 15,color: Colors.white)),
),
Text('Introductory Offer', style: TextStyle(fontSize: 12,color: Colors.white)),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"\$$dollars",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
decoration: TextDecoration.lineThrough),
),
SizedBox(width: 5),
Text(
"\$$dollars2",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: whiteColor),
),
]),
行(
mainAxisAlignment:mainAxisAlignment.space,
mainAxisSize:mainAxisSize.max,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
大小盒子(
身高:150,
宽度:MediaQuery.of(context).size.width*0.45,
孩子:升起按钮(
颜色:原色,
按下:(){},
孩子:填充(
填充:仅限常量边集(顶部:20.0),
子:列(
儿童:[
文本(“绝妙独奏”,样式:TextStyle(字体大小:15,颜色:Colors.white)),
文本(“当前计划”,样式:TextStyle(字体大小:12,颜色:Colors.white)),
填充物(
填充:常数边集全部(8.0),
子项:文本(“\$$dollars3”,样式:TextStyle(fontSize:20,颜色:Colors.white)),
),
正文(
“永远自由”,
样式:TextStyle(
fontWeight:fontWeight.bold,
尺寸:13,
颜色:白色),
),
],
),
),
),
),
尺寸箱(高度:20),
大小盒子(
身高:150,
宽度:MediaQuery.of(context).size.width*0.45,
孩子:升起按钮(
颜色:原色,
按下按钮:
() {},
子:列(
儿童:[
填充物(
填充:仅限常量边集(顶部:20.0),
子项:文本('Startup Pack',样式:TextStyle(fontSize:15,颜色:Colors.white)),
),
Text('介绍性报价',样式:TextStyle(字体大小:12,颜色:Colors.white)),
填充物(
填充:常数边集全部(10.0),
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
mainAxisSize:mainAxisSize.max,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
正文(
“\$$美元”,
样式:TextStyle(
fontWeight:fontWeight.bold,
尺寸:20,
装饰:文本装饰。线条贯穿),
),
尺寸箱(宽度:5),
正文(
“\$$dollars2”,
样式:TextStyle(
fontWeight:fontWeight.bold,
尺寸:20,
颜色:白色),
),
]),
您可以为此使用切换按钮
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
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> {
List<bool> buttonsState = [
true,
false
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: ToggleButtons(
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
color: Colors.blue,
fillColor: Colors.blue,
isSelected: buttonsState,
children: <Widget>[
Padding(
padding: EdgeInsets.only(
left: 8,
right:8,
),
child: Text(
"Free forever", style: _getTextStyle(0),
textAlign: TextAlign.center,
),
),
Padding(
padding: EdgeInsets.only(
left: 8,
right: 8,
),
child: Text(
"Startup Pack", style: _getTextStyle(1),
textAlign: TextAlign.center,
),
),
],
onPressed: _updateButtonState,
borderRadius: BorderRadius.all(Radius.circular(8.0)),
),
),
);
}
_updateButtonState(int index) {
setState(() {
for (int buttonIndex = 0; buttonIndex < buttonsState.length; buttonIndex++) {
if (buttonIndex == index) {
buttonsState[buttonIndex] = true;
} else {
buttonsState[buttonIndex] = false;
}
}
});
}
TextStyle _getTextStyle(int index) {
return buttonsState[index]
? TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.white) : TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.blue);
}
}
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
列表按钮状态=[
是的,
假的
];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
//中心是一个布局小部件。它接受一个子元素并对其进行定位
/在父母的中间。
子:切换按钮(
highlightColor:Colors.transparent,
splashColor:Colors.transparent,
颜色:颜色,蓝色,
fillColor:Colors.blue,
当选:按钮状态,
儿童:[
填充物(
填充:仅限边缘设置(
左:8,,
右:8,,
),
子:文本(
“永远自由”,风格:_getTextStyle(0),
textAlign:textAlign.center,
),
),
填充物(
填充:仅限边缘设置(
左:8,,
右:8,,
),
中国