Flutter custom widget styling

419

Basically you need to understand the concept of custom widgets and know the different types of chips that exist.

  1. You can have several types of bullets - choice, filter, action, input - Chips. Chips - Material Design

  2. From the different types of styles that material design offers, you adapt this to the widgets you receive from your design team. For example wrap your bullets in a Column to have an extra Label.

class MyActionChip extends StatelessWidget {
  /// Default chip have a string label
  const MyActionChip({
    required this.label,
    required this.onPressed,
    Key? key,
    this.onTapAvatar,
  })  : child = null, super(key: key);
  
  /// Default chip have a widget label
  const MyActionChip.child({
    required this.child,
    required this.onPressed,
    Key? key,
    this.onTapAvatar,
  })  : label = null, super(key: key);
  
  final String? label;
  final Widget? child;
  final VoidCallback? onTapAvatar;
  final Function()? onPressed;
  
  @override
  Widget build(BuildContext context) {
    return InputChip(
      backgroundColor: MyColors.milk,
      shape: const RoundedRectangleBorder(
          borderRadius:
          BorderRadius.all(Radius.circular(MyConstants.borderRadiusMedium)),
          side: BorderSide(color: MyColors.anthracite20),
      ),
      avatar: const Icon(
          MyIcons.plus,
          size: 18,
          color: MyColors.anthracite,
      ),
      labelPadding:
          const EdgeInsets.only(right: SdConstants.contentPaddingMedium),
          label: child ??
              Text(
                  label!,
                  style: const TextStyle(
                    color: MyColors.anthracite,
            fontSize: 14,
            fontWeight: FontWeight.bold,
          ),
        ),
      onPressed: onPressed,
    );
  }
}
  1. In complex cases, you can build a custom chip according to the complexity.

Note: the parameters that vary according to the screens and callbacks are simply exposed outside the constructor. You need several constructors/classes to customize all your chips.

Share:
419
Br4infreze
Author by

Br4infreze

Updated on January 04, 2023

Comments

  • Br4infreze
    Br4infreze over 1 year

    I'm working on a big app tightly with the designer.
    I was a part of the style guide and the component design process,
    the designer handed off his work and now its my turn to translate some prototypes and styles to real flutter app.

    I'm having a really hard time translating the style guide to flutter widgets.
    For example, I have a custom chip widget created from scratch, with many style properties.
    There are two main different styles for this chip (normal and highlighted),
    and the chip is used widely (I believe more than 50 references), excluding other more complex widgets using it.

    I tried :

    1. Creating a style class for each big widget, and created extension on ThemeData.
    class MyCustomChip extends StatelessWidget {
        Widget build(BuildContext context) {
           final style = Theme.of(context).myCustomChip;
           /** return the custom chip **/
        }
    }
    
    class MyCustomChipStyle {
    /** many style properties **/
    }
    
    extension MyCustomChipThemeData on ThemeData {
        MyCustomChipStyle get myCustomChip => ...
    }
    
    
    1. Created a static style class, and passed relevant style properties every time the widget used.
    class MyCustomChip extends StatelessWidget {
        final Color color;
        final BorderRadius borderRadius;
        /** many more styling properties **/
    }
    
    abstract class MyCustomChipValues {
        static const BorderRadius kBorder = /** Getting value from a predefined radiuses **/
        static const Color kColor = /** Getting value from a predefined palette **/
        /** Many more **/
    }
    
    class WidgetUsingTheChip extends StatelessWidget {
        Widget build(BuildContext context) {
          /** those lines will be duplicated alot **/
          return MyCustomChip(
            color: MyCustomChipValues.kColor,
            borderRadius: MyCustomChipValues.kBorder,
            /** Many more **/
          );
        }
    }
    
    1. Used all the relevant style properties inside the custom widget, and didn't expose them.
    abstract class MyCustomChipValues {
        static const BorderRadius kBorder = /** Getting value from a predefined radiuses **/
        static const Color kColor = /** Getting value from a predefined palette **/
        /** Many more **/
    }
    
    class MyCustomChip extends StatelessWidget {
        Widget build(BuildContext context) {
           /** making it virtually impossible to change style depending on state **/
           return Container(
              color: MyCustomChipValues.kColor,
              /** Many more **/
           );
        }
    }
    
    class WidgetUsingTheChip extends StatelessWidget {
        Widget build(BuildContext context) {
          return MyCustomChip();
        }
    }
    

    I have defined other style classes :

    • Insets (various paddings and margins)
    • Colors (our palette, with buttons, chips, input, scaffold, etc..)
    • BorderRadiuses
    • Shadows
    • Animations (duration and curve)
    • Guidelines (based on the gutter and the design grid we prototyped with)
    • Other classes...

    I cant wrap my head on which of the following is more easy to maintain and refactor in the time of need..

    Any suggestion on how to manage this whole Style guide -> code will be helpful,
    especially on how to manage custom widget styles.

    Thanks !

    • PRATHIV
      PRATHIV about 2 years
      kindly add related images what you have been trying to design