Flutter Transparent/Translucent Background App больше не поддерживается или требуются дополнительные действия

Я пытаюсь создать флаттер-приложение с полупрозрачным или прозрачным фоном. Я попробовал следовать инструкциям, найденным в stackoverflow и

Инструкция о том, как создать приложение Flutter с полупрозрачным или прозрачным фоном в текущей версии Flutter.

AndroidManifest.xml

<activity
android:name = ".MainActivity"
android:launchMode = "singleTop"
android:theme = "@style/LaunchTheme"
android:configChanges = "keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|density|layoutDirection"
android:windowSoftInputMode = "adjustResize">
<meta-data
    android:name = "io.flutter.embedding.android.NormalTheme"
    android:resource = "@style/NormalTheme"
    />
<intent-filter>
    <action android:name = "android.intent.action.MAIN"/>
    <category android:name = "android.intent.category.LAUNCHER"/>
</intent-filter>

стили.xml

<style name = "LaunchTheme" parent = "Theme.AppCompat.Light.NoActionBar">
    <!-- Transparent background -->
    <item name = "android:background">@android:color/transparent</item>
    <item name = "android:windowBackground">@android:color/transparent</item>
    <item name = "android:windowIsTranslucent">true</item>
</style>

<style name = "NormalTheme" parent = "Theme.AppCompat.Light.NoActionBar">
    <!-- Transparent background -->
    <item name = "android:background">@android:color/transparent</item>
    <item name = "android:windowBackground">@android:color/transparent</item>
    <item name = "android:windowIsTranslucent">true</item>
</style>

MainActivity.kt


import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import android.view.WindowManager

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setBackgroundDrawableResource(android.R.color.transparent)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
    }
}

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: TransparentScreen(),
    );
  }
}

class TransparentScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Center(
        child: Container(
          color: Colors.white.withOpacity(0.5), // Adjust the opacity as needed
          child: Text(
            'Hello, World!',
            style: TextStyle(fontSize: 24),
          ),
        ),
      ),
    );
  }
}

Я не помню, чтобы когда-либо видел обои на телефоне. Когда приложение запущено, домашняя страница вообще не отображается.

Randal Schwartz 18.07.2024 16:10

Пожалуйста, отредактируйте вопрос, чтобы ограничить его конкретной проблемой и указать достаточно подробностей, чтобы найти адекватный ответ.

Community 18.07.2024 16:20
0
2
50
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Добавьте эти строки в файл style.xml:

<style name = "LaunchTheme" parent = "@android:style/Theme.Light.NoTitleBar">
    <item name = "android:windowIsTranslucent">true</item>
    <item name = "android:windowBackground">@android:color/transparent</item>
    <item name = "android:windowContentOverlay">@null</item>
    <item name = "android:windowNoTitle">true</item>
    <item name = "android:windowIsFloating">true</item>
    <item name = "android:backgroundDimEnabled">false</item>
</style>

и:

<style name = "NormalTheme" parent = "@android:style/Theme.Light.NoTitleBar">
    <item name = "android:windowIsTranslucent">true</item>
    <item name = "android:windowBackground">@android:color/transparent</item>
    <item name = "android:windowContentOverlay">@null</item>
    <item name = "android:windowNoTitle">true</item>
    <item name = "android:windowIsFloating">true</item>
    <item name = "android:backgroundDimEnabled">false</item>
</style>

А в MainActivity.kt переопределите следующую функцию:

override fun getTransparencyMode(): TransparencyMode {
    return TransparencyMode.transparent
}

Никаких других манипуляций с MainActivity не требуется.

Другие вопросы по теме