storing appearance and metrics constants in Flutter app












1















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.










share|improve this question



























    1















    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.










    share|improve this question

























      1












      1








      1


      1






      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 20 at 9:22









      moonvadermoonvader

      3,51772451




      3,51772451
























          1 Answer
          1






          active

          oldest

          votes


















          1















          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




          1. 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.


          2. Also you can use this custom widgets and resource constants in all your projects with minor modification.


          3. Views code looks more clean and readable.







          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            1















            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




            1. 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.


            2. Also you can use this custom widgets and resource constants in all your projects with minor modification.


            3. Views code looks more clean and readable.







            share|improve this answer




























              1















              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




              1. 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.


              2. Also you can use this custom widgets and resource constants in all your projects with minor modification.


              3. Views code looks more clean and readable.







              share|improve this answer


























                1












                1








                1








                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




                1. 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.


                2. Also you can use this custom widgets and resource constants in all your projects with minor modification.


                3. Views code looks more clean and readable.







                share|improve this answer














                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




                1. 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.


                2. Also you can use this custom widgets and resource constants in all your projects with minor modification.


                3. Views code looks more clean and readable.








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 20 at 10:52









                Parikshit ChalkeParikshit Chalke

                13310




                13310
































                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Homophylophilia

                    Updating UILabel text programmatically using a function

                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage