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

How to Create a Flexible Custom Wrapper for Widgets to overriding some default properties? #147718

Closed
talpx0 opened this issue May 2, 2024 · 4 comments
Labels
in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds

Comments

@talpx0
Copy link

talpx0 commented May 2, 2024

I'm currently developing a custom wrapper for widgets in Flutter that enables extensive customization, similar to how {...rest} works in TypeScript with React components. However, Dart doesn't support the spread operator for widget properties, so I'm struggling to find an effective way to forward a wide range of properties to a base widget like IconButton.

Here's an example of what I might do in TypeScript:

const CustomIconButton: React.FC = ({
  accessibilityLabel,
  ...rest
}) => {
  return (
    <IconButton {...rest} aria-label={accessibilityLabel ?? ""} color={"red"}/>
  );
};

In Flutter, I'm looking for a way to achieve something similar. How can I create a wrapper that allows users to customize any widget with multiple properties without manually specifying each one? Here's a basic approach in Dart, but it requires explicitly forwarding each property, which isn't scalable. For example, an IconButton can have additional properties like activecolor and border. Manually copying all of them can make the code redundant. Here's my current approach:

import 'package:flutter/material.dart';

class CustomIconButton extends StatelessWidget {
  final VoidCallback onPressed;
  final IconData icon;
  final IconButtonConfig config;

  const CustomIconButton({
    Key? key,
    required this.onPressed,
    required this.icon,
    this.config = const IconButtonConfig(),
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: Icon(icon),
      onPressed: onPressed,
      tooltip: config.tooltip,
      color: config.color,
      // add other IconButton properties here using the config object
    );
  }
}

class IconButtonConfig {
  final String tooltip;
  final Color color;
  final double iconSize;

  const IconButtonConfig({
    this.tooltip = '',
    this.color = Colors.black,
    this.iconSize = 24.0,
  });
}

How can I achieve similar behavior to React Native in Flutter? Any insights would be greatly appreciated!

I try to use extend and extension , but they all looks like required you copy all the properties.

@XuanTung95
Copy link

How about extends IconButton?

@talpx0
Copy link
Author

talpx0 commented May 3, 2024

How about extends IconButton?

after I research , I found that they prefer use callback to create an IconButtonBuilder or export the whole IconButton as an individual child , I can't find out more than this . extension is good , but you also need to forward all the key , and I saw they do forward all the key in their source code . I guess they don't plan to support this because null safety , but it makes code decoupling become much redundant.

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label May 3, 2024
@darshankawar
Copy link
Member

@talpx0
Can you check this and see if it helps in your case or not ?

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 3, 2024
Copy link

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now.
If you find this problem please file a new issue with the same description, what happens, logs and the output of 'flutter doctor -v'. All system setups can be slightly different so it's always better to open new issues and reference the related ones.
Thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds
Projects
None yet
Development

No branches or pull requests

3 participants