选择线条颜色

This commit is contained in:
toly
2023-05-09 08:03:19 +08:00
parent c58c351428
commit 2f9e47cddc
2 changed files with 91 additions and 8 deletions

View File

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

View File

@@ -2,6 +2,7 @@ import 'dart:math';
import 'dart:ui'; import 'dart:ui';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_first_station/paper/color_selector.dart';
import 'package:flutter_first_station/paper/conform_dialog.dart'; import 'package:flutter_first_station/paper/conform_dialog.dart';
import 'model.dart'; import 'model.dart';
@@ -31,6 +32,17 @@ class _PaperState extends State<Paper> {
Colors.blue, Colors.blue,
Colors.indigo, Colors.indigo,
Colors.purple, Colors.purple,
Colors.pink,
// Colors.grey,
// Colors.redAccent,
// Colors.orangeAccent,
// Colors.yellowAccent,
// Colors.greenAccent,
// Colors.blueAccent,
// Colors.indigoAccent,
// Colors.purpleAccent,
// Colors.pinkAccent,
]; ];
// 支持的线粗 // 支持的线粗
@@ -43,6 +55,7 @@ class _PaperState extends State<Paper> {
onClear: _showClearDialog, onClear: _showClearDialog,
), ),
body: Stack( body: Stack(
alignment: Alignment.bottomLeft,
children: [ children: [
GestureDetector( GestureDetector(
onPanStart: _onPanStart, onPanStart: _onPanStart,
@@ -52,15 +65,24 @@ class _PaperState extends State<Paper> {
child: ConstrainedBox(constraints: const BoxConstraints.expand()), child: ConstrainedBox(constraints: const BoxConstraints.expand()),
), ),
), ),
Positioned(
bottom: 40,
child: ColorSelector(
supportColors: supportColors,
activeIndex: _activeColorIndex,
onSelect: _onSelectColor,
),
),
Positioned( Positioned(
bottom: 0, bottom: 0,
right: 10, right: 10,
child: StorkWidthSelector( child: StorkWidthSelector(
supportStorkWidths: supportStorkWidths, supportStorkWidths: supportStorkWidths,
color: Colors.black, color: supportColors[_activeColorIndex],
activeIndex: _activeStorkWidthIndex, activeIndex: _activeStorkWidthIndex,
onSelect: _onSelectStorkWidth, onSelect: _onSelectStorkWidth,
)), )
),
], ],
), ),
); );
@@ -88,6 +110,7 @@ class _PaperState extends State<Paper> {
_lines.add(Line( _lines.add(Line(
points: [details.localPosition], points: [details.localPosition],
strokeWidth: supportStorkWidths[_activeStorkWidthIndex], strokeWidth: supportStorkWidths[_activeStorkWidthIndex],
color: supportColors[_activeColorIndex],
)); ));
} }
@@ -103,6 +126,14 @@ class _PaperState extends State<Paper> {
}); });
} }
} }
void _onSelectColor(int index) {
if (index != _activeColorIndex) {
setState(() {
_activeColorIndex = index;
});
}
}
} }
class PaperPainter extends CustomPainter { class PaperPainter extends CustomPainter {