gpt4 book ai didi

Flutter ToggleButton 类 - Flutter 1.9.1

转载 作者:行者123 更新时间:2023-12-01 16:40:21 25 4
gpt4 key购买 nike

我目前正在学习 Flutter 并取得了良好的进展,所以如果这是一个菜鸟问题,请耐心等待。

Flutter 最近更新到 1.9.1,随之而来的是新的小部件 ToggleButton 类;

这正是我所追求的,所以我在代码中实现了该小部件,如下所示

var isSelected1 = [false, true];
var isSelected2 = [false, true];

ToggleButtons(
borderColor: Colors.black,
fillColor: Colors.grey,
borderWidth: 2,
selectedBorderColor: Colors.black,
selectedColor: Colors.white,
borderRadius: BorderRadius.circular(0),
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Open 24 Hours',
style: TextStyle(fontSize: 16),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Custom Hours',
style: TextStyle(fontSize: 16),
),
),
],
onPressed: (int index) {
setState(() {
for (int buttonIndex = 0;
buttonIndex < isSelected2.length;
buttonIndex++) {
if (buttonIndex == index) {
isSelected2[buttonIndex] = true;
} else {
isSelected2[buttonIndex] = false;
}
}
});
},
isSelected: isSelected2,
),`

我想做的是在选择按钮时显示一个小部件。

我已经尝试了多种使用 if、and、else 语句的方法,但到目前为止我还无法弄清楚。

例如

if (buttonIndex == index[0]) {
// code here}
else {
//code here}

我哪里出错了?

谢谢!

最佳答案

    import 'package:flutter/material.dart';

class SamplePage extends StatefulWidget {
@override
_SamplePageState createState() => _SamplePageState();
}

class _SamplePageState extends State<SamplePage> {
List<bool> isSelected;

@override
void initState() {
isSelected = [true, false];
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ToggleButtons'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ToggleButtons(
borderColor: Colors.black,
fillColor: Colors.grey,
borderWidth: 2,
selectedBorderColor: Colors.black,
selectedColor: Colors.white,
borderRadius: BorderRadius.circular(0),
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Open 24 Hours',
style: TextStyle(fontSize: 16),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Custom Hours',
style: TextStyle(fontSize: 16),
),
),
],
onPressed: (int index) {
setState(() {
for (int i = 0; i < isSelected.length; i++) {
isSelected[i] = i == index
}
});
},
isSelected: isSelected,
),
],
),
),
);
}
}

enter image description here

关于Flutter ToggleButton 类 - Flutter 1.9.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57910554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com