【问题标题】:Flutter switch widget- I ony want it to execute the function when it's active, what to do?Flutter switch 小部件-我只希望它在它处于活动状态时执行该功能,该怎么办?
【发布时间】:2020-07-04 02:11:05
【问题描述】:

我在 Flutter 中遇到了 switch 小部件的问题。我只想在激活onChanged 功能时执行它,但每次我点击开关时,即使它没有激活,它也会执行该功能并且我会出现弹出菜单。

new Switch(
  value: false,

  onChanged: (bool isOn) {
    if(isOn){
      setState(() {
        return showDialog(
          context: context,
          barrierDismissible: false,
          builder: (context) {
            return AlertDialog(
              key: _alertDialogKey,
              contentPadding: EdgeInsets.only(left: 25, right: 25),
              title: Center(
                child: Text("Choisissez une filiale")
              ),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                Radius.circular(20.0)),
              ),
              content: Container(
                height: 350,
                width: 300,
                child: ListView.builder(
                  itemCount: litems.length,
                  itemBuilder: (_, index) {
                    return RaisedButton(
                      onPressed: () => popupAppuieSurUneFiliale(  index)
                        //changementAffiliation(litems[index]),
                        child: Text(
                          litems[index],
                          style: TextStyle(color: Colors.white),
                        ),
                      color: Colors.black,
                    );
                  }
                ),
              ), 
              actions:[
                RaisedButton(
                  child: Text("Annuler",),
                  onPressed: (){
                    Navigator.of(context).pop();
                  },
                  color: Colors.blue,
                ),
              ]
            );
          }
        );
      });
    } else(){

    };
  }
);

Demo

【问题讨论】:

    标签: function flutter switch-statement widget


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    您可以声明 bool _isOn 并设置为 false 然后在 onChanged 更改值
    代码sn-p

     bool _isOn = false;
     ...
     Switch(
                    value: _isOn,
                    onChanged: (bool isOn) {
                      setState(() {
                        _isOn = isOn;
                      });
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      List<String> litems = ["test"];
      bool _isOn = false;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Switch(
                    value: _isOn,
                    onChanged: (bool isOn) {
                      setState(() {
                        _isOn = isOn;
                      });
    
                      if (isOn) {
                        return showDialog(
                            context: context,
                            barrierDismissible: false,
                            builder: (context) {
                              return AlertDialog(
                                  //key: _alertDialogKey,
                                  contentPadding:
                                      EdgeInsets.only(left: 25, right: 25),
                                  title:
                                      Center(child: Text("Choisissez une filiale")),
                                  shape: RoundedRectangleBorder(
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(20.0)),
                                  ),
                                  content: Container(
                                    height: 350,
                                    width: 300,
                                    child: ListView.builder(
                                        itemCount: litems.length,
                                        itemBuilder: (_, index) {
                                          return RaisedButton(
                                            onPressed: () => null,
                                            //changementAffiliation(litems[index]),
                                            child: Text(
                                              litems[index],
                                              style: TextStyle(color: Colors.white),
                                            ),
                                            color: Colors.black,
                                          );
                                        }),
                                  ),
                                  actions: [
                                    RaisedButton(
                                      child: Text(
                                        "Annuler",
                                      ),
                                      onPressed: () {
                                        Navigator.of(context).pop();
                                      },
                                      color: Colors.blue,
                                    ),
                                  ]);
                            });
                      } else
                        () {};
                    }),
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      相关资源
      最近更新 更多