代码之家  ›  专栏  ›  技术社区  ›  Hasan A Yousef Michael Benjamin

如何从交换机调用函数

  •  3
  • Hasan A Yousef Michael Benjamin  · 技术社区  · 6 年前

    使用默认值 Flutter 应用程序创建于 Android Studio ,我试着测试 Switch ,因此我添加了以下代码:

    new Switch(value: true, onChanged: (bool newValue) {
      setState(() {
        _incrementCounter();  // executed only if the value is true
      });
    },),
    

    这个 incrementCounter 功能是:

      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    

    我的问题是 递增计数器 一旦 Switch 值切换回 false ,而我所期望的是 递增计数器 每次切换开关时,即无论新值为真或假,都应调用函数!

    3 回复  |  直到 6 年前
        1
  •  2
  •   Raouf Rahiche AbdulMomen عبدالمؤمن    6 年前

    正如@aziza所说,问题是:

    您没有切换值。

    所以这应该行得通

    class MyApp extends StatefulWidget {
      @override
      MyAppState createState() {
        return new MyAppState();
      }
    }
    
    class MyAppState extends State<MyApp> {
      var _value=false;
      var inc=0;
      onchange(bool value){
        setState((){
         inc++;
         _value=value;
        });
      }
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(),
          body : new Column(children: <Widget>[
            new Switch(value: _value, onChanged: (bool value)=>onchange(value)),
            new Center(child: new Text("value ${inc}"),)
          ],)
        );
      }
    }
    

    enter image description here

        2
  •  1
  •   Shady Aziza    6 年前

    您没有切换值。

    错误基本上在这一行的代码中:

    value: true
    

    可以使用类级布尔值来处理此问题。

    示例:

    //State level class
    
    bool switchValue = false;
      _dummyMethod(){
        print (this.switchValue);
      }
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(),
          body: new Center(
            child: new Switch(
              onChanged: (newValue){
                setState((){
                  this.switchValue = newValue;
                });
                _dummyMethod();
              },
             value: switchValue,)
          ),
        );
      }
    
        3
  •  0
  •   Hasan A Yousef Michael Benjamin    6 年前
    import 'package:flutter/material.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: new ThemeData(
            primarySwatch: Colors.deepOrange,
          ),
          home: new MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
    
      var _value=false;
      double _bodyHeight=0.0;
    
      void onchange(bool value) {
        setState(() {
          _value = value;
          this._bodyHeight = (value == true) ? 400.0 : 0.0;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          backgroundColor: Colors.grey[500],
          appBar: new AppBar(
            title: new Text(widget.title),
          ),
          body: new SingleChildScrollView(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Card(
                  child: new Container(
                    height: 50.0,
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
                        new Text("Select me pls"),
                        new Switch(value: _value, onChanged: (bool value) => onchange(value)),
                      ],
                    ),
                  ),
                ),
                new Card(
                  child: new AnimatedContainer(
                    child: new ListView(
                      children: <Widget>[
                          new ListTile(
                          leading: const Icon(Icons.person),
                          title: new TextField(
                            decoration: new InputDecoration(
                              hintText: "Name",
                            ),
                          ),
                        ),
                        new ListTile(
                          leading: const Icon(Icons.phone),
                          title: new TextField(
                            decoration: new InputDecoration(
                              hintText: "Phone",
                            ),
                          ),
                        ),
                        new ListTile(
                          leading: const Icon(Icons.email),
                          title: new TextField(
                            decoration: new InputDecoration(
                              hintText: "Email",
                            ),
                          ),
                        ),
                        const Divider(
                          height: 1.0,
                        ),
                        new ListTile(
                          leading: const Icon(Icons.label),
                          title: const Text('Nick'),
                          subtitle: const Text('None'),
                        ),
                        new ListTile(
                          leading: const Icon(Icons.today),
                          title: const Text('Birthday'),
                          subtitle: const Text('February 20, 1980'),
                        ),
                          new ListTile(
                            leading: const Icon(Icons.group),
                            title: const Text('Contact group'),
                            subtitle: const Text('Not specified'),
                          ),
                          new ListTile(
                            leading: const Icon(Icons.person),
                            title: new TextField(
                              decoration: new InputDecoration(
                                hintText: "Name",
                              ),
                            ),
                          ),
                          new ListTile(
                            leading: const Icon(Icons.phone),
                            title: new TextField(
                              decoration: new InputDecoration(
                                hintText: "Phone",
                              ),
                            ),
                          ),
                          new ListTile(
                            leading: const Icon(Icons.email),
                            title: new TextField(
                              decoration: new InputDecoration(
                                hintText: "Email",
                              ),
                            ),
                          ),
                          const Divider(
                            height: 1.0,
                          ),
                          new ListTile(
                            leading: const Icon(Icons.label),
                            title: const Text('Nick'),
                            subtitle: const Text('None'),
                          ),
                          new ListTile(
                            leading: const Icon(Icons.today),
                            title: const Text('Birthday'),
                            subtitle: const Text('February 20, 1980'),
                          ),
                          new ListTile(
                            leading: const Icon(Icons.group),
                            title: const Text('Contact group'),
                            subtitle: const Text('Not specified'),
                          )
                      ],
                    ),
                    curve: Curves.easeInOut,
                    duration: const Duration(milliseconds: 500),
                    height: _bodyHeight,
                    // color: Colors.red,
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }