
我需要一個可重新排列的三角形作為容器小部件上方的指示器,它還可以應用容器的漸變。
現在紅色三角形是一種自定義油漆,但我愿意以另一種方式來做。它的位置必須是可定制的。一切都很好,直到漸變發揮作用,因為我也不能將它附加到三角形。
所以我需要的是一個小部件,它結合了兩個元素(三角形和容器),并讓我為它定義一個裝飾,就像普通容器一樣。關于如何解決類似問題的任何想法?
uj5u.com熱心網友回復:
使用 ClipPath()
ClipPath(
clipper: ArrowClipper(),
child: Container(
height: arrowHeight,
width: 40,
decoration: BoxDecoration(
//Your gradient...
),
),
)
...
class ArrowClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
final Path path = Path();
path.moveTo(0, size.height);
path.lineTo(size.width / 2, 0);
path.lineTo(size.width, size.height);
return path;
}
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => true;
}
uj5u.com熱心網友回復:
作為 ClipPath 的替代方法,您可以在按鈕容器后面使用帶有旋轉容器的 Stack。這應該為回應式 UI 提供一點錯誤余地。(為簡潔起見,我沒有設定藍色按鈕容器的樣式,因為您似乎沒問題。)

您可以使用第一個 Positionedleft:屬性控制指標的位置。
您可以在 DartPad 中運行以下代碼。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: 300,
height: 150,
child: Stack(
children: [
Positioned(
top: 12.5,
left: 100,
child: Transform.rotate(
angle: 0.7854,
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.red,
Colors.orange,
],
begin: FractionalOffset(0.0, 0.0),
end: FractionalOffset(1.0, 1.0),
stops: [0.0, 1.0],
tileMode: TileMode.clamp,
)
),
)
),
),
Positioned(
top: 20,
child: Container(
width: 300,
height: 100,
color: Colors.blue,
child: const SizedBox(width: 300, height: 100)
),
)
])
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360406.html
