选择线条粗细

This commit is contained in:
toly
2023-05-09 07:17:18 +08:00
parent 45eae5c947
commit c58c351428
2 changed files with 98 additions and 25 deletions

View File

@@ -6,6 +6,7 @@ import 'package:flutter_first_station/paper/conform_dialog.dart';
import 'model.dart'; import 'model.dart';
import 'paper_app_bar.dart'; import 'paper_app_bar.dart';
import 'stork_width_selector.dart';
class Paper extends StatefulWidget { class Paper extends StatefulWidget {
const Paper({Key? key}) : super(key: key); const Paper({Key? key}) : super(key: key);
@@ -22,14 +23,18 @@ class _PaperState extends State<Paper> {
// 支持的颜色 // 支持的颜色
final List<Color> supportColors = [ final List<Color> supportColors = [
Colors.black, Colors.red, Colors.orange, Colors.black,
Colors.yellow, Colors.green, Colors.blue, Colors.red,
Colors.indigo, Colors.purple, Colors.orange,
Colors.yellow,
Colors.green,
Colors.blue,
Colors.indigo,
Colors.purple,
]; ];
// 支持的线粗 // 支持的线粗
final List<double> supportStorkWidths = [1,2, 4, 6, 8, 10]; final List<double> supportStorkWidths = [1, 2, 4, 6, 8, 10];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -37,15 +42,26 @@ class _PaperState extends State<Paper> {
appBar: PaperAppBar( appBar: PaperAppBar(
onClear: _showClearDialog, onClear: _showClearDialog,
), ),
body: GestureDetector( body: Stack(
onPanStart: _onPanStart, children: [
onPanUpdate: _onPanUpdate, GestureDetector(
child: CustomPaint( onPanStart: _onPanStart,
painter: PaperPainter( onPanUpdate: _onPanUpdate,
lines: _lines child: CustomPaint(
painter: PaperPainter(lines: _lines),
child: ConstrainedBox(constraints: const BoxConstraints.expand()),
),
), ),
child: ConstrainedBox(constraints: const BoxConstraints.expand()), Positioned(
), bottom: 0,
right: 10,
child: StorkWidthSelector(
supportStorkWidths: supportStorkWidths,
color: Colors.black,
activeIndex: _activeStorkWidthIndex,
onSelect: _onSelectStorkWidth,
)),
],
), ),
); );
} }
@@ -55,30 +71,37 @@ class _PaperState extends State<Paper> {
showDialog( showDialog(
context: context, context: context,
builder: (ctx) => ConformDialog( builder: (ctx) => ConformDialog(
title: '清空提示', title: '清空提示',
conformText: '确定', conformText: '确定',
msg: msg, msg: msg,
onConform: _clear, onConform: _clear,
)); ));
} }
void _clear(){ void _clear() {
_lines.clear(); _lines.clear();
Navigator.of(context).pop(); Navigator.of(context).pop();
setState(() { setState(() {});
});
} }
void _onPanStart(DragStartDetails details) { void _onPanStart(DragStartDetails details) {
_lines.add(Line(points: [details.localPosition],)); _lines.add(Line(
points: [details.localPosition],
strokeWidth: supportStorkWidths[_activeStorkWidthIndex],
));
} }
void _onPanUpdate(DragUpdateDetails details) { void _onPanUpdate(DragUpdateDetails details) {
_lines.last.points.add(details.localPosition); _lines.last.points.add(details.localPosition);
setState(() { setState(() {});
}
}); void _onSelectStorkWidth(int index) {
if (index != _activeStorkWidthIndex) {
setState(() {
_activeStorkWidthIndex = index;
});
}
} }
} }

View File

@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
class StorkWidthSelector extends StatelessWidget {
final List<double> supportStorkWidths;
final ValueChanged<int> onSelect;
final int activeIndex;
final Color color;
const StorkWidthSelector({
Key? key,
required this.supportStorkWidths,
required this.activeIndex,
required this.onSelect,
required this.color,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: List.generate(
supportStorkWidths.length,
_buildByIndex,
)),
);
}
Widget _buildByIndex(int index) {
bool select = index == activeIndex;
return GestureDetector(
onTap: () => onSelect(index),
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 2),
width: 70,
height: 18,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: select ? Border.all(color: Colors.blue) : null),
child: Container(
width: 50,
color: color,
height: supportStorkWidths[index],
),
),
);
}
}