Refactor stringToColor function

pull/17/head
singerdmx 4 years ago
parent c3a9c115d4
commit 1f33d621f4
  1. 125
      lib/utils/color.dart
  2. 127
      lib/widgets/text_line.dart
  3. 16
      lib/widgets/toolbar.dart

@ -0,0 +1,125 @@
import 'dart:ui';
import 'package:flutter/material.dart';
Color stringToColor(String s) {
switch (s) {
case 'transparent':
return Colors.transparent;
case 'black':
return Colors.black;
case 'black12':
return Colors.black12;
case 'black26':
return Colors.black26;
case 'black38':
return Colors.black38;
case 'black45':
return Colors.black45;
case 'black54':
return Colors.black54;
case 'black87':
return Colors.black87;
case 'white':
return Colors.white;
case 'white10':
return Colors.white10;
case 'white12':
return Colors.white12;
case 'white24':
return Colors.white24;
case 'white30':
return Colors.white30;
case 'white38':
return Colors.white38;
case 'white54':
return Colors.white54;
case 'white60':
return Colors.white60;
case 'white70':
return Colors.white70;
case 'red':
return Colors.red;
case 'redAccent':
return Colors.redAccent;
case 'amber':
return Colors.amber;
case 'amberAccent':
return Colors.amberAccent;
case 'yellow':
return Colors.yellow;
case 'yellowAccent':
return Colors.yellowAccent;
case 'teal':
return Colors.teal;
case 'tealAccent':
return Colors.tealAccent;
case 'purple':
return Colors.purple;
case 'purpleAccent':
return Colors.purpleAccent;
case 'pink':
return Colors.pink;
case 'pinkAccent':
return Colors.pinkAccent;
case 'orange':
return Colors.orange;
case 'orangeAccent':
return Colors.orangeAccent;
case 'deepOrange':
return Colors.deepOrange;
case 'deepOrangeAccent':
return Colors.deepOrangeAccent;
case 'indigo':
return Colors.indigo;
case 'indigoAccent':
return Colors.indigoAccent;
case 'lime':
return Colors.lime;
case 'limeAccent':
return Colors.limeAccent;
case 'grey':
return Colors.grey;
case 'blueGrey':
return Colors.blueGrey;
case 'green':
return Colors.green;
case 'greenAccent':
return Colors.greenAccent;
case 'lightGreen':
return Colors.lightGreen;
case 'lightGreenAccent':
return Colors.lightGreenAccent;
case 'blue':
return Colors.blue;
case 'blueAccent':
return Colors.blueAccent;
case 'lightBlue':
return Colors.lightBlue;
case 'lightBlueAccent':
return Colors.lightBlueAccent;
case 'cyan':
return Colors.cyan;
case 'cyanAccent':
return Colors.cyanAccent;
case 'brown':
return Colors.brown;
}
if (s.startsWith('rgba')) {
s = s.substring(5); // trim left 'rgba('
s = s.substring(0, s.length - 1); // trim right ')'
final arr = s.split(',').map((e) => e.trim()).toList();
return Color.fromRGBO(int.parse(arr[0]), int.parse(arr[1]),
int.parse(arr[2]), double.parse(arr[3]));
}
if (!s.startsWith('#')) {
throw ("Color code not supported");
}
String hex = s.replaceFirst('#', '');
hex = hex.length == 6 ? 'ff' + hex : hex;
int val = int.parse(hex, radix: 16);
return Color(val);
}

@ -11,6 +11,7 @@ import 'package:flutter_quill/models/documents/nodes/leaf.dart';
import 'package:flutter_quill/models/documents/nodes/line.dart';
import 'package:flutter_quill/models/documents/nodes/node.dart';
import 'package:flutter_quill/models/documents/style.dart';
import 'package:flutter_quill/utils/color.dart';
import 'package:flutter_quill/widgets/cursor.dart';
import 'package:flutter_quill/widgets/proxy.dart';
import 'package:flutter_quill/widgets/text_selection.dart';
@ -111,128 +112,6 @@ class TextLine extends StatelessWidget {
return TextSpan(children: children, style: textStyle);
}
Color _hexStringToColor(String s) {
switch (s) {
case 'transparent':
return Colors.transparent;
case 'black':
return Colors.black;
case 'black12':
return Colors.black12;
case 'black26':
return Colors.black26;
case 'black38':
return Colors.black38;
case 'black45':
return Colors.black45;
case 'black54':
return Colors.black54;
case 'black87':
return Colors.black87;
case 'white':
return Colors.white;
case 'white10':
return Colors.white10;
case 'white12':
return Colors.white12;
case 'white24':
return Colors.white24;
case 'white30':
return Colors.white30;
case 'white38':
return Colors.white38;
case 'white54':
return Colors.white54;
case 'white60':
return Colors.white60;
case 'white70':
return Colors.white70;
case 'red':
return Colors.red;
case 'redAccent':
return Colors.redAccent;
case 'amber':
return Colors.amber;
case 'amberAccent':
return Colors.amberAccent;
case 'yellow':
return Colors.yellow;
case 'yellowAccent':
return Colors.yellowAccent;
case 'teal':
return Colors.teal;
case 'tealAccent':
return Colors.tealAccent;
case 'purple':
return Colors.purple;
case 'purpleAccent':
return Colors.purpleAccent;
case 'pink':
return Colors.pink;
case 'pinkAccent':
return Colors.pinkAccent;
case 'orange':
return Colors.orange;
case 'orangeAccent':
return Colors.orangeAccent;
case 'deepOrange':
return Colors.deepOrange;
case 'deepOrangeAccent':
return Colors.deepOrangeAccent;
case 'indigo':
return Colors.indigo;
case 'indigoAccent':
return Colors.indigoAccent;
case 'lime':
return Colors.lime;
case 'limeAccent':
return Colors.limeAccent;
case 'grey':
return Colors.grey;
case 'blueGrey':
return Colors.blueGrey;
case 'green':
return Colors.green;
case 'greenAccent':
return Colors.greenAccent;
case 'lightGreen':
return Colors.lightGreen;
case 'lightGreenAccent':
return Colors.lightGreenAccent;
case 'blue':
return Colors.blue;
case 'blueAccent':
return Colors.blueAccent;
case 'lightBlue':
return Colors.lightBlue;
case 'lightBlueAccent':
return Colors.lightBlueAccent;
case 'cyan':
return Colors.cyan;
case 'cyanAccent':
return Colors.cyanAccent;
case 'brown':
return Colors.brown;
}
if (s.startsWith('rgba')) {
s = s.substring(5); // trim left 'rgba('
s = s.substring(0, s.length - 1); // trim right ')'
final arr = s.split(',').map((e) => e.trim()).toList();
return Color.fromRGBO(int.parse(arr[0]), int.parse(arr[1]),
int.parse(arr[2]), double.parse(arr[3]));
}
if (!s.startsWith('#')) {
throw ("Color code not supported");
}
String hex = s.replaceFirst('#', '');
hex = hex.length == 6 ? 'ff' + hex : hex;
int val = int.parse(hex, radix: 16);
return Color(val);
}
TextSpan _getTextSpanFromNode(DefaultStyles defaultStyles, Node node) {
leaf.Text textNode = node as leaf.Text;
Style style = textNode.style;
@ -275,13 +154,13 @@ class TextLine extends StatelessWidget {
Attribute color = textNode.style.attributes[Attribute.color.key];
if (color != null && color.value != null) {
final textColor = _hexStringToColor(color.value);
final textColor = stringToColor(color.value);
res = res.merge(new TextStyle(color: textColor));
}
Attribute background = textNode.style.attributes[Attribute.background.key];
if (background != null && background.value != null) {
final backgroundColor = _hexStringToColor(background.value);
final backgroundColor = stringToColor(background.value);
res = res.merge(new TextStyle(backgroundColor: backgroundColor));
}

@ -5,6 +5,7 @@ import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:flutter_quill/models/documents/attribute.dart';
import 'package:flutter_quill/models/documents/nodes/embed.dart';
import 'package:flutter_quill/models/documents/style.dart';
import 'package:flutter_quill/utils/color.dart';
import 'package:image_picker/image_picker.dart';
import 'controller.dart';
@ -632,20 +633,20 @@ class _ColorButtonState extends State<ColorButton> {
Widget build(BuildContext context) {
final theme = Theme.of(context);
Color iconColor = _isToggledColor && !widget.background && !_isWhite
? fromHex(_selectionStyle.attributes["color"].value)
? stringToColor(_selectionStyle.attributes["color"].value)
: theme.iconTheme.color;
Color iconColorBackground =
_isToggledBackground && widget.background && !_isWhitebackground
? fromHex(_selectionStyle.attributes["background"].value)
? stringToColor(_selectionStyle.attributes["background"].value)
: theme.iconTheme.color;
Color fillColor = _isToggledColor && !widget.background && _isWhite
? fromHex('#ffffff')
? stringToColor('#ffffff')
: theme.canvasColor;
Color fillColorBackground =
_isToggledBackground && widget.background && _isWhitebackground
? fromHex('#ffffff')
? stringToColor('#ffffff')
: theme.canvasColor;
return QuillIconButton(
@ -671,13 +672,6 @@ class _ColorButtonState extends State<ColorButton> {
Navigator.of(context).pop();
}
Color fromHex(String hexString) {
final buffer = StringBuffer();
if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
buffer.write(hexString.replaceFirst('#', ''));
return Color(int.parse(buffer.toString(), radix: 16));
}
_showColorPicker() {
showDialog(
context: context,

Loading…
Cancel
Save