Add transitions in stack layout

This commit is contained in:
2024-07-12 22:27:09 +02:00
parent 4da0e784ca
commit 51d2e7be05
4 changed files with 55 additions and 1 deletions

52
AnimatedStackLayout.qml Normal file
View File

@@ -0,0 +1,52 @@
import QtQuick
import QtQuick.Layouts
StackLayout {
id: root
property int previousIndex: 0
property var items: children
property Item previousItem: items[previousIndex]
property Item currentItem: items[currentIndex]
Component.onCompleted: {
previousIndex = currentIndex
for(var i = 1; i < count; ++i) {
children[i].opacity = 0
}
}
Component {
id: crossFader
ParallelAnimation {
property Item fadeOutTarget
property Item fadeInTarget
NumberAnimation {
target: fadeOutTarget
property: "opacity"
to: 0
duration: 300
}
NumberAnimation {
target: fadeInTarget
property: "opacity"
to: 1
duration: 300
}
}
}
onCurrentIndexChanged: {
items = root.children
if(previousItem && currentItem && (previousItem != currentItem)) {
previousItem.visible = false
currentItem.visible = true
var crossFaderAnim = crossFader.createObject(parent, {fadeOutTarget: previousItem, fadeInTarget: currentItem})
crossFaderAnim.restart()
}
previousIndex = currentIndex
}
}

View File

@@ -53,6 +53,7 @@ qt_add_qml_module(evcharger-app
AboutPage.qml AboutPage.qml
AccessPage.qml AccessPage.qml
AddDeviceScreen.qml AddDeviceScreen.qml
AnimatedStackLayout.qml
AnimatedStackView.qml AnimatedStackView.qml
ApiKeyValueItem.qml ApiKeyValueItem.qml
ApiSettingsPage.qml ApiSettingsPage.qml

View File

@@ -175,7 +175,7 @@ ColumnLayout {
} }
} }
StackLayout { AnimatedStackLayout {
id: stackLayout id: stackLayout
Layout.fillWidth: true Layout.fillWidth: true
Layout.fillHeight: true Layout.fillHeight: true

1
qmldir
View File

@@ -3,6 +3,7 @@ module EVChargerApp
EVChargerApp 1.0 AboutPage.qml EVChargerApp 1.0 AboutPage.qml
EVChargerApp 1.0 AccessPage.qml EVChargerApp 1.0 AccessPage.qml
EVChargerApp 1.0 AddDeviceScreen.qml EVChargerApp 1.0 AddDeviceScreen.qml
EVChargerApp 1.0 AnimatedStackLayout.qml
EVChargerApp 1.0 AnimatedStackView.qml EVChargerApp 1.0 AnimatedStackView.qml
EVChargerApp 1.0 ApiKeyValueItem.qml EVChargerApp 1.0 ApiKeyValueItem.qml
EVChargerApp 1.0 ApiSettingsPage.qml EVChargerApp 1.0 ApiSettingsPage.qml