This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-demo/src/StickDemo.ts

131 lines
5.2 KiB
TypeScript
Raw Normal View History

import { Panel, Group, scroller, vlayout, image, layoutConfig, LayoutSpec, Input, Gravity, log, stack, hlayout, text, CENTER, slider, slideItem, modal, Slider, Text, Color, View, Stack, animate, flowlayout, FlowLayoutItem, NestedSlider, ScaleType } from "doric";
2019-12-07 15:50:37 +08:00
import { title, colors } from "./utils";
function tab(idx: number, title: string, sliderView: Slider) {
return text({
text: title,
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().just().configWeight(1),
2019-12-07 15:50:37 +08:00
height: 41,
onClick: () => {
sliderView.slidePage(context, 0, true)
},
})
}
@Entry
class StickDemo extends Panel {
private tabs!: Text[]
private indicator!: View
private sliderView!: NestedSlider
build(root: Group) {
this.indicator = new Stack
this.indicator.backgroundColor = colors[0]
this.indicator.width = 20
this.indicator.height = 2
scroller(
vlayout([
2019-12-09 20:34:28 +08:00
stack([
image({
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().most(),
2019-12-09 20:34:28 +08:00
imageUrl: "https://p.upyun.com/demo/webp/webp/jpg-0.webp",
scaleType: ScaleType.ScaleAspectFill,
}),
]).apply({
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().most().configHeight(LayoutSpec.JUST),
2019-12-07 15:50:37 +08:00
height: 200,
backgroundColor: colors[0],
}),
stack([
hlayout([
...this.tabs = [0, 1, 2].map(idx => {
return text({
text: `Tab ${idx}`,
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().just().configWeight(1),
2019-12-07 15:50:37 +08:00
height: 41,
onClick: () => {
this.sliderView.slidePage(context, idx, true)
},
})
})
]).apply({
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().most(),
2019-12-07 15:50:37 +08:00
gravity: Gravity.Center,
}),
2019-12-07 15:50:37 +08:00
this.indicator,
]).apply({
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().most().configHeight(LayoutSpec.JUST),
2019-12-07 15:50:37 +08:00
height: 57,
}),
(new NestedSlider).also(v => {
this.sliderView = v;
v.onPageSlided = (idx) => {
this.refreshTabs(idx)
}
[0, 1, 2].map(idx => {
return flowlayout({
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().just(),
2019-12-07 15:50:37 +08:00
width: root.width,
height: root.height - 57,
itemCount: 100,
columnCount: 2,
columnSpace: 10,
rowSpace: 10,
renderItem: (itemIdx) => {
return new FlowLayoutItem().apply({
backgroundColor: colors[itemIdx % colors.length],
height: 50,
layoutConfig: layoutConfig().configWidth(LayoutSpec.MOST),
2019-12-07 15:50:37 +08:00
}).also(it => {
it.addChild(text({
text: `In Page ${idx},${itemIdx}`,
textColor: Color.WHITE,
textSize: 20,
2020-01-03 13:35:40 +08:00
layoutConfig: layoutConfig().fit().configAlignment(Gravity.Center)
2019-12-07 15:50:37 +08:00
}).also(v => {
v.onClick = () => {
v.text = "Clicked"
}
}))
})
},
})
}).forEach(e => {
v.addSlideItem(e)
})
}).apply({
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().just(),
2019-12-07 15:50:37 +08:00
width: root.width,
height: root.height - 57,
}),
])
.also(it => {
2019-12-14 16:32:04 +08:00
it.layoutConfig = layoutConfig().most().configHeight(LayoutSpec.FIT)
2019-12-07 15:50:37 +08:00
}))
.apply({
2019-12-14 16:32:04 +08:00
layoutConfig: layoutConfig().most()
2019-12-07 15:50:37 +08:00
})
.in(root)
this.indicator.centerX = this.getRootView().width / this.tabs.length / 2
this.refreshTabs(0)
}
refreshTabs(page: number) {
this.tabs.forEach((e, idx) => {
if (idx == page) {
e.textColor = colors[0]
} else {
e.textColor = Color.BLACK
}
})
2020-01-03 13:35:40 +08:00
this.indicator.layoutConfig = layoutConfig().just().configAlignment(Gravity.Bottom).configMargin({ bottom: 13 })
2019-12-14 14:20:05 +08:00
animate(context)({
2019-12-07 15:50:37 +08:00
animations: () => {
this.indicator.centerX = this.getRootView().width / this.tabs.length * (page + 0.5)
},
duration: 300,
})
}
}