Education
Software Development Executive - I
Software Development Executive - II
Last updated onSep 15, 2023
Last updated onJul 31, 2023
Flutter's theming capabilities offer a powerful way to create visually appealing and consistent user interfaces. In this blog post, we will take an in-depth look at advanced concepts around custom themes and how to extend them to create stunning, customizable, and maintainable UIs.
Before we jump into the advanced concepts, let's quickly recap the basics of theming in Flutter. Themes in Flutter are defined using the ThemeData class, which allows you to customize various aspects of your app's visual appearance. By default, Flutter provides a light and dark theme out of the box, which you can easily apply to your app.
To apply a theme, wrap your app's root widget with a MaterialApp and provide the desired theme using the theme parameter.
Creating a custom theme allows you to tailor the visual experience of your app to match your brand or specific design requirements. You can define custom colors, typography, shapes, and more. Let's explore some advanced techniques to customize your app's theme.
Custom colors in Flutter.
One of the first steps in creating a custom theme is defining your app's color palette. Flutter allows you to create custom color schemes by extending the ColorScheme class. This enables you to have a consistent set of colors throughout your app.
1 class MyAppColorScheme extends ColorScheme { 2 // Define your custom colors here 3 static const Color myPrimaryColor = Color(0xFF1E88E5); 4 static const Color mySecondaryColor = Color(0xFFFFA726); 5 6 // Override the constructor 7 const MyAppColorScheme({ 8 // Set your custom colors as primary and secondary 9 Color primary = myPrimaryColor, 10 Color secondary = mySecondaryColor, 11 12 // Include other color properties from the super class 13 // such as background, surface, onBackground, etc. 14 15 }) : super(primary: primary, secondary: secondary, ...); 16 } 17
This allows you to create a cohesive color scheme that reflects your app's branding, making it easily maintainable and consistent across various UI elements.
Typography in Flutter.
Typography plays a significant role in user interface design. To create a consistent and appealing text style throughout your app, you can define a custom Typography class. This class allows you to specify the font family, size, weight, and other text properties.
1 class MyAppTypography { 2 static const TextStyle myHeadingStyle = TextStyle( 3 fontSize: 24, 4 fontWeight: FontWeight.bold, 5 // other text properties 6 ); 7 static const TextStyle myBodyStyle = TextStyle( 8 fontSize: 16, 9 // other text properties 10 ); 11 } 12
By defining a custom Typography, you ensure that all text elements in your app follow a unified style, creating a more polished and professional look.
Custom shapes and shadows can give your app a unique look. Flutter's ShapeBorder and BoxShadow classes allow you to create custom shapes and shadows to apply throughout your app.
1 class MyAppShape { 2 static const RoundedRectangleBorder myRoundedRectangleBorder = 3 RoundedRectangleBorder( 4 borderRadius: BorderRadius.all(Radius.circular(16)), 5 // other border properties 6 ); 7 8 } 9 10 class MyAppBoxShadow { 11 static const BoxShadow myCustomBoxShadow = BoxShadow( 12 color: Colors.black12, 13 offset: Offset(0, 2), 14 blurRadius: 4, 15 // other shadow properties 16 ); 17 } 18
Using custom shapes and shadows, you can add visual interest and depth to various UI elements, making your app stand out from the crowd.
Now that we have our custom theme defined, let's take it a step further and create theme extensions. Theme extensions enable us to build customizable UI components that adapt to the app's main theme. This way, you can reuse components across your app and easily switch themes.
Let's create a customizable button that adapts to the app's theme. We'll create a CustomButton widget that takes a ButtonStyle parameter, allowing developers to customize the button's appearance easily.
1 class CustomButton extends StatelessWidget { 2 final String text; 3 final ButtonStyle style; 4 const CustomButton({required this.text, required this.style}); 5 @override 6 Widget build(BuildContext context) { 7 return ElevatedButton( 8 onPressed: () {}, 9 child: Text(text), 10 style: ElevatedButton.styleFrom( 11 12 // Apply the custom style, and fallback to the default theme 13 // if a particular property is not provided 14 15 padding: style.padding ?? const EdgeInsets.all(16), 16 backgroundColor: 17 style.backgroundColor ?? Theme.of(context).primaryColor, 18 // other button properties 19 ).merge(style), 20 ); 21 } 22 } 23
Now, developers can use the CustomButton widget and easily customize its appearance:
1 CustomButton( 2 text: 'Click Me', 3 style: ButtonStyle( 4 padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12), 5 backgroundColor: Colors.green, 6 textStyle: TextStyle(color: Colors.white), 7 // other button style properties 8 ), 9 ) 10
With this approach, you can create a wide variety of buttons that adapt to the app's theme, making your UI more cohesive and versatile.
Similarly, we can create a CustomCard widget that adapts to the app's theme. We'll use CardTheme to define the card's appearance and allow customizations.
1 class CustomCard extends StatelessWidget { 2 final Widget child; 3 final CardTheme theme; 4 const CustomCard({required this.child, required this.theme}); 5 @override 6 Widget build(BuildContext context) { 7 return Card( 8 child: child, 9 shape: theme.shape ?? Theme.of(context).cardTheme.shape, 10 color: theme.color ?? Theme.of(context).cardTheme.color, 11 // other card properties 12 ); 13 } 14 } 15
With the CustomCard widget, developers can easily create adaptable cards:
1 CustomCard( 2 child: ListTile( 3 leading: Icon(Icons.person), 4 title: Text('John Doe'), 5 subtitle: Text('Software Engineer'), 6 ), 7 theme: CardTheme( 8 color: Colors.blueAccent, 9 shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)), 10 // other card theme properties 11 ), 12 ) 13
By creating adaptable UI components, you make your app more flexible and user-friendly, empowering developers to build engaging interfaces with ease.
We've covered custom themes, theme extensions, and dynamic theming in great detail. By understanding and utilizing these advanced theming techniques in Flutter, you can create beautiful and customizable user interfaces that align with your app's branding and provide a delightful user experience.
Before you start implementing custom themes and extensions in your Flutter projects, I have an exciting recommendation for you: WiseGPT. WiseGPT is a powerful plugin that will revolutionize how you handle code generation in your Flutter apps.
WiseGPT
With WiseGPT, you can effortlessly generate code for APIs directly into your Flutter project, without worrying about output size limitations. Whether you're dealing with simple or complex API endpoints, WiseGPT has got you covered. It will automatically mirror your coding style, ensuring a seamless integration of the generated code into your existing project.
One of the most remarkable features of WiseGPT is its promptless nature. You no longer need to provide detailed prompts for the plugin to understand your requirements. WiseGPT leverages advanced language models to comprehend your API needs and generates corresponding code.
By utilizing WiseGPT during the Flutter development process, you can save a significant amount of time and effort and concentrate on other important features of your project. It simplifies the entire API integration process, making it more efficient and enjoyable.
So, what are you waiting for? Give WiseGPT a try and experience the true power of automated code generation for your APIs in Flutter. With WiseGPT by your side, you can build sophisticated and feature-rich apps with ease.
Thank you for joining me on this journey of exploring custom themes and theme extensions in Flutter. I hope you found this blog insightful and that it empowers you to create stunning, themeable apps that leave a lasting impression on your users!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.