I have a problem with the switch widget in flutter. I only want to execute the onChanged
function when I activate it but every time I click on the switch even when it's not active, it executes the function and I have the popup menu that appears.
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(){
};
}
);
You can copy paste run full code below
You can declare a bool
_isOn
and set to false
and then in onChanged
change value
code snippet
bool _isOn = false;
...
Switch(
value: _isOn,
onChanged: (bool isOn) {
setState(() {
_isOn = isOn;
});
working demo
full code
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),
),
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.