【发布时间】:2021-08-06 08:27:21
【问题描述】:
我仍然是颤振的初学者,我正在尝试制作一个小应用程序以允许用户将单位从 SI 转换为 IP 单位......我使用了ToggleButton,作为一次尝试ToggleButton切换页面底部的小文本必须更改,但没有发生任何事情,任何人都可以帮助我做错了什么......代码是
class ToggleButtonPage extends StatefulWidget {
const ToggleButtonPage({Key key}) : super(key: key);
@override
_ToggleButtonPageState createState() => _ToggleButtonPageState();
}
class _ToggleButtonPageState extends State<ToggleButtonPage> {
bool siUnits;
String siUnitsText;
String showText;
List<bool> isSelected2 = [true, false];
String textShow(String siUnitsText) {
if (siUnitsText == 'true') {
showText = 'ooooooo';
} else {
showText = 'xxx';
print('xxx');
}
return showText;
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
margin: EdgeInsets.only(top: 50.0),
child: ToggleButtons(
fillColor: Colors.lightGreen,
borderWidth: 2,
borderColor: Colors.blueGrey,
selectedBorderColor: Colors.black,
borderRadius: BorderRadius.circular(10),
children: [
Text(
'SI',
style: TextStyle(
fontSize: 15,
),
),
Text(
'IP',
style: TextStyle(
fontSize: 15,
),
),
],
onPressed: (int newIndex) {
setState(() {
switch (newIndex) {
case 0:
siUnitsText = 'true';
break;
case 1:
siUnitsText = 'false';
break;
}
for (int index = 0; index < isSelected2.length; index++) {
if (index == newIndex) {
isSelected2[index] = true;
} else {
isSelected2[index] = false;
}
}
});
print(siUnitsText);
},
isSelected: isSelected2,
),
),
Text('Value is equal to $showText'),
],
),
);
}
}
【问题讨论】:
标签: flutter togglebutton