Created
October 17, 2024 21:12
-
-
Save flar/18753764f8ea5f024a172b75ab1bac3d to your computer and use it in GitHub Desktop.
Test of very tiny join decorations (green is path, blue is 2 lines)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
StrokeJoin join = StrokeJoin.miter; | |
double offset = 1.0; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
CustomPaint( | |
foregroundPainter: _MyPainter(StrokeJoin.miter, offset), | |
size: const Size(400, 400), | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
DropdownButton( | |
value: join, | |
items: StrokeJoin.values.map((e) { | |
return DropdownMenuItem<StrokeJoin>( | |
value: e, | |
child: Text('$e'), | |
); | |
}).toList(), | |
onChanged: (value) { | |
setState(() { | |
join = value!; | |
}); | |
}, | |
), | |
Slider( | |
value: offset, | |
min: -5.0, | |
max: 5.0, | |
onChanged: (value) { | |
setState(() { | |
offset = value; | |
}); | |
},) | |
], | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class _MyPainter extends CustomPainter { | |
_MyPainter(this.join, this.offset); | |
final StrokeJoin join; | |
final double offset; | |
void drawStringAboveCenter(Canvas canvas, String label, double centerX, double centerY) { | |
TextSpan span = TextSpan(text: label, style: const TextStyle(color: Colors.black)); | |
TextPainter painter = TextPainter( | |
text: span, | |
textDirection: TextDirection.ltr, | |
); | |
painter.layout(); | |
painter.paint(canvas, Offset(centerX - painter.width * 0.5, | |
centerY - painter.height)); | |
} | |
@override | |
void paint(Canvas canvas, Size size) { | |
Paint paint = Paint(); | |
paint.style = PaintingStyle.stroke; | |
paint.strokeJoin = join; | |
paint.strokeWidth = 50.0; | |
drawStringAboveCenter(canvas, 'path', 200, 120); | |
paint.color = Colors.green; | |
{ | |
Path path = Path(); | |
path.moveTo(50, 150); | |
path.lineTo(150, 150); | |
path.lineTo(250, 150 + offset); | |
canvas.drawPath(path, paint); | |
} | |
{ | |
Path path = Path(); | |
path.moveTo(350, 50); | |
path.lineTo(350, 150); | |
path.lineTo(350 + offset, 250); | |
canvas.drawPath(path, paint); | |
} | |
drawStringAboveCenter(canvas, '2 lines', 200, 220); | |
paint.color = Colors.blueAccent; | |
canvas.drawLine(const Offset(50, 250), const Offset(150, 250), paint); | |
canvas.drawLine(const Offset(150, 250), Offset(250, 250 + offset), paint); | |
} | |
@override | |
bool shouldRepaint(covariant CustomPainter oldDelegate) => false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment