sách gpt4 ai đã đi

Flutter ToggleButton 类 - Flutter 1.9.1

In lại 作者:行者123 更新时间:2023-12-01 16:40:21 25 4
mua khóa gpt4 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) {
đặtState(() {
for (int buttonIndex = 0;
buttonIndex < isSelected2.length;
buttonIndex++) {
if (buttonIndex == index) {
isSelected2[buttonIndex] = true;
} khác {
isSelected2[buttonIndex] = false;
}
}
});
},
isSelected: isSelected2,
),`

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

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

Ví dụ

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

我哪里出错了?

Cảm ơn!

1 Câu trả lời

    import 'package:flutter/material.dart';

class SamplePage extends StatefulWidget {
@ghi đè
_SamplePageState createState() => _SamplePageState();
}

class _SamplePageState extends State {
List isSelected;

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

@ghi đè
Xây dựng tiện ích (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ToggleButtons'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
trẻ em: [
ToggleButtons(
borderColor: Colors.black,
fillColor: Colors.grey,
borderWidth: 2,
selectedBorderColor: Colors.black,
selectedColor: Colors.white,
borderRadius: BorderRadius.circular(0),
trẻ em: [
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) {
đặtState(() {
for (int i = 0; i < isSelected.length; i++) {
isSelected[i] = i == index
}
});
},
isSelected: isSelected,
),
],
),
),
);
}
}

nhập mô tả hình ảnh ở đây

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

25 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com