parent
cb92202d00
commit
7c2e3d6ac6
3 changed files with 184 additions and 146 deletions
@ -0,0 +1,149 @@ |
||||
import 'package:flutter/material.dart'; |
||||
import 'package:flutter_colorpicker/flutter_colorpicker.dart' |
||||
show ColorPicker, MaterialPicker, colorToHex; |
||||
|
||||
import '../../../../../translations.dart'; |
||||
import '../../../../models/documents/style.dart'; |
||||
import 'color.dart' show hexToColor; |
||||
|
||||
class ColorPickerDialog extends StatefulWidget { |
||||
const ColorPickerDialog({ |
||||
required this.isBackground, |
||||
required this.onRequestChangeColor, |
||||
required this.isToggledColor, |
||||
required this.selectionStyle, |
||||
super.key, |
||||
}); |
||||
final bool isBackground; |
||||
|
||||
final bool isToggledColor; |
||||
final Function(BuildContext context, Color color) onRequestChangeColor; |
||||
final Style selectionStyle; |
||||
|
||||
@override |
||||
State<ColorPickerDialog> createState() => ColorPickerDialogState(); |
||||
} |
||||
|
||||
class ColorPickerDialogState extends State<ColorPickerDialog> { |
||||
var pickerType = 'material'; |
||||
var selectedColor = Colors.black; |
||||
|
||||
late final TextEditingController hexController; |
||||
late void Function(void Function()) colorBoxSetState; |
||||
|
||||
@override |
||||
void initState() { |
||||
super.initState(); |
||||
hexController = TextEditingController(text: colorToHex(selectedColor)); |
||||
if (widget.isToggledColor) { |
||||
selectedColor = widget.isBackground |
||||
? hexToColor(widget.selectionStyle.attributes['background']?.value) |
||||
: hexToColor(widget.selectionStyle.attributes['color']?.value); |
||||
} |
||||
} |
||||
|
||||
@override |
||||
Widget build(BuildContext context) { |
||||
return AlertDialog( |
||||
title: Text(context.loc.selectColor), |
||||
actions: [ |
||||
TextButton( |
||||
onPressed: () { |
||||
Navigator.of(context).pop(); |
||||
}, |
||||
child: Text(context.loc.ok)), |
||||
], |
||||
backgroundColor: Theme.of(context).canvasColor, |
||||
content: SingleChildScrollView( |
||||
child: Column( |
||||
mainAxisSize: MainAxisSize.min, |
||||
children: [ |
||||
Row( |
||||
children: [ |
||||
TextButton( |
||||
onPressed: () { |
||||
setState(() { |
||||
pickerType = 'material'; |
||||
}); |
||||
}, |
||||
child: Text(context.loc.material)), |
||||
TextButton( |
||||
onPressed: () { |
||||
setState(() { |
||||
pickerType = 'color'; |
||||
}); |
||||
}, |
||||
child: Text(context.loc.color)), |
||||
], |
||||
), |
||||
Column( |
||||
children: [ |
||||
if (pickerType == 'material') |
||||
MaterialPicker( |
||||
pickerColor: selectedColor, |
||||
onColorChanged: (color) { |
||||
widget.onRequestChangeColor(context, color); |
||||
Navigator.of(context).pop(); |
||||
}, |
||||
), |
||||
if (pickerType == 'color') |
||||
ColorPicker( |
||||
pickerColor: selectedColor, |
||||
onColorChanged: (color) { |
||||
widget.onRequestChangeColor(context, color); |
||||
hexController.text = colorToHex(color); |
||||
selectedColor = color; |
||||
colorBoxSetState(() {}); |
||||
}, |
||||
), |
||||
const SizedBox( |
||||
height: 10, |
||||
), |
||||
Row( |
||||
children: [ |
||||
SizedBox( |
||||
width: 100, |
||||
height: 60, |
||||
child: TextFormField( |
||||
controller: hexController, |
||||
onChanged: (value) { |
||||
selectedColor = hexToColor(value); |
||||
widget.onRequestChangeColor(context, selectedColor); |
||||
|
||||
colorBoxSetState(() {}); |
||||
}, |
||||
decoration: InputDecoration( |
||||
labelText: context.loc.hex, |
||||
border: const OutlineInputBorder(), |
||||
), |
||||
), |
||||
), |
||||
const SizedBox( |
||||
width: 10, |
||||
), |
||||
StatefulBuilder( |
||||
builder: (context, mcolorBoxSetState) { |
||||
colorBoxSetState = mcolorBoxSetState; |
||||
return Container( |
||||
width: 25, |
||||
height: 25, |
||||
decoration: BoxDecoration( |
||||
border: Border.all( |
||||
color: Colors.black45, |
||||
), |
||||
color: selectedColor, |
||||
borderRadius: BorderRadius.circular(5), |
||||
), |
||||
); |
||||
}, |
||||
), |
||||
], |
||||
), |
||||
], |
||||
) |
||||
], |
||||
), |
||||
), |
||||
); |
||||
} |
||||
} |
Loading…
Reference in new issue