我目前正在用dart编写flutter项目,并使用flutter构建android应用。我目前有一张图像,可将整个屏幕作为启动屏幕。现在,我想提供其他图像,以便用户每次打开应用程序时该应用程序显示随机图像。
我已经搜索了“如何随机化Android的启动屏幕”,但是这都需要修改一些Java代码(但是我正在编写一个dart项目)。还有另一种方法,例如仅修改.dart和/或.xml代码吗? 我已将第二张图片添加到其他位置,但是没有任何效果。见下文:
在launch_background.xml中,以下内容仅显示splash_screen_1图像,从不显示splash_screen_2。
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<!-- You can insert your own image assets here -->
<item>
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="@drawable/splash_screen_1" />
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="@drawable/splash_screen_2" />
</item>
</layer-list>
以下仅显示splash_screen_2图片,从不显示splash_screen_1。
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<!-- You can insert your own image assets here -->
<item>
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="@drawable/splash_screen_1" />
</item>
<item>
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="@drawable/splash_screen_2" />
</item>
</layer-list>
有没有办法解决抖动问题?例如仅修改.xml和/或.dart代码,而无需修改Java代码?谢谢!
答案 0 :(得分:0)
尝试一下:
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
Widget randomScreen(int max) {
var rnd = Random();
var num = rnd.nextInt(max);
print(num);
switch (num) {
case 1: return SecondSplashScreen();break;
case 2: return ThirdSplashScreen();break;
default: return FirstSplashScreen();break;
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: randomScreen(4),
);
}
}
class FirstSplashScreen extends StatefulWidget {
FirstSplashScreen({Key key}) : super(key: key);
_FirstSplashScreenState createState() => _FirstSplashScreenState();
}
class _FirstSplashScreenState extends State<FirstSplashScreen> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blue
);
}
}
class SecondSplashScreen extends StatefulWidget {
SecondSplashScreen({Key key}) : super(key: key);
_SecondSplashScreenState createState() => _SecondSplashScreenState();
}
class _SecondSplashScreenState extends State<SecondSplashScreen> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.red
);
}
}
class ThirdSplashScreen extends StatefulWidget {
ThirdSplashScreen({Key key}) : super(key: key);
_ThirdSplashScreenState createState() => _ThirdSplashScreenState();
}
class _ThirdSplashScreenState extends State<ThirdSplashScreen> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.green
);
}
}