【问题标题】:Flutter Application Stuck at SplashscreenFlutter 应用程序卡在 Splashscreen 上
【发布时间】:2020-08-07 13:39:15
【问题描述】:

大家好...我在 Flutter 上使用 SplashScreen 时遇到问题...我的应用程序安装并打开,但它卡在启动屏幕徽标中...它没有进一步...我为您提供我的代码,需要帮助...

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manish.hotel">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

   <application
    android:name=".Application"
    android:label="Hotel App"
    android:icon="@mipmap/ic_launcher">

    
    <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/ic_notification" />
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <!-- This keeps the window background of the activity showing
             until Flutter renders its first frame. It can be removed if
             there is no splash screen (such as the default splash screen
             defined in @style/LaunchTheme). -->
        <meta-data
            android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
            android:value="true" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

SplashScreen.dart

        import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:mvc_pattern/mvc_pattern.dart';
import 'package:manish_hotel_ui/generated/i18n.dart';
import 'package:manish_hotel_ui/src/controllers/controller.dart';
import 'package:manish_hotel_ui/src/repository/user_repository.dart';

class SplashScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SplashScreenState();
  }
}

class SplashScreenState extends StateMVC<SplashScreen> {
  Controller _con;

  SplashScreenState() : super(Controller()) {
    _con = controller;
  }

  @override
  void initState() {
    super.initState();
    loadData();
  }

  Future<Timer> loadData() async {
    return new Timer(Duration(seconds: 5), onDoneLoading);
  }

  onDoneLoading() async {
    SchedulerBinding.instance.addPostFrameCallback((_) {
      if (currentUser.apiToken == null) {
        Navigator.of(context).pushReplacementNamed('/login');
      } else {
        Navigator.of(context).pushReplacementNamed('/pages', arguments: 2);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _con.scaffoldKey,
      body: Container(
        decoration: BoxDecoration(
          color: Theme.of(context).accentColor,
        ),
        child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Icon(
                Icons.hotel_menu,
                size: 90,
                color: Theme.of(context).scaffoldBackgroundColor,
              ),
              Text(
                S.of(context).hotel,
                style: Theme.of(context)
                    .textTheme
                    .display1
                    .merge(TextStyle(color: Theme.of(context).scaffoldBackgroundColor)),
              ),
              SizedBox(height: 50),
              CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(Theme.of(context).scaffoldBackgroundColor),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

样式.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
</resources>

launch_background

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />
     <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher" />
    </item> 
</layer-list>

求各位大神帮忙,搞了2个月,还是没解决……

【问题讨论】:

  • 我也遇到了同样的问题,重启应用就可以解决问题了。
  • 谢谢你的回答,但我试过没有进展...
  • 这方面有什么更新吗?你设法解决了这个问题吗?怎么样?

标签: android flutter flutter-dependencies


【解决方案1】:

我认为问题在于您在启动后 5 秒注册了 PostFrameCallback,因此该小部件已经构建并且永远不会执行回调。尝试删除它:

SchedulerBinding.instance.addPostFrameCallback((_) {
 // Extract the code inside
});

【讨论】:

  • 谢谢你的回答,我试过了还是没有进展...
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 2022-10-08
  • 1970-01-01
  • 2020-05-12
  • 2021-04-21
  • 2021-06-10
  • 1970-01-01
  • 2021-11-06
相关资源
最近更新 更多