Skip to content
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

[Suggestion]: Home Widget Mixin on Widget #187

Open
burhanrashid52 opened this issue Dec 5, 2023 · 0 comments
Open

[Suggestion]: Home Widget Mixin on Widget #187

burhanrashid52 opened this issue Dec 5, 2023 · 0 comments

Comments

@burhanrashid52
Copy link

burhanrashid52 commented Dec 5, 2023

To update a Widget screenshot on the native side the home widget works well.

However, when we have multiple widgets to be displayed on the native widget, we need configuration for each widget separately.

This is where the updating Widget screenshot becomes repetitive. Since it was related to rendering a widget. I thought of extracting the same logic in mixin and applying it to the Flutter widget.

typedef WidgetConfig = ({
  String widgetName,
  String imageKey,
  String clickUrlKey,
  String clickUrlValue,
});

mixin HomeWidgetMixin on Widget {
  
  WidgetConfig get widgetConfig;

  void update() {
    final config = widgetConfig;
    HomeWidget.setAppGroupId(_iOSAppGroupId);
    final screenSize = MediaQuery.sizeOf(context);
 
    HomeWidget.saveWidgetData(
      config.clickUrlKey,
      config.clickUrlValue,
    );
    final saveWidget = HomeWidget.renderFlutterWidget(
      SizedBox.square(
        dimension: screenSize.width / 2,
        child: this,
      ),
      key: config.imageKey,
      logicalSize: Size.square(screenSize.width), // Can be configrable
    );
    saveWidget
        .then((_) => _updateNativeWidget());
  }

  Future<bool?> _updateNativeWidget() {
    return HomeWidget.updateWidget(
      iOSName: widgetConfig.widgetName,
      androidName: widgetConfig.widgetName,
    );
  }
}

On Widget to Dispalyed on Widget

class LoginButtonHomeWidget extends StatelessWidget with HomeWidgetMixin {
  const LoginButtonHomeWidget();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text( 'Please Login'),
      ),
    );
  }

  @override
  WidgetConfig get widgetConfig => (
        widgetName: 'LoginButtonHomeWidget',
        imageKey: 'login_image',
        clickUrlKey: 'click_login',
        clickUrlValue: 'myDeeplink://login',
      );
}

Update Widget

LoginButtonHomeWidget().update();

I am happy to create PR on this If this looks interesting and want to add support to the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant