Skip to content

Commit 571ed9b

Browse files
authored
fix: create color palette based on display's supported colors (#40)
* Fixed color palettes according to the display and set initial color to black * Added color provider * Fixed initialisation of color * fixed comment * implemented getIt for ColorPaletteProvider * updated pubspec.yaml for getit package * Used ChangeNotifierProvider in display_selection_screen.dart instead of main.dart and used getit to get the colors in color_picker.dart
1 parent 521bff8 commit 571ed9b

File tree

9 files changed

+93
-73
lines changed

9 files changed

+93
-73
lines changed

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import 'package:flutter/material.dart';
2+
import 'package:magic_epaper_app/provider/getitlocator.dart';
23
import 'package:magic_epaper_app/provider/image_loader.dart';
34
import 'package:provider/provider.dart';
45
import 'package:magic_epaper_app/view/display_selection_screen.dart';
56

67
void main() {
8+
setupLocator();
79
runApp(MultiProvider(providers: [
810
ChangeNotifierProvider(create: (context) => ImageLoader()),
911
], child: const MyApp()));

lib/pro_image_editor/features/bottom_bar.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class BottomBarCustom extends StatefulWidget {
3333
required this.configs,
3434
required this.strokeWidth,
3535
required this.onSetLineWidth,
36-
required this.initColor,
3736
required this.onColorChanged,
3837
this.iconStrokeWidthThin = ProImageEditorIcons.penSize1,
3938
this.iconStrokeWidthMedium = ProImageEditorIcons.penSize2,
@@ -62,7 +61,7 @@ class BottomBarCustom extends StatefulWidget {
6261
///
6362
/// This color sets the initial paint color, providing a starting point
6463
/// for color customization.
65-
final Color initColor;
64+
final Color initColor = Colors.black;
6665

6766
/// Callback function for handling color changes.
6867
///
@@ -136,7 +135,6 @@ class _BottomBarCustomState extends State<BottomBarCustom> {
136135
? Expanded(
137136
child: ColorPickerCustom(
138137
onColorChanged: widget.onColorChanged,
139-
initColor: widget.initColor,
140138
),
141139
)
142140
: _buildLineWidths(),

lib/pro_image_editor/features/color_picker.dart

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Flutter imports:
22
import 'package:flutter/material.dart';
3+
import 'package:magic_epaper_app/provider/color_palette_provider.dart';
4+
import 'package:magic_epaper_app/provider/getitlocator.dart';
35

46
/// A stateful widget that provides a color picker inspired by WhatsApp.
57
///
@@ -23,7 +25,6 @@ class ColorPickerCustom extends StatefulWidget {
2325
const ColorPickerCustom({
2426
super.key,
2527
required this.onColorChanged,
26-
required this.initColor,
2728
});
2829

2930
/// Callback for handling color changes.
@@ -36,26 +37,14 @@ class ColorPickerCustom extends StatefulWidget {
3637
///
3738
/// This color sets the initial value of the picker, providing a starting
3839
/// point for color selection.
39-
final Color initColor;
4040
4141
@override
4242
State<ColorPickerCustom> createState() => _ColorPickerCustomState();
4343
}
4444

4545
class _ColorPickerCustomState extends State<ColorPickerCustom> {
4646
Color _selectedColor = Colors.black;
47-
48-
final List<Color> _colors = [
49-
Colors.white,
50-
Colors.black,
51-
Colors.red,
52-
];
53-
54-
@override
55-
void initState() {
56-
super.initState();
57-
_selectedColor = widget.initColor;
58-
}
47+
final List<Color> _colors = getIt<ColorPaletteProvider>().colors;
5948

6049
@override
6150
Widget build(BuildContext context) {
@@ -67,10 +56,8 @@ class _ColorPickerCustomState extends State<ColorPickerCustom> {
6756
itemBuilder: (context, index) {
6857
Color color = _colors[index];
6958
bool selected = _selectedColor == color;
70-
7159
double size = !selected ? 20 : 24;
7260
double borderWidth = !selected ? 2.5 : 4;
73-
7461
return Center(
7562
child: GestureDetector(
7663
onTap: () {

lib/pro_image_editor/features/movable_background_image.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ class _MovableBackgroundImageExampleState
443443
bodyItems: _buildPaintEditorBody,
444444
),
445445
style: const PaintEditorStyle(
446+
initialColor: Colors.black,
446447
uiOverlayStyle: SystemUiOverlayStyle(
447448
statusBarColor: Colors.black,
448449
),
@@ -590,7 +591,6 @@ List<ReactiveWidget> _buildPaintEditorBody(
590591
builder: (_) => BottomBarCustom(
591592
configs: paintEditor.configs,
592593
strokeWidth: paintEditor.paintCtrl.strokeWidth,
593-
initColor: paintEditor.paintCtrl.color,
594594
onColorChanged: (color) {
595595
paintEditor.paintCtrl.setColor(color);
596596
paintEditor.uiPickerStream.add(null);

lib/pro_image_editor/features/text_bottom_bar.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ class _TextBottomBarState extends State<TextBottomBar> {
124124
? Expanded(
125125
child: ColorPickerCustom(
126126
onColorChanged: widget.onColorChanged,
127-
initColor: widget.initColor,
128127
),
129128
)
130129
: Expanded(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:flutter/material.dart';
2+
3+
class ColorPaletteProvider extends ChangeNotifier {
4+
List<Color> _colors = [];
5+
6+
List<Color> get colors => _colors;
7+
8+
void updateColors(List<Color> newColors) {
9+
_colors = newColors;
10+
notifyListeners();
11+
}
12+
}

lib/provider/getitlocator.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:get_it/get_it.dart';
2+
import 'package:magic_epaper_app/provider/color_palette_provider.dart';
3+
4+
final GetIt getIt = GetIt.instance;
5+
6+
void setupLocator() {
7+
getIt.registerLazySingleton<ColorPaletteProvider>(
8+
() => ColorPaletteProvider());
9+
}

lib/view/display_selection_screen.dart

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import 'package:flutter/material.dart';
22
import 'package:magic_epaper_app/constants.dart';
3+
import 'package:magic_epaper_app/provider/getitlocator.dart';
34
import 'package:magic_epaper_app/util/epd/epd.dart';
45
import 'package:magic_epaper_app/util/epd/gdey037z03.dart';
56
import 'package:magic_epaper_app/util/epd/gdey037z03bw.dart';
67
import 'package:magic_epaper_app/view/image_editor.dart';
8+
import 'package:provider/provider.dart';
9+
import 'package:magic_epaper_app/provider/color_palette_provider.dart';
710
import 'package:magic_epaper_app/view/widget/display_card.dart';
811

912
class DisplaySelectionScreen extends StatefulWidget {
@@ -19,60 +22,65 @@ class _DisplaySelectionScreenState extends State<DisplaySelectionScreen> {
1922

2023
@override
2124
Widget build(BuildContext context) {
22-
return Scaffold(
23-
backgroundColor: Colors.white,
24-
appBar: AppBar(
25-
backgroundColor: colorAccent,
26-
elevation: 0,
27-
title: const Padding(
28-
padding: EdgeInsets.fromLTRB(5, 16, 16, 5),
29-
child: Column(
30-
crossAxisAlignment: CrossAxisAlignment.start,
31-
children: [
32-
Text('Magic ePaper',
33-
style: TextStyle(
34-
fontSize: 24,
35-
fontWeight: FontWeight.bold,
36-
color: Colors.white,
37-
)),
38-
SizedBox(height: 8),
39-
Text('Select your ePaper display type',
40-
style: TextStyle(
41-
fontSize: 16,
42-
color: Colors.white,
43-
)),
44-
],
45-
),
46-
),
47-
toolbarHeight: 85,
48-
),
49-
body: SafeArea(
50-
child: Padding(
51-
padding: const EdgeInsets.fromLTRB(10.0, 14, 16.0, 16.0),
52-
child: Column(
53-
children: [
54-
Expanded(
55-
child: GridView.builder(
56-
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
57-
crossAxisCount: 2,
58-
childAspectRatio: 0.6,
59-
mainAxisSpacing: 8,
60-
crossAxisSpacing: 8,
61-
),
62-
itemCount: displays.length,
63-
itemBuilder: (context, index) => DisplayCard(
64-
display: displays[index],
65-
isSelected: selectedIndex == index,
66-
onTap: () => setState(() => selectedIndex = index),
67-
),
25+
return ChangeNotifierProvider<ColorPaletteProvider>(
26+
create: (_) => getIt<ColorPaletteProvider>(),
27+
builder: (context, child) {
28+
return Scaffold(
29+
backgroundColor: Colors.white,
30+
appBar: AppBar(
31+
backgroundColor: colorAccent,
32+
elevation: 0,
33+
title: const Padding(
34+
padding: EdgeInsets.fromLTRB(5, 16, 16, 5),
35+
child: Column(
36+
crossAxisAlignment: CrossAxisAlignment.start,
37+
children: [
38+
Text('Magic ePaper',
39+
style: TextStyle(
40+
fontSize: 24,
41+
fontWeight: FontWeight.bold,
42+
color: Colors.white,
43+
)),
44+
SizedBox(height: 8),
45+
Text('Select your ePaper display type',
46+
style: TextStyle(
47+
fontSize: 16,
48+
color: Colors.white,
49+
)),
50+
],
6851
),
6952
),
70-
_buildContinueButton(context),
71-
],
72-
),
73-
),
74-
),
75-
);
53+
toolbarHeight: 85,
54+
),
55+
body: SafeArea(
56+
child: Padding(
57+
padding: const EdgeInsets.fromLTRB(10.0, 14, 16.0, 16.0),
58+
child: Column(
59+
children: [
60+
Expanded(
61+
child: GridView.builder(
62+
gridDelegate:
63+
const SliverGridDelegateWithFixedCrossAxisCount(
64+
crossAxisCount: 2,
65+
childAspectRatio: 0.6,
66+
mainAxisSpacing: 8,
67+
crossAxisSpacing: 8,
68+
),
69+
itemCount: displays.length,
70+
itemBuilder: (context, index) => DisplayCard(
71+
display: displays[index],
72+
isSelected: selectedIndex == index,
73+
onTap: () => setState(() => selectedIndex = index),
74+
),
75+
),
76+
),
77+
_buildContinueButton(context),
78+
],
79+
),
80+
),
81+
),
82+
);
83+
});
7684
}
7785

7886
Widget _buildContinueButton(BuildContext context) {
@@ -83,6 +91,10 @@ class _DisplaySelectionScreenState extends State<DisplaySelectionScreen> {
8391
child: ElevatedButton(
8492
onPressed: isEnabled
8593
? () {
94+
context.read<ColorPaletteProvider>().updateColors(
95+
displays[selectedIndex].colors,
96+
);
97+
8698
Navigator.push(
8799
context,
88100
MaterialPageRoute(

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dependencies:
4141
mime: ^2.0.0
4242
intl: ^0.19.0
4343
path_provider: ^2.0.15
44+
get_it: ^8.0.3
4445

4546
dev_dependencies:
4647
flutter_test:

0 commit comments

Comments
 (0)