Flutter 单击2~3个切换按钮时如何更改图像
单击2~3个切换按钮时如何更改图像 这就是我想做的。 9示例中包含的CAT图片 我想加载cat图像(总共9个cat图像)//单击room1按钮,cat1按钮->加载cat1图像//room1按钮,CAT2按钮->加载cat 2图像。。。单击room3,CAT3按钮->加载cat 9图像 在这里输入代码Flutter 单击2~3个切换按钮时如何更改图像,flutter,dart,toggle,togglebutton,image-load,Flutter,Dart,Toggle,Togglebutton,Image Load,单击2~3个切换按钮时如何更改图像 这就是我想做的。 9示例中包含的CAT图片 我想加载cat图像(总共9个cat图像)//单击room1按钮,cat1按钮->加载cat1图像//room1按钮,CAT2按钮->加载cat 2图像。。。单击room3,CAT3按钮->加载cat 9图像 在这里输入代码 import '123.dart'; import '456.dart'; void main() { runApp(MyApp()); } class MyApp extends Sta
import '123.dart';
import '456.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('imageToggle'),),
body: Column(
children: [
Expanded(child: Image.asset('images/meow.jpg')),
Button123(),
Button456(),
],
),
),
);
}
}
import'123.dart';
进口“456.dart”;
void main(){
runApp(MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(标题:文本('imageToggle'),),
正文:专栏(
儿童:[
已展开(子项:Image.asset('images/meow.jpg')),
按钮123(),
按钮456(),
],
),
),
);
}
}
类按钮123扩展StatefulWidget{
@凌驾
_Button123State createState()=>_Button123State();
}
类_按钮123状态扩展状态{
List isSelected=List.generate(3,(索引)=>false);
@凌驾
小部件构建(构建上下文){
返回容器(
身高:40,
子:切换按钮(
当选:当选,
颜色:颜色,黑色,
填充颜色:颜色。灰色,
儿童:[
填充(填充:常量边集。对称(水平:2),
子项:文本('room1'),),
填充(填充:常量边集。对称(水平:2),
子项:文本('room2'),),
填充(填充:常量边集。对称(水平:2),
子项:文本('room3'),),
],
onPressed:(int newIndex){
设置状态(){
for(int index=0;index
仔细检查您的问题图像未显示我无法加载图像..将图像放入列表中,然后清除a int var=0,然后将其与资产图像的索引一起使用,然后将状态设置为索引
class Button123 extends StatefulWidget {
@override
_Button123State createState() => _Button123State();
}
class _Button123State extends State<Button123> {
List<bool> isSelected = List.generate(3, (index) => false);
@override
Widget build(BuildContext context) {
return Container(
height: 40,
child: ToggleButtons(
isSelected: isSelected,
color: Colors.black,
fillColor: Colors.grey,
children: [
Padding(padding: const EdgeInsets.symmetric(horizontal: 2),
child: Text('room1'),),
Padding(padding: const EdgeInsets.symmetric(horizontal: 2),
child: Text('room2'),),
Padding(padding: const EdgeInsets.symmetric(horizontal: 2),
child: Text('room3'),),
],
onPressed: (int newIndex) {
setState(() {
for (int index = 0; index < isSelected.length; index++) {
if (index == newIndex) {
isSelected[index] = true;
} else {
isSelected[index] = false;
}
}
});
},
)
);
}
}