flutter - 如何通过单击不同的按钮向 ListView.builder 添加不同的元素?
问题描述
有 3 个按钮,当我单击这些按钮时,我希望它向 ListView.builder 添加不同的元素。
示例:点击红色按钮并添加一个红色开关按钮,我希望蓝色按钮添加一个蓝色开关按钮。
我编写的代码中的错误是:当我单击按钮时,它们的颜色都相同。我只想改变最后添加元素的颜色。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
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> {
int counter = 0;
int lc = 0;
int buttonMode = 0;
List<Widget> wid = [];
List<bool> mode = List(50);
void incrementCounter() {
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
child: ListView.builder(
itemCount: lc,
itemBuilder: (ctx, inx) => sw(inx: inx, m: buttonMode),
)),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
RaisedButton(
color: Colors.blue,
onPressed: () {
setState(() {
buttonMode = 0;
lc++;
});
},
),
RaisedButton(
color: Colors.green,
onPressed: () {
setState(() {
buttonMode = 1;
lc++;
});
},
),
RaisedButton(
color: Colors.orange,
onPressed: () {
setState(() {
buttonMode = 2;
lc++;
});
},
),
],
),
);
}
Widget sw({int inx, int m = 0}) {
return buttonMode == 0
? Switch(
onChanged: (d) {
setState(() {
mode[inx] = d;
});
print("$inx. index = $d");
},
value: mode[inx] != null ? mode[inx] : false,
)
: buttonMode == 1
? Switch(
activeTrackColor: Colors.lightGreenAccent,
activeColor: Colors.green,
onChanged: (d) {
setState(() {
mode[inx] = d;
});
print("$inx. index = $d");
},
value: mode[inx] != null ? mode[inx] : false,
)
: Switch(
activeTrackColor: Colors.orangeAccent,
activeColor: Colors.orange,
onChanged: (d) {
setState(() {
mode[inx] = d;
});
print("$inx. index = $d");
},
value: mode[inx] != null ? mode[inx] : false,
);
}
}
解决方案
您需要将 添加buttonModes
到类内的列表_MyHomePageState
并修改sw
函数:
class _MyHomePageState extends State<MyHomePage> {
int counter = 0;
int lc = 0;
int buttonMode = 0;
List<Widget> wid = [];
List<bool> mode = List(50);
void incrementCounter() {
setState(() {
counter++;
});
}
List<int> buttonModeList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
child: ListView.builder(
itemCount: lc,
itemBuilder: (ctx, inx) => sw(inx: inx, m: buttonModeList[inx]),
)),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
RaisedButton(
color: Colors.blue,
onPressed: () {
setState(() {
buttonModeList.add(0);
lc++;
});
},
),
RaisedButton(
color: Colors.green,
onPressed: () {
setState(() {
buttonModeList.add(1);
lc++;
});
},
),
RaisedButton(
color: Colors.orange,
onPressed: () {
setState(() {
buttonModeList.add(2);
lc++;
});
},
),
],
),
);
}
Widget sw({int inx, int m = 0}) {
return m == 0
? Switch(
onChanged: (d) {
setState(() {
mode[inx] = d;
});
print("$inx. index = $d");
},
value: mode[inx] != null ? mode[inx] : false,
)
: m == 1
? Switch(
activeTrackColor: Colors.lightGreenAccent,
activeColor: Colors.green,
onChanged: (d) {
setState(() {
mode[inx] = d;
});
print("$inx. index = $d");
},
value: mode[inx] != null ? mode[inx] : false,
)
: Switch(
activeTrackColor: Colors.orangeAccent,
activeColor: Colors.orange,
onChanged: (d) {
setState(() {
mode[inx] = d;
});
print("$inx. index = $d");
},
value: mode[inx] != null ? mode[inx] : false,
);
}
}
推荐阅读
- python - 在类别数组上细分ndarray
- java - Java Socket 不允许摆动框架出现
- java - 抛出异常以强制退出递归有效吗?
- visual-studio-code - 在 VS Code 中创建新文件的替代方法?
- postgresql - 首先尝试更新的 INSERT ... ON CONFLICT 相当于什么?
- prism - Fluent.Ribbon 的 RegionAdapter
- python - 如何修复部署在 AWS EC2 中但 CSS 和照片不起作用的 Django 项目
- python - PyTorch 数据集:ImageFolder 和子文件夹过滤
- javascript - 为什么值为空?
- javascript - 如何让 MongoDB 将数字识别为双精度数?