从SwitchListTile返回True值?

时间:2019-09-07 03:55:42

标签: flutter

这是一个菜鸟问题,但我似乎无法弄清楚该怎么做。

我有3个SwitchListTile,在任何给定时间只能有一个为真-但我不确定如何告诉我的代码用户选择了哪个为真,以便我可以将String传递给另一个对象以执行某项操作其他。

我有main.dart,我将其称为colorswitch1.dart(它确定用户想要的颜色选择,即绿色或蓝色或红色或黄色等)。

UI组件工作正常

我已经研究了未来的变量和Navigator,但是我不确定正确的答案是什么……或者更有效的答案

我的SwitchListTile如下所示:

    child: SwitchListTile(
            title: const Text('Color'),
            value: _anycolor,
            activeColor: Colors.black,

            onChanged: (bool value) {
              setState(() {


                if(value = true){
                  _anycolor = value;
                  _greyscale = false;
                  _transparent = false;
                  toRet = '';

                }


              });
            },
            //secondary: const Icon(Icons.color_lens),
          ),

toRet只是一个字符串,我试图返回到main.dart,在其中我可以有一个名为“字符串选择”的变量

或者这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

使用bool列表来保留SwithListTile的结果,并在单击开关后使用回调以重置选择。

回调代码段_finalSelection具有结果

void switchCallback(bool value, int index) {
    setState(() {
      for (var i=0; i<choice.length; i++) {
        choice[i] = false;
      }

      print(' click ${index}');
      choice[index] = value;

      _finalSelection = ' index ${index}  {$value}';
      print(' final selection ${_finalSelection}');
    });
  } 

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: MyStatefulWidget(),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}


String _finalSelection = "";

class _MyStatefulWidgetState extends State<MyStatefulWidget> {

  static List<bool> choice = [false, false, false];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Center(
          child: SwitchListTile(
            title: const Text('Lights 1'),
            value: choice[0],
            onChanged: (bool value) {
              switchCallback(value, 0);
            },
            secondary: const Icon(Icons.lightbulb_outline),
          ),
        ),
        Center(
          child: SwitchListTile(
            title: const Text('Lights 2'),
            value: choice[1],
            onChanged: (bool value) {
              switchCallback(value, 1);
            },
            secondary: const Icon(Icons.lightbulb_outline),
          ),
        ),
        Center(
          child: SwitchListTile(
            title: const Text('Lights 3'),
            value: choice[2],
            onChanged: (bool value) {
              switchCallback(value, 2);
            },
            secondary: const Icon(Icons.lightbulb_outline),
          ),
        ),
      ],
    );
  }

  void switchCallback(bool value, int index) {
    setState(() {
      for (var i=0; i<choice.length; i++) {
        choice[i] = false;
      }

      print(' click ${index}');
      choice[index] = value;

      _finalSelection = ' index ${index}  {$value}';
      print(' final selection ${_finalSelection}');
    });
  }
}

enter image description here

执行信息,您可以根据您的请求更改_finalSelection

I/flutter ( 9216):  click 2
I/flutter ( 9216):  final selection  index 2  {true}
I/flutter ( 9216):  click 1
I/flutter ( 9216):  final selection  index 1  {true}
I/flutter ( 9216):  click 1
I/flutter ( 9216):  final selection  index 1  {false}
I/flutter ( 9216):  click 0
I/flutter ( 9216):  final selection  index 0  {true}
I/flutter ( 9216):  click 1
I/flutter ( 9216):  final selection  index 1  {true}
I/flutter ( 9216):  click 2
I/flutter ( 9216):  final selection  index 2  {true}

答案 1 :(得分:0)

尝试添加setState((){});

 if(value = true){
   setState((){
    _anycolor = value;
    _greyscale = false;
    _transparent = false;
    toRet = '';
   }); 
 }