FluCarousel.qml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import QtQuick
  2. import QtQuick.Controls
  3. import FluentUI
  4. Item {
  5. property bool flagXChanged: true
  6. property int radius : 5
  7. property int loopTime: 2000
  8. property bool showIndicator: true
  9. id:control
  10. width: 400
  11. height: 300
  12. ListModel{
  13. id:content_model
  14. }
  15. FluRectangle{
  16. anchors.fill: parent
  17. radius: [control.radius,control.radius,control.radius,control.radius]
  18. FluShadow{
  19. radius:control.radius
  20. }
  21. ListView{
  22. id:list_view
  23. anchors.fill: parent
  24. snapMode: ListView.SnapOneItem
  25. clip: true
  26. boundsBehavior: ListView.StopAtBounds
  27. model:content_model
  28. maximumFlickVelocity: 4 * (list_view.orientation === Qt.Horizontal ? width : height)
  29. preferredHighlightBegin: 0
  30. preferredHighlightEnd: 0
  31. highlightMoveDuration: 0
  32. orientation : ListView.Horizontal
  33. delegate: Item{
  34. width: ListView.view.width
  35. height: ListView.view.height
  36. property int displayIndex: {
  37. if(index === 0)
  38. return content_model.count-3
  39. if(index === content_model.count-1)
  40. return 0
  41. return index-1
  42. }
  43. Image {
  44. anchors.fill: parent
  45. source: model.url
  46. asynchronous: true
  47. fillMode:Image.PreserveAspectCrop
  48. }
  49. }
  50. onMovementEnded:{
  51. currentIndex = list_view.contentX/list_view.width
  52. if(currentIndex === 0){
  53. currentIndex = list_view.count-2
  54. }else if(currentIndex === list_view.count-1){
  55. currentIndex = 1
  56. }
  57. flagXChanged = false
  58. timer_run.start()
  59. }
  60. onMovementStarted: {
  61. flagXChanged = true
  62. timer_run.stop()
  63. }
  64. onContentXChanged: {
  65. if(flagXChanged){
  66. var maxX = Math.min(list_view.width*(currentIndex+1),list_view.count*list_view.width)
  67. var minY = Math.max(0,(list_view.width*(currentIndex-1)))
  68. if(contentX>=maxX){
  69. contentX = maxX
  70. }
  71. if(contentX<=minY){
  72. contentX = minY
  73. }
  74. }
  75. }
  76. }
  77. }
  78. function setData(data){
  79. content_model.clear()
  80. content_model.append(data[data.length-1])
  81. content_model.append(data)
  82. content_model.append(data[0])
  83. list_view.currentIndex = 1
  84. timer_run.restart()
  85. }
  86. Row{
  87. spacing: 10
  88. anchors{
  89. horizontalCenter: parent.horizontalCenter
  90. bottom: parent.bottom
  91. bottomMargin: 20
  92. }
  93. visible: showIndicator
  94. Repeater{
  95. model: list_view.count
  96. Rectangle{
  97. width: 8
  98. height: 8
  99. radius: 4
  100. visible: {
  101. if(index===0 || index===list_view.count-1)
  102. return false
  103. return true
  104. }
  105. border.width: 1
  106. border.color: FluColors.Grey100
  107. color: list_view.currentIndex === index ? FluTheme.primaryColor.dark : Qt.rgba(1,1,1,0.5)
  108. }
  109. }
  110. }
  111. Timer{
  112. id:timer_anim
  113. interval: 250
  114. onTriggered: {
  115. list_view.highlightMoveDuration = 0
  116. if(list_view.currentIndex === list_view.count-1){
  117. list_view.currentIndex = 1
  118. }
  119. }
  120. }
  121. Timer{
  122. id:timer_run
  123. interval: control.loopTime
  124. repeat: true
  125. onTriggered: {
  126. list_view.highlightMoveDuration = 250
  127. list_view.currentIndex = list_view.currentIndex+1
  128. timer_anim.start()
  129. }
  130. }
  131. }