This commit is contained in:
toly
2023-12-11 21:53:27 +08:00
parent 838d6aa668
commit 25e51d789f
104 changed files with 6414 additions and 19 deletions

View File

@@ -0,0 +1,84 @@
import 'package:flutter/material.dart';
/// create by 张风捷特烈 on 2020/5/1
/// contact me by email 1981462002@qq.com
/// 说明:
@immutable
class Coordinate {
final double step;
final double strokeWidth;
final Color axisColor;
final Color gridColor;
Coordinate(
{this.step = 20,
this.strokeWidth = .5,
this.axisColor = Colors.blue,
this.gridColor = Colors.grey});
final Paint _gridPaint = Paint();
void paint(Canvas canvas, Size size) {
canvas.save();
canvas.translate(size.width / 2, size.height / 2);
_drawGrid(canvas, size);
_drawAxis(canvas, size);
canvas.restore();
}
void _drawGrid(Canvas canvas, Size size) {
_gridPaint
..style = PaintingStyle.stroke
..strokeWidth = .5
..color = Colors.grey;
_drawBottomRight(canvas, size);
canvas.save();
canvas.scale(1, -1); //沿x轴镜像
_drawBottomRight(canvas, size);
canvas.restore();
canvas.save();
canvas.scale(-1, 1); //沿y轴镜像
_drawBottomRight(canvas, size);
canvas.restore();
canvas.save();
canvas.scale(-1, -1); //沿原点镜像
_drawBottomRight(canvas, size);
canvas.restore();
}
void _drawAxis(Canvas canvas, Size size) {
_gridPaint
..color = Colors.blue
..strokeWidth = 1.5;
canvas.drawLine(
Offset(-size.width / 2, 0), Offset(size.width / 2, 0), _gridPaint);
canvas.drawLine(
Offset(0, -size.height / 2), Offset(0, size.height / 2), _gridPaint);
canvas.drawLine(Offset(0, size.height / 2),
Offset(0 - 7.0, size.height / 2 - 10), _gridPaint);
canvas.drawLine(Offset(0, size.height / 2),
Offset(0 + 7.0, size.height / 2 - 10), _gridPaint);
canvas.drawLine(
Offset(size.width / 2, 0), Offset(size.width / 2 - 10, 7), _gridPaint);
canvas.drawLine(
Offset(size.width / 2, 0), Offset(size.width / 2 - 10, -7), _gridPaint);
}
void _drawBottomRight(Canvas canvas, Size size) {
canvas.save();
for (int i = 0; i < size.height / 2 / step; i++) {
canvas.drawLine(Offset(0, 0), Offset(size.width / 2, 0), _gridPaint);
canvas.translate(0, step);
}
canvas.restore();
canvas.save();
for (int i = 0; i < size.width / 2 / step; i++) {
canvas.drawLine(Offset(0, 0), Offset(0, size.height / 2), _gridPaint);
canvas.translate(step , 0);
}
canvas.restore();
}
}

View File

@@ -0,0 +1,164 @@
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
/// create by 张风捷特烈 on 2020/5/1
/// contact me by email 1981462002@qq.com
/// 说明:
@immutable
class Coordinate {
final double step;
final double strokeWidth;
final Color axisColor;
final Color gridColor;
final TextPainter _textPainter = TextPainter(textDirection: TextDirection.ltr);
Coordinate(
{this.step = 20,
this.strokeWidth = .5,
this.axisColor = Colors.blue,
this.gridColor = Colors.grey});
final Paint _gridPaint = Paint();
final Path _gridPath = Path();
void paint(Canvas canvas, Size size) {
canvas.save();
canvas.translate(size.width / 2, size.height / 2);
_drawGridLine(canvas, size);
_drawAxis(canvas, size);
_drawText(canvas, size);
canvas.restore();
}
void _drawAxis(Canvas canvas, Size size) {
_gridPaint
..color = Colors.blue
..strokeWidth = 1.5;
canvas.drawLine(
Offset(-size.width / 2, 0), Offset(size.width / 2, 0), _gridPaint);
canvas.drawLine(
Offset(0, -size.height / 2), Offset(0, size.height / 2), _gridPaint);
canvas.drawLine(Offset(0, size.height / 2),
Offset(0 - 7.0, size.height / 2 - 10), _gridPaint);
canvas.drawLine(Offset(0, size.height / 2),
Offset(0 + 7.0, size.height / 2 - 10), _gridPaint);
canvas.drawLine(
Offset(size.width / 2, 0), Offset(size.width / 2 - 10, 7), _gridPaint);
canvas.drawLine(
Offset(size.width / 2, 0), Offset(size.width / 2 - 10, -7), _gridPaint);
}
void _drawGridLine(Canvas canvas, Size size) {
_gridPaint
..style = PaintingStyle.stroke
..strokeWidth = .5
..color = Colors.grey;
for (int i = 0; i < size.width / 2 / step; i++) {
_gridPath.moveTo(step * i, -size.height / 2 );
_gridPath.relativeLineTo(0, size.height);
_gridPath.moveTo(-step * i, -size.height / 2 );
_gridPath.relativeLineTo(0, size.height);
}
for (int i = 0; i < size.height / 2 / step; i++) {
_gridPath.moveTo(-size.width / 2,step * i );
_gridPath.relativeLineTo(size.width,0 );
_gridPath.moveTo(-size.width / 2,-step * i, );
_gridPath.relativeLineTo(size.width,0 );
}
canvas.drawPath(_gridPath, _gridPaint);
}
void _drawAxisText(Canvas canvas, String str,
{Color color = Colors.black, bool? x = false}) {
TextSpan text = TextSpan(
text: str,
style: TextStyle(
fontSize: 11,
color: color,
));
_textPainter.text = text;
_textPainter.layout(); // 进行布局
Size size = _textPainter.size;
Offset offset = Offset.zero;
if (x == null) {
offset = Offset(-size.width*1.5, size.width*0.7);
} else if (x) {
offset = Offset(-size.width / 2, size.height / 2);
} else {
offset = Offset(size.height / 2, -size.height / 2 + 2);
}
_textPainter.paint(canvas, offset);
}
void _drawText(Canvas canvas, Size size) {
// y > 0 轴 文字
canvas.save();
for (int i = 0; i < size.height / 2 / step; i++) {
if (step < 30 && i.isOdd || i == 0) {
canvas.translate(0, step);
continue;
} else {
var str = (i * step).toInt().toString();
_drawAxisText(canvas, str, color: Colors.green);
}
canvas.translate(0, step);
}
canvas.restore();
// x > 0 轴 文字
canvas.save();
for (int i = 0; i < size.width / 2 / step; i++) {
if (i == 0) {
_drawAxisText(canvas, "O", color: Colors.black, x: null);
canvas.translate(step, 0);
continue;
}
if (step < 30 && i.isOdd) {
canvas.translate(step, 0);
continue;
} else {
var str = (i * step).toInt().toString();
_drawAxisText(canvas, str, color: Colors.green, x: true);
}
canvas.translate(step, 0);
}
canvas.restore();
// y < 0 轴 文字
canvas.save();
for (int i = 0; i < size.height / 2 / step; i++) {
if (step < 30 && i.isOdd || i == 0) {
canvas.translate(0, -step);
continue;
} else {
var str = (-i * step).toInt().toString();
_drawAxisText(canvas, str, color: Colors.green);
}
canvas.translate(0, -step);
}
canvas.restore();
// x < 0 轴 文字
canvas.save();
for (int i = 0; i < size.width / 2 / step; i++) {
if (step < 30 && i.isOdd || i == 0) {
canvas.translate(-step, 0);
continue;
} else {
var str = (-i * step).toInt().toString();
_drawAxisText(canvas, str, color: Colors.green, x: true);
}
canvas.translate(-step, 0);
}
canvas.restore();
}
}

View File

@@ -0,0 +1,119 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class DemoShower extends StatefulWidget {
final List<Widget> demos;
const DemoShower({super.key, required this.demos});
@override
State<DemoShower> createState() => _DemoShowerState();
}
class _DemoShowerState extends State<DemoShower> {
PageController _ctrl = PageController();
int _index = 0;
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.bottomCenter,
children: [
PageView(
controller: _ctrl,
children: widget.demos,
),
Positioned(
bottom: 20,
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
TolyIconButton(
onTap: (){
_index= (_index-1)%widget.demos.length;
setState(() {
});
_ctrl.animateToPage(_index,curve: Curves.easeIn,duration: Duration(milliseconds: 200));
},
iconData: Icons.navigate_before,
size: 36,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Text('${_index+1}/${widget.demos.length}',style: TextStyle(fontSize: 16,color: Colors.grey),),
),
TolyIconButton(
onTap: (){
_index= (_index+1)%widget.demos.length;
setState(() {
});
_ctrl.animateToPage(_index,curve: Curves.easeIn,duration: Duration(milliseconds: 200));
},
size: 36,
iconData: Icons.navigate_next,
),
],
)),
],
);
}
}
class TolyIconButton extends StatefulWidget {
final IconData iconData;
final VoidCallback onTap;
final double size;
const TolyIconButton({
super.key,
required this.iconData,
required this.size, required this.onTap,
});
@override
State<TolyIconButton> createState() => _TolyIconButtonState();
}
class _TolyIconButtonState extends State<TolyIconButton> {
bool _hover = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onTap,
child: MouseRegion(
onEnter: _onEnter,
onExit: _onExit,
cursor: SystemMouseCursors.click,
child: Container(
width: widget.size,
height: widget.size,
decoration: BoxDecoration(
color: _hover?Colors.grey.withOpacity(0.6):Colors.grey.withOpacity(0.5), shape: BoxShape.circle),
child: Icon(
widget.iconData,
size: 26,
color: Colors.white,
),
),
),
);
}
void _onEnter(PointerEnterEvent event) {
setState(() {
_hover = true;
});
}
void _onExit(PointerExitEvent event) {
setState(() {
_hover = false;
});
}
}