对应用程序功能的说明。显示按钮被按下。出现一个对话框。如果在其中按Ok,文本出现,按钮名称将更改为Hide,如果按Cancel,则文本不出现,按钮名称保持不变。如果单击Hide,文本将消失。一切都很简单。
完整的代码将随之而来。一切正常。
我不清楚什么。如果您在代码中调用函数,一切正常
Navigator.of(context).pop( _f2() );
如果你把这个函数的实现,就会发生错误,第一个分号有下划线
Navigator.of(context).pop(
_b = true;
_visible = true;
_text = 'Hide';
);
如果将此函数的实现放在 setState 中,则不会发生错误,它可以正常工作
Navigator.of(context).pop(
setState(() {
_b = true;
_visible = true;
_text = 'Hide';
})
);
如果我放一个匿名函数,没有错误,但没有文字出现
Navigator.of(context).pop(
() {
_b = true;
_visible = true;
_text = 'Hide';
}
);
都是关于setState的吗?告诉我怎么做对吗?谢谢你。
这是完整的代码。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Name App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Name Page'),
),
body: MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _text;
bool _visible;
bool _b;
@override
void initState() {
super.initState();
_text = 'Show';
_visible = false;
_b = false;
}
@override
void dispose() {
super.dispose();
}
void _f1() {
setState(() {
switch(_b) {
case false:
_showAlertDialog1();
break;
case true:
_b = false;
_visible = false;
_text = 'Show';
break;
}
});
}
void _f2() {
setState(() {
_b = true;
_visible = true;
_text = 'Hide';
});
}
Future<void> _showAlertDialog1() async {
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
'SHOW TEXT!'
),
content: Text(
'Show text now'
),
actions: <Widget>[
FlatButton(
onPressed: () {
//все нормально работает
Navigator.of(context).pop( _f2() );
//происходит ошибка, подчеркнута первая точка с запятой
/*Navigator.of(context).pop(
_b = true;
_visible = true;
_text = 'Hide';
);*/
//ошибки не происходит, работает нормально
/*Navigator.of(context).pop(
setState(() {
_b = true;
_visible = true;
_text = 'Hide';
})
);*/
//ошибки нет, но текст не появляется
/*Navigator.of(context).pop(
() {
_b = true;
_visible = true;
_text = 'Hide';
}
);*/
},
child: Text(
'Ok',
),
),
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
'Cancel',
),
),
],
);
}
);
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Align(
alignment: Alignment.topLeft,
child: Container(
width: double.infinity,
margin: const EdgeInsets.all(16.0),
child: RaisedButton(
onPressed: () { _f1(); },
splashColor: Colors.blue.withOpacity(0.5),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
child: Text(
_text,
style: TextStyle(
color: Colors.deepPurple,
fontSize: 20.0,
),
),
),
),
),
Align(
alignment: Alignment.center,
child: Visibility(
visible: _visible,
child: Text(
'Hello World!',
style: TextStyle(
color: Colors.pink,
fontSize: 36.0,
),
),
),
),
],
);
}
}
首先你需要看看它是什么
setState
:现在它由什么组成
(){}
(一个匿名函数):setState
现在的问题是:注意和之间的相似之处(){}
?答:两个
void
功能。现在
Navigator.of(context).pop()
,看看它是由什么组成的pop
:T
(即通用)继承自Object
.因此,当我们在那里发送一个函数(例如,您的示例中的 _f1 )时,给定的函数
pop
会变成这样:最后一个问题:为什么匿名函数不更新小部件的状态?因为 y
StatefulWidget
只能更新一个小部件的状态setState
。但是不要混淆小部件的状态和变量的状态,变量总是会更新它的状态,而小部件需要被赋予一个命令(
setState
)用新数据重绘 => 更新小部件的状态。为什么会出现错误:
您将参数与代码块混淆了。
正确的变体:根据需要工作。