![](/img/trans.png)
[英]the function doesnt execute when i send it to it parent extracted widget
[英]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.