【问题标题】:Flutter splashScreen for Android how do I change the background colorFlutter splashScreen for Android 如何更改背景颜色
【发布时间】:2021-03-14 07:45:18
【问题描述】:

我想为我的 Flutter 应用 android 版本更改 splashScreen。我已经检查了 youtube,甚至在 stackOverFlow 中的旧帖子中,我已经按照这些步骤操作,但问题是当我启动应用程序时我的 colors.xml 没有被识别,我得到了白色的 splashScreen 几秒钟。

我附上了截图: 这是colors.xml 文件

这是launch_background.xml 文件

您可以清楚地看到我的colors.xml 文件值没有被识别。

我是新来的颤振,我试图参考文档,但它对我来说都是混乱的,无法弄清楚那里的头部和尾部,所以任何人都可以帮助我。我知道这对你们来说很简单。

请任何专业人士提前感谢您。

我也试过 invalid/Caches Restarted the studio too 还是没有结果。

这是我的main.dart

 class MyApp extends StatelessWidget {

    // This widget is the root of your application.
     @override
     Widget build(BuildContext context) {
     return MaterialApp(
     debugShowCheckedModeBanner: false,
     title: 'Flutter Demo',
      home:splash(),
      routes: <String, WidgetBuilder>{'/1': (BuildContext context) => 
      splashScreen()},
      );
     }
    }

这是我的splash.dart

// ignore: camel_case_types
class _splashState extends State<splash> {

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

startTime() async{
  var duration = Duration(seconds: 2);
  return Timer(duration, navigationPage);
 }

void navigationPage(){
 Navigator.of(context).pushReplacementNamed('/1');
}

 @override
  Widget build(BuildContext context) {
   return Scaffold(
    backgroundColor: myColors.myBackColor,
  );
}
}

这是我的splashScreen.dart

class splashScreen extends StatefulWidget {
 @override
 _splashScreenState createState() => _splashScreenState();
 }

// ignore: camel_case_types
class _splashScreenState extends State<splashScreen> with 
SingleTickerProviderStateMixin{

 AnimationController _animationController;
 Animation _animation;
 double top= 600.0;
 AlignmentTween _alignmentTween;
 Text _text;

 @override
 void initState() {
   super.initState();
   _animationController= AnimationController(
     vsync: this,
     duration: Duration(milliseconds: 600));
   _animationController.forward();
   _animation= CurvedAnimation(
       parent: _animationController,
       curve: Curves.easeIn);
   _alignmentTween= AlignmentTween(begin: Alignment.bottomCenter, end: 
   Alignment.topCenter);
   _text= Text('MyApp Name',style: TextStyle(
     fontSize: myFontSize.titleName,
     color: myColors.mySplashTitleColor,
     fontWeight: FontWeight.bold,
     fontFamily: 'acme',
     letterSpacing: 2.0,
   ));
 }

 @override
 Widget build(BuildContext context) {

   String key;

   return Scaffold(
     backgroundColor: myColors.myBackColor,
     body: TweenAnimationBuilder(
       key: Key(key),
       child: Align(
         alignment: Alignment(0,.6),
           child: _text),
       duration: Duration(milliseconds: 500),
       tween: Tween<double>(begin: 0.0, end: 1.0),
       builder: (context, double val,Widget child){
         return Opacity(opacity: val,
         child: child,);
       },
     ),
   );
 }
}

我在 youtube 上找到的上述代码和我尝试编辑的一些博客,以便我能理解发生了什么,然后当我想到启动画面时,它是 white color 我试图改变它然后我参考了youtube 和一些来自 StackOverFlow 的旧帖子,请按照相同的步骤操作,但我无法更改它。

我得到的结果:

【问题讨论】:

  • 只需更改colors.xml中的颜色并运行。没关系。
  • @OMiShah 我刚才试过了,我启动应用时仍然得到白色的splashScreen
  • 你的 Flutter clean 运行了吗?
  • 你的意思是 ternimal?
  • @RobertSandberg 是的,刚才我试过了,打开应用还是白屏。

标签: flutter


【解决方案1】:

试试下面...

在 ./values/colors.xml 中:

<resources>
    <color name="launcher_background_color">#FFCA28</color>
</resources>

在 ./drawable/launch_background.xml 中:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/launcher_background_color" />
</layer-list>

flutter clean 在终端中并重建

【讨论】:

  • 我已经尝试过相同的方法,但我仍然会在几秒钟内得到白色。
  • 我实际上刚刚创建了一个新项目,其中只有这些更改。这将产生一个黄色的屏幕。您是否在 Android 设备上运行,而不是意外启动了 IOS 设备? :) (我现在测试时确实犯了这个错误。)
  • 是的,我正在尝试在android设备上运行,是的,我没有启动IOS设备..
  • 尝试创建一个全新的全新项目,并确保它能够正常工作。然后看看您的新项目和现有项目之间可能存在哪些差异。
  • 太棒了!将答案标记为已接受/已完成/已解决。
猜你喜欢
  • 2020-10-26
  • 1970-01-01
  • 2014-11-08
  • 2021-06-21
  • 2020-02-27
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
相关资源
最近更新 更多