Dart 如何从开关调用函数
使用在Android Studio中创建的默认Dart 如何从开关调用函数,dart,flutter,Dart,Flutter,使用在Android Studio中创建的默认flatter应用程序,我尝试测试,因此添加了以下代码: new Switch(value: true, onChanged: (bool newValue) { setState(() { _incrementCounter(); // executed only if the value is true }); },), 递增计数器功能是: void _incrementCounter() { setState((
flatter
应用程序,我尝试测试,因此添加了以下代码:
new Switch(value: true, onChanged: (bool newValue) {
setState(() {
_incrementCounter(); // executed only if the value is true
});
},),
递增计数器
功能是:
void _incrementCounter() {
setState(() {
_counter++;
});
}
我遇到的问题是,一旦
开关
值切换回false
,就会调用并执行递增计数器
函数,而我期望的是每次切换开关时都会调用递增计数器
函数,即无论新值为true还是false 您没有切换该值
错误基本上在这一行的代码中:
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,)
),
);
}
正如@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}"),)
],)
);
}
}
类MyApp扩展StatefulWidget{
@凌驾
MyAppState createState(){
返回新的MyAppState();
}
}
类MyAppState扩展了状态{
var_值=假;
var inc=0;
onchange(布尔值){
设置状态(){
inc++;
_价值=价值;
});
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(),
正文:新列(子项:[
新开关(值:_值,onChanged:(bool值)=>onchange(值)),
新中心(子项:新文本(“value${inc}”),)
],)
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(新的MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“颤振演示”,
主题:新主题数据(
primarySwatch:颜色。深橙色,
),
主页:新MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
var_值=假;
双倍车身高度=0.0;
更改后无效(布尔值){
设置状态(){
_价值=价值;
这个._bodyHeight=(value==true)?400.0:0.0;
});
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
背景颜色:颜色。灰色[500],
appBar:新的appBar(
标题:新文本(widget.title),
),
正文:新的SingleChildScrollView(
子:新列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
新卡(
子容器:新容器(
身高:50.0,
孩子:新的一排(
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
新文本(“请选择我”),
新开关(值:_值,onChanged:(bool值)=>onchange(值)),
],
),
),
),
新卡(
子对象:新的动画容器(
子:新列表视图(
儿童:[
新ListTile(
前导:常量图标(Icons.person),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“名称”,
),
),
),
新ListTile(
前导:常量图标(Icons.phone),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“电话”,
),
),
),
新ListTile(
引导:常量图标(Icons.email),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“电子邮件”,
),
),
),
常数分频器(
高度:1.0,
),
新ListTile(
前导:常量图标(图标.标签),
标题:常量文本(“尼克”),
副标题:常量文本(“无”),
),
新ListTile(
领先:const图标(今天的图标),
标题:const Text(“生日”),
字幕:康斯特文本(1980年2月20日),
),
新ListTile(
前导:常量图标(Icons.group),
标题:const Text(“联络小组”),
副标题:常量文本(“未指定”),
),
新ListTile(
前导:常量图标(Icons.person),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“名称”,
),
),
),
新ListTile(
前导:常量图标(Icons.phone),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“电话”,
),
),
),
新ListTile(
引导:常量图标(Icons.email),
标题:新文本字段(
装饰:新的输入装饰(
hintText:“电子邮件”,
),
),
),
常数分频器(
高度:1.0,
),
新ListTile(
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,
),
),
],
),
),
);
}
}