Skip to content

fix: Refactor HomeScreen to use proper Provider pattern #1356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:badgemagic/providers/getitlocator.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nope3472 having all the providers global can make the app slower, and logically the providers should be setup and should be concentarted to files where they are being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using global providers in our app ensures that shared state (like badge data and animations) stays consistent across all screens. It simplifies data flow, avoids duplication, and makes it easier to manage features that interact with each other. Plus, with Provider’s efficient rebuild mechanism, performance isn’t negatively impacted.

import 'package:badgemagic/providers/imageprovider.dart';
import 'package:badgemagic/providers/animation_badge_provider.dart';
import 'package:badgemagic/providers/speed_dial_provider.dart';
import 'package:badgemagic/providers/badge_message_provider.dart';
import 'package:badgemagic/view/about_us_screen.dart';
import 'package:badgemagic/view/draw_badge_screen.dart';
import 'package:badgemagic/view/homescreen.dart';
Expand All @@ -22,7 +25,20 @@ void main() {
runApp(MultiProvider(
providers: [
ChangeNotifierProvider<InlineImageProvider>(
create: (context) => getIt<InlineImageProvider>()),
create: (context) => getIt<InlineImageProvider>(),
),
ChangeNotifierProvider<AnimationBadgeProvider>(
create: (context) => AnimationBadgeProvider(),
),
ChangeNotifierProxyProvider<AnimationBadgeProvider, SpeedDialProvider>(
create: (context) =>
SpeedDialProvider(context.read<AnimationBadgeProvider>()),
update: (context, animationProvider, speedDialProvider) =>
speedDialProvider!..badgeProvider = animationProvider,
),
ChangeNotifierProvider<BadgeMessageProvider>(
create: (context) => BadgeMessageProvider(),
),
],
child: const MyApp(),
));
Expand Down
3 changes: 2 additions & 1 deletion lib/providers/badge_message_provider.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'dart:io';
import 'package:badgemagic/bademagic_module/bluetooth/base_ble_state.dart';
import 'package:badgemagic/bademagic_module/bluetooth/datagenerator.dart';
Expand Down Expand Up @@ -37,7 +38,7 @@ Map<int, Speed> speedMap = {
8: Speed.eight,
};

class BadgeMessageProvider {
class BadgeMessageProvider extends ChangeNotifier {
static final Logger logger = Logger();
InlineImageProvider controllerData =
GetIt.instance.get<InlineImageProvider>();
Expand Down
Loading
Loading