我目前正在学习Flutter ,并取得了良好的进展,所以如果这是一个noob问题,请容忍我.

Ffltter最近更新到了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,
              ),`

我要做的是在 Select 按钮时显示一个小部件.

我已经try 了很多种方法来使用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

Dart相关问答推荐

dart - 通过异步等待避免递归 Stackoverflow

Dart - 检测未完成的期货

Const 构造函数不适用于 Dart 并显示 id 为 0

从 Dart 中的工厂构造函数返回 null 是否可以接受?

默认情况下不可为空(Non-nullable ):如何启用experiment?

FlutterError (setState() 在 dispose() 之后调用:(生命周期状态:defunct,not mounted)

LinearProgressIndicator Flutter使用

Flutter 中的嵌套数据和 BLoC

Flutter:测试期间的计时器问题

Flutter url_launcher 未在发布模式下启动 url

Flutter: shared preferences

Flutter:如何获取assets目录中所有图像的名称列表?

如何循环遍历元素列表

Dart 后期初始化final变量

从 Dart 调用 javascript

我应该如何在 Dart 中测试 Future?

如何在dart中生成随机字符串?

找不到名为pubspec.yaml的文件

Dart:创建一个从 0 到 N 的列表

如何在构造函数中初始化最终类属性?