簡體   English   中英

Flutter 開關小部件 - 我只希望它在活動時執行該功能,該怎么辦?

[英]Flutter switch widget- I ony want it to execute the function when it's active, what to do?

我對顫振中的開關小部件有問題。 我只想在激活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(){

    };
  }
);

演示

您可以在下面復制粘貼運行完整代碼
您可以聲明一個bool _isOn並設置為false然后在onChanged更改值
代碼片段

 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),
      ),
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM