Flutter 从另一个文件调用void函数
我正在创建一个应用程序,它有一个开始按钮(类StartButton),我希望该按钮从另一个类(类NewRide)调用一个方法,但是如何从另一个文件调用void函数呢Flutter 从另一个文件调用void函数,flutter,dart,timer,Flutter,Dart,Timer,我正在创建一个应用程序,它有一个开始按钮(类StartButton),我希望该按钮从另一个类(类NewRide)调用一个方法,但是如何从另一个文件调用void函数呢 class StartButton extends StatefulWidget{ @override _StartButtonState createState() => _StartButtonState(); } class _StartButtonState extends State<StartButto
class StartButton extends StatefulWidget{
@override
_StartButtonState createState() => _StartButtonState();
}
class _StartButtonState extends State<StartButton> {
String _driverState = 'offline';
@override
Widget build(context){
return FlatButton(
child: Text('Start'),
onPressed: (){
setState(() {
if (_driverState == 'offline') {
_driverState = 'online';
} else {
_driverState = 'offline';
}
});
},
);
}
}
class StartButton扩展StatefulWidget{
@凌驾
_StartButtonState createState()=>_StartButtonState();
}
类_StartButtonState扩展状态{
字符串_driverState='offline';
@凌驾
小部件构建(上下文){
返回按钮(
子项:文本('Start'),
已按下:(){
设置状态(){
如果(_driverState=='offline'){
_driverState='在线';
}否则{
_driverState='offline';
}
});
},
);
}
}
这是一个新的骑乘类,它有一个void函数,我想在按下启动按钮时调用它
class NewRide extends StatefulWidget {
@override
_NewRideState createState() => _NewRideState();
}
class _NewRideState extends State<NewRide> {
int _counter = 60;
Timer _timer;
@override
Widget build(BuildContext context) {
void startTimer() {
_counter = 60;
if (_timer != null) {
_timer.cancel();
}
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
if (_counter > 0) {
_counter--;
} else {
_timer.cancel();
}
});
});
}
_showNewRideAlert() {
if (_counter == 0) {
return Text('Counter == 0');
} else {
return Text('Counter != 0');
}
}
return _showNewRideAlert();
}
}
类NewRide扩展StatefulWidget{
@凌驾
_NewRideState createState()=>\u NewRideState();
}
类_NewRideState扩展状态{
int_计数器=60;
定时器(u定时器),;
@凌驾
小部件构建(构建上下文){
void startTimer(){
_计数器=60;
如果(_timer!=null){
_timer.cancel();
}
_计时器=计时器。周期性(持续时间(秒:1),(计时器){
设置状态(){
如果(_计数器>0){
_计数器--;
}否则{
_timer.cancel();
}
});
});
}
_showNewRideAlert(){
如果(_计数器==0){
返回文本('Counter==0');
}否则{
返回文本('Counter!=0');
}
}
return_showNewRideAlert();
}
}
首先将另一个文件/类导入到主类中(我不确定文件的名称)
导入“…/newRide.dart”作为newRide代码>
然后像这样调用函数:
newRide._showNewRideAlert();
但是,由于类/状态的设置方式,它可能无法工作(不会构建小部件)。因此,您可能必须将代码移动到可调用的位置,例如类的顶部,或者将函数实现到使用它的页面中。首先将另一个文件/类导入到主类中(我不确定您的文件被调用了什么)
导入“…/newRide.dart”作为newRide代码>
然后像这样调用函数:
newRide._showNewRideAlert();
但是,由于类/状态的设置方式,它可能无法工作(不会构建小部件)。因此,您可能必须将代码移动到可调用的位置,例如类的顶部,或者将函数实现到使用它的页面中。使用GlobalKey并将startTimer公开,并将NewRideState更改为NewRideState
class StartButton extends StatefulWidget {
@override
_StartButtonState createState() => _StartButtonState();
}
class _StartButtonState extends State<StartButton> {
String _driverState = 'offline';
GlobalKey<NewRideState> newRideKey = GlobalKey<NewRideState>();
@override
Widget build(context) {
return Column(
children: [
NewRide(key: newRideKey),
FlatButton(
child: Text('Start'),
onPressed: () {
setState(() {
if (_driverState == 'offline') {
_driverState = 'online';
} else {
_driverState = 'offline';
}
});
},
),
],
);
}
}
class NewRide extends StatefulWidget {
const NewRide({Key key}) : super(key: key);
@override
NewRideState createState() => NewRideState();
}
class NewRideState extends State<NewRide> {
int _counter = 60;
Timer _timer;
@override
Widget build(BuildContext context) {
_showNewRideAlert() {
if (_counter == 0) {
return Text('Counter == 0');
} else {
return Text('Counter != 0');
}
}
return _showNewRideAlert();
}
void startTimer() {
_counter = 60;
if (_timer != null) {
_timer.cancel();
}
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
if (_counter > 0) {
_counter--;
} else {
_timer.cancel();
}
});
});
}
}
class StartButton扩展StatefulWidget{
@凌驾
_StartButtonState createState()=>_StartButtonState();
}
类_StartButtonState扩展状态{
字符串_driverState='offline';
GlobalKey-newRideKey=GlobalKey();
@凌驾
小部件构建(上下文){
返回列(
儿童:[
NewRide(钥匙:newRideKey),
扁平按钮(
子项:文本('Start'),
已按下:(){
设置状态(){
如果(_driverState=='offline'){
_driverState='在线';
}否则{
_driverState='offline';
}
});
},
),
],
);
}
}
类NewRide扩展StatefulWidget{
const NewRide({Key}):super(Key:Key);
@凌驾
NewRideState createState()=>NewRideState();
}
类NewRideState扩展状态{
int_计数器=60;
定时器(u定时器),;
@凌驾
小部件构建(构建上下文){
_showNewRideAlert(){
如果(_计数器==0){
返回文本('Counter==0');
}否则{
返回文本('Counter!=0');
}
}
return_showNewRideAlert();
}
void startTimer(){
_计数器=60;
如果(_timer!=null){
_timer.cancel();
}
_计时器=计时器。周期性(持续时间(秒:1),(计时器){
设置状态(){
如果(_计数器>0){
_计数器--;
}否则{
_timer.cancel();
}
});
});
}
}
使用GlobalKey将startTimer公开,并将NewRideState更改为uNewRidestate
class StartButton extends StatefulWidget {
@override
_StartButtonState createState() => _StartButtonState();
}
class _StartButtonState extends State<StartButton> {
String _driverState = 'offline';
GlobalKey<NewRideState> newRideKey = GlobalKey<NewRideState>();
@override
Widget build(context) {
return Column(
children: [
NewRide(key: newRideKey),
FlatButton(
child: Text('Start'),
onPressed: () {
setState(() {
if (_driverState == 'offline') {
_driverState = 'online';
} else {
_driverState = 'offline';
}
});
},
),
],
);
}
}
class NewRide extends StatefulWidget {
const NewRide({Key key}) : super(key: key);
@override
NewRideState createState() => NewRideState();
}
class NewRideState extends State<NewRide> {
int _counter = 60;
Timer _timer;
@override
Widget build(BuildContext context) {
_showNewRideAlert() {
if (_counter == 0) {
return Text('Counter == 0');
} else {
return Text('Counter != 0');
}
}
return _showNewRideAlert();
}
void startTimer() {
_counter = 60;
if (_timer != null) {
_timer.cancel();
}
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
if (_counter > 0) {
_counter--;
} else {
_timer.cancel();
}
});
});
}
}
class StartButton扩展StatefulWidget{
@凌驾
_StartButtonState createState()=>_StartButtonState();
}
类_StartButtonState扩展状态{
字符串_driverState='offline';
GlobalKey-newRideKey=GlobalKey();
@凌驾
小部件构建(上下文){
返回列(
儿童:[
NewRide(钥匙:newRideKey),
扁平按钮(
子项:文本('Start'),
已按下:(){
设置状态(){
如果(_driverState=='offline'){
_driverState='在线';
}否则{
_driverState='offline';
}
});
},
),
],
);
}
}
类NewRide扩展StatefulWidget{
const NewRide({Key}):super(Key:Key);
@凌驾
NewRideState createState()=>NewRideState();
}
类NewRideState扩展状态{
int_计数器=60;
定时器(u定时器),;
@凌驾
小部件构建(构建上下文){
_showNewRideAlert(){
如果(_计数器==0){
返回文本('Counter==0');
}否则{
返回文本('Counter!=0');
}
}
return_showNewRideAlert();
}
void startTimer(){
_计数器=60;
如果(_timer!=null){
_timer.cancel();
}
_计时器=计时器。周期性(持续时间(秒:1),(计时器){
设置状态(){
如果(_计数器>0){
_计数器--;
}否则{
_timer.cancel();
}
});
});
}
}
谢谢!但是我想调用void startTimer()
,所以我尝试了newRide.startTimer()
;但是没有起作用。它说函数“startTimer”没有定义。如果函数直接在类中,它会工作,但它在扩展类中,这就是为什么它不工作的原因,您可以创建自定义类。谢谢!但是我想调用void startTimer()