storing appearance and metrics constants in Flutter app
While developing flutter app with many widgets involved on the same screen I realised that it can be useful to store some structs with appearance and metrics constants that are used on screen somewhere outside widgets but I am not sure where.
For example - I have blog post widget that consists of 8-10 Text widgets. And I want to store their TextStyles properties somewhere together so it would be easy to modify them and this widgets code will be shorter.
Same thing can be applied to metrics: width, padding, corner radius etc.
dart flutter
add a comment |
While developing flutter app with many widgets involved on the same screen I realised that it can be useful to store some structs with appearance and metrics constants that are used on screen somewhere outside widgets but I am not sure where.
For example - I have blog post widget that consists of 8-10 Text widgets. And I want to store their TextStyles properties somewhere together so it would be easy to modify them and this widgets code will be shorter.
Same thing can be applied to metrics: width, padding, corner radius etc.
dart flutter
add a comment |
While developing flutter app with many widgets involved on the same screen I realised that it can be useful to store some structs with appearance and metrics constants that are used on screen somewhere outside widgets but I am not sure where.
For example - I have blog post widget that consists of 8-10 Text widgets. And I want to store their TextStyles properties somewhere together so it would be easy to modify them and this widgets code will be shorter.
Same thing can be applied to metrics: width, padding, corner radius etc.
dart flutter
While developing flutter app with many widgets involved on the same screen I realised that it can be useful to store some structs with appearance and metrics constants that are used on screen somewhere outside widgets but I am not sure where.
For example - I have blog post widget that consists of 8-10 Text widgets. And I want to store their TextStyles properties somewhere together so it would be easy to modify them and this widgets code will be shorter.
Same thing can be applied to metrics: width, padding, corner radius etc.
dart flutter
dart flutter
asked Jan 20 at 9:22
moonvadermoonvader
3,51772451
3,51772451
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your question context in reducing boilerplate code by creating custom
component with high reuse-ability and maintenance.
So you can achieve this with 4 simple steps:
1. Create application directory as :
-resources (parent resource directory)
-menus (store all menus list constants)
-values
-app_strings.dart (store all strings constants)
-app_colors.dart (store all colors constants)
-app_styles.dart (store all styles i.e. material dark, light, cupertino etc.)
-app_dimens.dart (store all dimension constants)
-components (parent component directory)
-your_custom_widget.dart(create custom component here)
-.....
-views
-your_view.dart(your view where you import custom component)
2. Creating resource constants:
Its very easy steps as you have to add only constants in respective resource files.
Example - creating color constants in app_colors.dart
import 'package:flutter/material.dart';
/// App Colors Class - Resource class for storing app level color constants
class AppColors {
static const Color PRIMARY_COLOR = Color(0xFF35B4C5);
static const Color PRIMARY_COLOR_LIGHT = Color(0xFFA5CFF1);
static const Color PRIMARY_COLOR_DARK = Color(0xFF0D3656);
static const Color ACCENT_COLOR = Color(0xFFF2DA04);
}
3. Create custom components:
Now in components
directory create a custom widget as :
class CustomWidget extends StatefulWidget{
// Declare your widget parameters
final data-type your_parameter;
.....
.....
.....
// Create constant constructor
const CustomWidget(
// Initialize all your widget parameters
this.your_parameter
.....
.....
.....)
@override
_CustomWidgetState createState() => _CustomWidgetState();
}
/// CustomWidget State class
class _CustomWidgetState extends State<CustomWidget>{
// Here you should use existing widget from either material library or cupertino etc
@override
Widget build(BuildContext context) {
return ExistingBaseWidget(
// Set here all necessary parameters for customization
// For setting constansts from resources you do it like this
color : AppColors.COLOR_NAME,
radius : AppDimens.BORDER_RADIUS,
.......
);
}
}
4. Import custom widget to any views:
In your any views you can import custom widgets as use like this
child: CustomWidget(
// Initialize all required parameters
)
Advantages
In future whenever you want to change your custom widgets or resources constants, you have to change it in only one place and it will reflect in all places.
Also you can use this custom widgets and resource constants in all your projects with minor modification.
Views code looks more clean and readable.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275065%2fstoring-appearance-and-metrics-constants-in-flutter-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your question context in reducing boilerplate code by creating custom
component with high reuse-ability and maintenance.
So you can achieve this with 4 simple steps:
1. Create application directory as :
-resources (parent resource directory)
-menus (store all menus list constants)
-values
-app_strings.dart (store all strings constants)
-app_colors.dart (store all colors constants)
-app_styles.dart (store all styles i.e. material dark, light, cupertino etc.)
-app_dimens.dart (store all dimension constants)
-components (parent component directory)
-your_custom_widget.dart(create custom component here)
-.....
-views
-your_view.dart(your view where you import custom component)
2. Creating resource constants:
Its very easy steps as you have to add only constants in respective resource files.
Example - creating color constants in app_colors.dart
import 'package:flutter/material.dart';
/// App Colors Class - Resource class for storing app level color constants
class AppColors {
static const Color PRIMARY_COLOR = Color(0xFF35B4C5);
static const Color PRIMARY_COLOR_LIGHT = Color(0xFFA5CFF1);
static const Color PRIMARY_COLOR_DARK = Color(0xFF0D3656);
static const Color ACCENT_COLOR = Color(0xFFF2DA04);
}
3. Create custom components:
Now in components
directory create a custom widget as :
class CustomWidget extends StatefulWidget{
// Declare your widget parameters
final data-type your_parameter;
.....
.....
.....
// Create constant constructor
const CustomWidget(
// Initialize all your widget parameters
this.your_parameter
.....
.....
.....)
@override
_CustomWidgetState createState() => _CustomWidgetState();
}
/// CustomWidget State class
class _CustomWidgetState extends State<CustomWidget>{
// Here you should use existing widget from either material library or cupertino etc
@override
Widget build(BuildContext context) {
return ExistingBaseWidget(
// Set here all necessary parameters for customization
// For setting constansts from resources you do it like this
color : AppColors.COLOR_NAME,
radius : AppDimens.BORDER_RADIUS,
.......
);
}
}
4. Import custom widget to any views:
In your any views you can import custom widgets as use like this
child: CustomWidget(
// Initialize all required parameters
)
Advantages
In future whenever you want to change your custom widgets or resources constants, you have to change it in only one place and it will reflect in all places.
Also you can use this custom widgets and resource constants in all your projects with minor modification.
Views code looks more clean and readable.
add a comment |
Your question context in reducing boilerplate code by creating custom
component with high reuse-ability and maintenance.
So you can achieve this with 4 simple steps:
1. Create application directory as :
-resources (parent resource directory)
-menus (store all menus list constants)
-values
-app_strings.dart (store all strings constants)
-app_colors.dart (store all colors constants)
-app_styles.dart (store all styles i.e. material dark, light, cupertino etc.)
-app_dimens.dart (store all dimension constants)
-components (parent component directory)
-your_custom_widget.dart(create custom component here)
-.....
-views
-your_view.dart(your view where you import custom component)
2. Creating resource constants:
Its very easy steps as you have to add only constants in respective resource files.
Example - creating color constants in app_colors.dart
import 'package:flutter/material.dart';
/// App Colors Class - Resource class for storing app level color constants
class AppColors {
static const Color PRIMARY_COLOR = Color(0xFF35B4C5);
static const Color PRIMARY_COLOR_LIGHT = Color(0xFFA5CFF1);
static const Color PRIMARY_COLOR_DARK = Color(0xFF0D3656);
static const Color ACCENT_COLOR = Color(0xFFF2DA04);
}
3. Create custom components:
Now in components
directory create a custom widget as :
class CustomWidget extends StatefulWidget{
// Declare your widget parameters
final data-type your_parameter;
.....
.....
.....
// Create constant constructor
const CustomWidget(
// Initialize all your widget parameters
this.your_parameter
.....
.....
.....)
@override
_CustomWidgetState createState() => _CustomWidgetState();
}
/// CustomWidget State class
class _CustomWidgetState extends State<CustomWidget>{
// Here you should use existing widget from either material library or cupertino etc
@override
Widget build(BuildContext context) {
return ExistingBaseWidget(
// Set here all necessary parameters for customization
// For setting constansts from resources you do it like this
color : AppColors.COLOR_NAME,
radius : AppDimens.BORDER_RADIUS,
.......
);
}
}
4. Import custom widget to any views:
In your any views you can import custom widgets as use like this
child: CustomWidget(
// Initialize all required parameters
)
Advantages
In future whenever you want to change your custom widgets or resources constants, you have to change it in only one place and it will reflect in all places.
Also you can use this custom widgets and resource constants in all your projects with minor modification.
Views code looks more clean and readable.
add a comment |
Your question context in reducing boilerplate code by creating custom
component with high reuse-ability and maintenance.
So you can achieve this with 4 simple steps:
1. Create application directory as :
-resources (parent resource directory)
-menus (store all menus list constants)
-values
-app_strings.dart (store all strings constants)
-app_colors.dart (store all colors constants)
-app_styles.dart (store all styles i.e. material dark, light, cupertino etc.)
-app_dimens.dart (store all dimension constants)
-components (parent component directory)
-your_custom_widget.dart(create custom component here)
-.....
-views
-your_view.dart(your view where you import custom component)
2. Creating resource constants:
Its very easy steps as you have to add only constants in respective resource files.
Example - creating color constants in app_colors.dart
import 'package:flutter/material.dart';
/// App Colors Class - Resource class for storing app level color constants
class AppColors {
static const Color PRIMARY_COLOR = Color(0xFF35B4C5);
static const Color PRIMARY_COLOR_LIGHT = Color(0xFFA5CFF1);
static const Color PRIMARY_COLOR_DARK = Color(0xFF0D3656);
static const Color ACCENT_COLOR = Color(0xFFF2DA04);
}
3. Create custom components:
Now in components
directory create a custom widget as :
class CustomWidget extends StatefulWidget{
// Declare your widget parameters
final data-type your_parameter;
.....
.....
.....
// Create constant constructor
const CustomWidget(
// Initialize all your widget parameters
this.your_parameter
.....
.....
.....)
@override
_CustomWidgetState createState() => _CustomWidgetState();
}
/// CustomWidget State class
class _CustomWidgetState extends State<CustomWidget>{
// Here you should use existing widget from either material library or cupertino etc
@override
Widget build(BuildContext context) {
return ExistingBaseWidget(
// Set here all necessary parameters for customization
// For setting constansts from resources you do it like this
color : AppColors.COLOR_NAME,
radius : AppDimens.BORDER_RADIUS,
.......
);
}
}
4. Import custom widget to any views:
In your any views you can import custom widgets as use like this
child: CustomWidget(
// Initialize all required parameters
)
Advantages
In future whenever you want to change your custom widgets or resources constants, you have to change it in only one place and it will reflect in all places.
Also you can use this custom widgets and resource constants in all your projects with minor modification.
Views code looks more clean and readable.
Your question context in reducing boilerplate code by creating custom
component with high reuse-ability and maintenance.
So you can achieve this with 4 simple steps:
1. Create application directory as :
-resources (parent resource directory)
-menus (store all menus list constants)
-values
-app_strings.dart (store all strings constants)
-app_colors.dart (store all colors constants)
-app_styles.dart (store all styles i.e. material dark, light, cupertino etc.)
-app_dimens.dart (store all dimension constants)
-components (parent component directory)
-your_custom_widget.dart(create custom component here)
-.....
-views
-your_view.dart(your view where you import custom component)
2. Creating resource constants:
Its very easy steps as you have to add only constants in respective resource files.
Example - creating color constants in app_colors.dart
import 'package:flutter/material.dart';
/// App Colors Class - Resource class for storing app level color constants
class AppColors {
static const Color PRIMARY_COLOR = Color(0xFF35B4C5);
static const Color PRIMARY_COLOR_LIGHT = Color(0xFFA5CFF1);
static const Color PRIMARY_COLOR_DARK = Color(0xFF0D3656);
static const Color ACCENT_COLOR = Color(0xFFF2DA04);
}
3. Create custom components:
Now in components
directory create a custom widget as :
class CustomWidget extends StatefulWidget{
// Declare your widget parameters
final data-type your_parameter;
.....
.....
.....
// Create constant constructor
const CustomWidget(
// Initialize all your widget parameters
this.your_parameter
.....
.....
.....)
@override
_CustomWidgetState createState() => _CustomWidgetState();
}
/// CustomWidget State class
class _CustomWidgetState extends State<CustomWidget>{
// Here you should use existing widget from either material library or cupertino etc
@override
Widget build(BuildContext context) {
return ExistingBaseWidget(
// Set here all necessary parameters for customization
// For setting constansts from resources you do it like this
color : AppColors.COLOR_NAME,
radius : AppDimens.BORDER_RADIUS,
.......
);
}
}
4. Import custom widget to any views:
In your any views you can import custom widgets as use like this
child: CustomWidget(
// Initialize all required parameters
)
Advantages
In future whenever you want to change your custom widgets or resources constants, you have to change it in only one place and it will reflect in all places.
Also you can use this custom widgets and resource constants in all your projects with minor modification.
Views code looks more clean and readable.
answered Jan 20 at 10:52
Parikshit ChalkeParikshit Chalke
13310
13310
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275065%2fstoring-appearance-and-metrics-constants-in-flutter-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown