Skip to content

Commit

Permalink
Add colors
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Dec 10, 2023
1 parent df1cc35 commit 612e94c
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 113 deletions.
49 changes: 30 additions & 19 deletions lib/bike.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@ class BikePageState extends ConsumerState<BikePage> {
Center(
child: Column(
children: [
Text(
"Background Lock tries to keep your settings locked even when the app is closed. It may cause battery drain.",
style: Theme.of(context).textTheme.bodySmall,
textAlign: TextAlign.center,
),
const SizedBox(height: 10),
InkWell(
onTap: () {
Expand Down Expand Up @@ -321,6 +316,7 @@ class LightControlWidget extends ConsumerWidget {
children: [
Expanded(
child: DiscoverCard(
colorIndex: bike.color,
title: "Light",
metric: bike.light ? "On" : "Off",
selected: bike.light,
Expand Down Expand Up @@ -352,6 +348,7 @@ class ModeControlWidget extends ConsumerWidget {
children: [
Expanded(
child: DiscoverCard(
colorIndex: bike.color,
title: "Mode",
metric: bike.viewMode,
selected: bike.viewMode == '1' ? false : true,
Expand All @@ -375,20 +372,33 @@ class BackgroundLockControlWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
var bikeControl = ref.watch(bikeProvider(bike.id).notifier);
return Padding(
padding: const EdgeInsets.only(top: 20.0),
child: DiscoverCard(
title: "Background Lock",
metric: bike.modeLock ? "On" : "Off",
selected: bike.modeLock,
onTap: () async {
await Permission.notification.request();
if (Platform.isAndroid) {
await FlutterForegroundTask.requestIgnoreBatteryOptimization();
}
bikeControl.toggleBackgroundLock();
},
),
return Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: DiscoverCard(
title: "Background Lock",
metric: bike.modeLock ? "On" : "Off",
selected: bike.modeLock,
colorIndex: bike.color,
onTap: () async {
await Permission.notification.request();
if (Platform.isAndroid) {
await FlutterForegroundTask.requestIgnoreBatteryOptimization();
}
bikeControl.toggleBackgroundLock();
},
),
),
const SizedBox(
height: 10,
),
Text(
"Background Lock tries to keep your settings locked even when the app is closed. It may cause battery drain.",
style: Theme.of(context).textTheme.bodySmall,
textAlign: TextAlign.center,
),
],
);
}
}
Expand All @@ -406,6 +416,7 @@ class AssistControlWidget extends ConsumerWidget {
children: [
Expanded(
child: DiscoverCard(
colorIndex: bike.color,
title: "Assist",
metric: bike.assist.toString(),
selected: bike.assist == 0 ? false : true,
Expand Down
36 changes: 36 additions & 0 deletions lib/colors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const _colors = [
(start: 0xff441DFC, end: 0xff4E81EB),
(start: 0xff2E3192, end: 0xff1BFFFF),
(start: 0xffD4145A, end: 0xffFBB03B),
(start: 0xff009245, end: 0xffFCEE21),
(start: 0xff662D8C, end: 0xffED1E79),
(start: 0xffEE9CA7, end: 0xffFFDDE1),
(start: 0xff614385, end: 0xff516395),
(start: 0xff02AABD, end: 0xff00CDAC),
(start: 0xffFF512F, end: 0xffDD2476),
(start: 0xffFF5F6D, end: 0xffFFC371),
(start: 0xff11998E, end: 0xff38EF7D),
(start: 0xffC6EA8D, end: 0xffFE90AF),
(start: 0xffEA8D8D, end: 0xffA890FE),
(start: 0xffD8B5FF, end: 0xff1EAE98),
(start: 0xffFF61D2, end: 0xffFE9090),
(start: 0xffBFF098, end: 0xff6FD6FF),
(start: 0xff4E65FF, end: 0xff92EFFD),
(start: 0xffA9F1DF, end: 0xffFFBBBB),
(start: 0xffC33764, end: 0xff1D2671),
(start: 0xff93A5CF, end: 0xffE4EfE9),
(start: 0xff868F96, end: 0xff596164),
(start: 0xff09203F, end: 0xff537895),
(start: 0xffFFECD2, end: 0xffFCB69F),
(start: 0xffA1C4FD, end: 0xffC2E9FB),
(start: 0xff764BA2, end: 0xff667EEA),
(start: 0xffFDFCFB, end: 0xffE2D1C3)
];

getColor(int index) {
return _colors[index];
}

getColorList() {
return _colors.map((e) => (start: e.start, end: e.end)).toList();
}
83 changes: 82 additions & 1 deletion lib/edit_bike.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:superduper/bike.dart';
import 'package:superduper/colors.dart';

show(BuildContext context, BikeState bike) {
showModalBottomSheet<void>(
Expand Down Expand Up @@ -76,9 +77,16 @@ class CompleteForm extends ConsumerStatefulWidget {

class _CompleteFormState extends ConsumerState<CompleteForm> {
final _formKey = GlobalKey<FormBuilderState>();
late int _selectedColorIndex;

var genderOptions = ['Male', 'Female', 'Other'];

@override
void initState() {
super.initState();
_selectedColorIndex = widget.bike.color;
}

Future<bool?> _showMyDialog() async {
return showDialog<bool>(
context: context,
Expand Down Expand Up @@ -120,6 +128,7 @@ class _CompleteFormState extends ConsumerState<CompleteForm> {
@override
Widget build(BuildContext context) {
var bikeNotifier = ref.watch(bikeProvider(widget.bike.id).notifier);
final colors = getColorList();
return Padding(
padding: const EdgeInsets.all(10),
child: Column(
Expand All @@ -134,6 +143,7 @@ class _CompleteFormState extends ConsumerState<CompleteForm> {
initialValue: {
'name': widget.bike.name,
'region': widget.bike.region,
'color': widget.bike.color,
},
child: Column(
children: <Widget>[
Expand Down Expand Up @@ -162,11 +172,42 @@ class _CompleteFormState extends ConsumerState<CompleteForm> {
))
.toList(),
),
const SizedBox(height: 40),
InkWell(
onTap: () async {
var colorIndex =
await _showColorPicker(context, _selectedColorIndex);
setState(() {
_selectedColorIndex = colorIndex;
});
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(26),
gradient: LinearGradient(
colors: [
Color(colors[_selectedColorIndex].start),
Color(colors[_selectedColorIndex].end),
],
begin: Alignment.bottomLeft,
end: Alignment.topRight,
),
),
child: Padding(
padding: const EdgeInsets.only(
left: 24, top: 10, bottom: 10, right: 24),
child: Text(
'Set Color',
style: Theme.of(context).textTheme.bodyMedium,
),
),
),
),
],
),
),
const SizedBox(
height: 40,
height: 30,
),
Row(
children: [
Expand Down Expand Up @@ -200,6 +241,7 @@ class _CompleteFormState extends ConsumerState<CompleteForm> {
bikeNotifier.writeStateData(
widget.bike.copyWith(
name: _formKey.currentState?.value['name'],
color: _selectedColorIndex,
region: _formKey.currentState?.value['region']),
saveToBike: false);
Navigator.pop(context);
Expand All @@ -216,3 +258,42 @@ class _CompleteFormState extends ConsumerState<CompleteForm> {
);
}
}

Future<int> _showColorPicker(BuildContext context, int currentIndex) async {
final colors = getColorList();
final answer = await showModalBottomSheet<int>(
context: context,
builder: (BuildContext context) {
return Container(
child: ListView.builder(
itemCount: colors.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(26),
gradient: LinearGradient(
colors: [
Color(colors[index].start),
Color(colors[index].end),
],
begin: Alignment.bottomLeft,
end: Alignment.topRight,
),
),
),
onTap: () {
Navigator.pop(context, index); // return the color index
},
);
},
),
);
},
);
if (answer != null) {
return answer;
}
return currentIndex;
}
27 changes: 14 additions & 13 deletions lib/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ class BikeState with _$BikeState {
@Assert('mode >= 0')
@Assert('assist >= 0')
@Assert('assist <= 4')
const factory BikeState({
required String id,
required int mode,
@Default(false) bool modeLocked,
required bool light,
@Default(false) bool lightLocked,
required int assist,
@Default(false) bool assistLocked,
required String name,
BikeRegion? region,
@Default(false) bool modeLock,
@Default(false) bool selected,
}) = _BikeState;
@Assert('color >= 0')
const factory BikeState(
{required String id,
required int mode,
@Default(false) bool modeLocked,
required bool light,
@Default(false) bool lightLocked,
required int assist,
@Default(false) bool assistLocked,
required String name,
BikeRegion? region,
@Default(false) bool modeLock,
@Default(false) bool selected,
@Default(0) int color}) = _BikeState;

factory BikeState.fromJson(Map<String, Object?> json) =>
_$BikeStateFromJson(json);
Expand Down
Loading

0 comments on commit 612e94c

Please sign in to comment.