Flutter ToggleButton 类 - Flutter 1.9.1

Arr*_*ray 12 dart flutter

我目前正在学习 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,
              ),`
Run Code Online (Sandbox Code Playgroud)

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

我已经尝试了很多方法来处理 if、and、else 语句,但到目前为止我还是想不通。

例如

if (buttonIndex == index[0]) {
 // code here} 
else {
 //code here}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

谢谢!

Ami*_*ati 36

    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,
                ),
            ],
            ),
        ),
        );
    }
    }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 天哪,` if (i == index) { isSelected[i] = true; } else {isSelected[i] = false; }` 可以替换为 `isSelected[i] = i == index` (6认同)