简体   繁体   English

Flutter Dart 类之间的回调或侦听器

[英]callback or listener between Flutter Dart Class

I am a newby and just started coding in Dart/Flutter for a mobile application.我是一个新手,刚刚开始在 Dart/Flutter 中为移动应用程序编码。 I am trying to create an interface or callback or listener between to class but I can't figure out the best way.我正在尝试在类之间创建接口或回调或侦听器,但我想不出最好的方法。 Right, the application is simple, it starts, show a splashscreen and after a timer expired, I have to show a another screen.是的,应用程序很简单,它启动,显示启动画面,在计时器到期后,我必须显示另一个屏幕。 I am using the SplashScreen lib to do it but I would like to call a listener/callback to start the other screen.我正在使用SplashScreen lib 来做这件事,但我想调用一个监听器/回调来启动另一个屏幕。 Below is my code for a better understanding of the structure.下面是我的代码,以便更好地理解结构。

main.dart

import 'package:flutter/material.dart';
import 'splash_screen.dart';

void main(){
  runApp(new MaterialApp(
    home: new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  Widget build(BuildContext context) {
    return new MyHomeSplashScreen().myHomeSplashScreen();
  }
}
}

splash_screen.dart

class MyHomeSplashScreen{
  SplashScreen myHomeSplashScreen(){
    return SplashScreen(
        seconds: 5,
        navigateAfterSeconds: null,//I want my callback there,
        title: _mySplashText(),
        imageBackground: new AssetImage("assets/images/splash_screen.png"),
        backgroundColor: Colors.transparent,
        styleTextUnderTheLoader: new TextStyle(),
        photoSize: 100.0,
        //onClick: ()=>print("Flutter Egypt"),
        loaderColor: Colors.white,
      );
  }

  Text _mySplashText(){
    return Text('MyHome', 
      style: new TextStyle(
        fontStyle: FontStyle.italic,
        fontWeight: FontWeight.bold,
        fontSize: 80.0,
        color: Colors.white
      ));
  }
}

The callback/listener should be called in the splash_screen.dart as shown in my example and have the associated code in the main.dart回调/侦听器应该在splash_screen.dart ,如我的示例所示,并在main.dart包含相关代码

Any idea how to do it at best ?知道如何做到最好吗? Thanks谢谢

你可以尝试扩展ValueNotifier和 usd addListener来回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM