flutter - flutter root widget 应该始终是 StatelessWidget 吗?

标签 flutter

当我在 flutter 中阅读文档时,我有一个问题,flutter root widget 应该始终是 StatelessWidget 吗?

 class MyApp extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return MaterialApp(
       title: 'Flutter Code Sample for Navigator',
       // MaterialApp contains our top-level Navigator
       initialRoute: '/',
       routes: {
         '/': (BuildContext context) => HomePage(),
         '/signup': (BuildContext context) => SignUpPage(),
       },
     );
   }
 }
  1. 因为我觉得有些时候需要调用init函数,可能不想把那个代码写在HomePage里。例如:检查token是否过期,决定去HomePage还是LoginPage
  2. 那么最好的选择是:我是否应该将根 Widget 更改为 StatefulWidget,并将上面的逻辑包含在其 initState 函数中?

最佳答案

让根widget成为StatefulWidget在监听AppLifecycleState时很有用
例如恢复 WebSocket 连接之类的恢复工作

代码片段

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

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {

  AppLifecycleState _lastLifecycleState;


  @override
  void initState() {
    super.initState();
    initPlatformState();
    WidgetsBinding.instance.addObserver(this);

  }

  initPlatformState() async {
    Screen.keepOn(true);   
  }

  Future<void> resumeCallBack() {
    if (sl<WebSocketService>().webSocketState == 'lost') {
      sl<WebSocketService>().initWebSocket();
    }

    if (mounted) {
      setState(() {});
    }
    print("resumeCallBack");
  }

  Future<void> suspendingCallBack() {
    if (mounted) {
      setState(() {});
    }
    print("suspendingCallBack");
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    super.didChangeAppLifecycleState(state);
    print("AppLifecycleState Current state = $state");
    setState(() {
      _lastLifecycleState = state;
    });

    switch (state) {
      case AppLifecycleState.inactive:
      case AppLifecycleState.paused:
      case AppLifecycleState.detached:
      /*case AppLifecycleState.suspending:
        await suspendingCallBack();
        break;*/
      case AppLifecycleState.resumed:
        await resumeCallBack();
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Template',
      debugShowCheckedModeBanner: false,
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: '/',
      routes: {
        '/': (context) => LoginPage(),

关于flutter - flutter root widget 应该始终是 StatelessWidget 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59170761/

相关文章:

dart - http url参数无效字符错误

flutter - Bloc:如何模拟获取状态

flutter - Flutter中如何在NetworkImage()中添加网络图片列表

dart - ListWheelScrollView 子项无法识别 onTap()

android - 在屏幕点击时显示(滑入)或隐藏(滑出)flutter AppBar

scroll - 当我在Sliver应用程序栏中点按文本字段时,CustomscrollView会自动滚动到顶部位置

android - flutter 插件 : "Methods marked with @UiThread must be executed on the main thread."

flutter - 如何向LinearProgressIndicator 添加线性渐变?

android - 在 Flutter for BottomModalSheet 小部件中找不到脚手架错误,即使它是在脚手架内定义的

firebase - flutterfire 尚未配置任何应用程序。检索线程信息时出错 : (ipc/send) invalid destination port