How to add a constructor to an existing class in Dart?












0















I'd like to make some convenience/factory constructors for a few common UI classes. For example, TextStyle:



enum Font {
AvenirNext,
AvenirNextCondensed
}

enum Weight {
Regular,
Medium,
DemiBold,
Bold
}

// Use:
// TextStyle(Fonts.AvenirNext, Weight.Medium, 20, Colors.white)

// Maybe it can be even shorter?
// TextStyle(AvenirNext, Medium, 20, Colors.black)


How to go about this in Dart?










share|improve this question



























    0















    I'd like to make some convenience/factory constructors for a few common UI classes. For example, TextStyle:



    enum Font {
    AvenirNext,
    AvenirNextCondensed
    }

    enum Weight {
    Regular,
    Medium,
    DemiBold,
    Bold
    }

    // Use:
    // TextStyle(Fonts.AvenirNext, Weight.Medium, 20, Colors.white)

    // Maybe it can be even shorter?
    // TextStyle(AvenirNext, Medium, 20, Colors.black)


    How to go about this in Dart?










    share|improve this question

























      0












      0








      0








      I'd like to make some convenience/factory constructors for a few common UI classes. For example, TextStyle:



      enum Font {
      AvenirNext,
      AvenirNextCondensed
      }

      enum Weight {
      Regular,
      Medium,
      DemiBold,
      Bold
      }

      // Use:
      // TextStyle(Fonts.AvenirNext, Weight.Medium, 20, Colors.white)

      // Maybe it can be even shorter?
      // TextStyle(AvenirNext, Medium, 20, Colors.black)


      How to go about this in Dart?










      share|improve this question














      I'd like to make some convenience/factory constructors for a few common UI classes. For example, TextStyle:



      enum Font {
      AvenirNext,
      AvenirNextCondensed
      }

      enum Weight {
      Regular,
      Medium,
      DemiBold,
      Bold
      }

      // Use:
      // TextStyle(Fonts.AvenirNext, Weight.Medium, 20, Colors.white)

      // Maybe it can be even shorter?
      // TextStyle(AvenirNext, Medium, 20, Colors.black)


      How to go about this in Dart?







      class constructor dart






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 19:36









      GoldenJoeGoldenJoe

      3,78023058




      3,78023058
























          1 Answer
          1






          active

          oldest

          votes


















          1














          How about extending TextStyle like below



          class Font {
          static const AvenirNext = 'AvenirNext';
          static const AvenirNextCondensed = 'AvenirNextCondensed';
          }

          class Weight{
          static const Regular = FontWeight.w400;
          static const Medium = FontWeight.w500;
          static const DemiBold = FontWeight.w700;
          static const Bold = FontWeight.w900;
          }

          class CTextStyle extends TextStyle {
          CTextStyle(String f, FontWeight w, num s, Color c)
          : super(
          fontFamily: f,
          fontWeight: w,
          fontSize: s.toDouble(),
          color: c);
          }
          }


          usage:



          TextStyle myCustom = CTextStyle(Font.AvenirNext, Weight.Medium, 20, Colors.black);





          share|improve this answer





















          • 2





            Alternatively, don't even make a class, and just define top-level factory functions that create a TextStyle object.

            – lrn
            Nov 26 '18 at 6:32











          • Yes , great. I was trying to explain that we can not modify the class .we can only extend it.

            – Saed Nabil
            Nov 26 '18 at 7:57











          • This is great. For sake of academics, what if I kept the enums and constructed an argument list for the super constructor: class TSExt extends TextStyle { //TSExt(Font font, Weight weight, double fontSize, Color color) : super(arguments); TSExt(Font font, Weight weight, double fontSize, Color color){ var args = { // ... }; super(args); } }

            – GoldenJoe
            Nov 26 '18 at 19:15













          • @GoldenJow It seems a good idea , if you tested this approch please, let us know

            – Saed Nabil
            Nov 27 '18 at 9:52











          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%2f53471137%2fhow-to-add-a-constructor-to-an-existing-class-in-dart%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














          How about extending TextStyle like below



          class Font {
          static const AvenirNext = 'AvenirNext';
          static const AvenirNextCondensed = 'AvenirNextCondensed';
          }

          class Weight{
          static const Regular = FontWeight.w400;
          static const Medium = FontWeight.w500;
          static const DemiBold = FontWeight.w700;
          static const Bold = FontWeight.w900;
          }

          class CTextStyle extends TextStyle {
          CTextStyle(String f, FontWeight w, num s, Color c)
          : super(
          fontFamily: f,
          fontWeight: w,
          fontSize: s.toDouble(),
          color: c);
          }
          }


          usage:



          TextStyle myCustom = CTextStyle(Font.AvenirNext, Weight.Medium, 20, Colors.black);





          share|improve this answer





















          • 2





            Alternatively, don't even make a class, and just define top-level factory functions that create a TextStyle object.

            – lrn
            Nov 26 '18 at 6:32











          • Yes , great. I was trying to explain that we can not modify the class .we can only extend it.

            – Saed Nabil
            Nov 26 '18 at 7:57











          • This is great. For sake of academics, what if I kept the enums and constructed an argument list for the super constructor: class TSExt extends TextStyle { //TSExt(Font font, Weight weight, double fontSize, Color color) : super(arguments); TSExt(Font font, Weight weight, double fontSize, Color color){ var args = { // ... }; super(args); } }

            – GoldenJoe
            Nov 26 '18 at 19:15













          • @GoldenJow It seems a good idea , if you tested this approch please, let us know

            – Saed Nabil
            Nov 27 '18 at 9:52
















          1














          How about extending TextStyle like below



          class Font {
          static const AvenirNext = 'AvenirNext';
          static const AvenirNextCondensed = 'AvenirNextCondensed';
          }

          class Weight{
          static const Regular = FontWeight.w400;
          static const Medium = FontWeight.w500;
          static const DemiBold = FontWeight.w700;
          static const Bold = FontWeight.w900;
          }

          class CTextStyle extends TextStyle {
          CTextStyle(String f, FontWeight w, num s, Color c)
          : super(
          fontFamily: f,
          fontWeight: w,
          fontSize: s.toDouble(),
          color: c);
          }
          }


          usage:



          TextStyle myCustom = CTextStyle(Font.AvenirNext, Weight.Medium, 20, Colors.black);





          share|improve this answer





















          • 2





            Alternatively, don't even make a class, and just define top-level factory functions that create a TextStyle object.

            – lrn
            Nov 26 '18 at 6:32











          • Yes , great. I was trying to explain that we can not modify the class .we can only extend it.

            – Saed Nabil
            Nov 26 '18 at 7:57











          • This is great. For sake of academics, what if I kept the enums and constructed an argument list for the super constructor: class TSExt extends TextStyle { //TSExt(Font font, Weight weight, double fontSize, Color color) : super(arguments); TSExt(Font font, Weight weight, double fontSize, Color color){ var args = { // ... }; super(args); } }

            – GoldenJoe
            Nov 26 '18 at 19:15













          • @GoldenJow It seems a good idea , if you tested this approch please, let us know

            – Saed Nabil
            Nov 27 '18 at 9:52














          1












          1








          1







          How about extending TextStyle like below



          class Font {
          static const AvenirNext = 'AvenirNext';
          static const AvenirNextCondensed = 'AvenirNextCondensed';
          }

          class Weight{
          static const Regular = FontWeight.w400;
          static const Medium = FontWeight.w500;
          static const DemiBold = FontWeight.w700;
          static const Bold = FontWeight.w900;
          }

          class CTextStyle extends TextStyle {
          CTextStyle(String f, FontWeight w, num s, Color c)
          : super(
          fontFamily: f,
          fontWeight: w,
          fontSize: s.toDouble(),
          color: c);
          }
          }


          usage:



          TextStyle myCustom = CTextStyle(Font.AvenirNext, Weight.Medium, 20, Colors.black);





          share|improve this answer















          How about extending TextStyle like below



          class Font {
          static const AvenirNext = 'AvenirNext';
          static const AvenirNextCondensed = 'AvenirNextCondensed';
          }

          class Weight{
          static const Regular = FontWeight.w400;
          static const Medium = FontWeight.w500;
          static const DemiBold = FontWeight.w700;
          static const Bold = FontWeight.w900;
          }

          class CTextStyle extends TextStyle {
          CTextStyle(String f, FontWeight w, num s, Color c)
          : super(
          fontFamily: f,
          fontWeight: w,
          fontSize: s.toDouble(),
          color: c);
          }
          }


          usage:



          TextStyle myCustom = CTextStyle(Font.AvenirNext, Weight.Medium, 20, Colors.black);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 6:31









          lrn

          10.6k1324




          10.6k1324










          answered Nov 26 '18 at 1:47









          Saed NabilSaed Nabil

          1,25329




          1,25329








          • 2





            Alternatively, don't even make a class, and just define top-level factory functions that create a TextStyle object.

            – lrn
            Nov 26 '18 at 6:32











          • Yes , great. I was trying to explain that we can not modify the class .we can only extend it.

            – Saed Nabil
            Nov 26 '18 at 7:57











          • This is great. For sake of academics, what if I kept the enums and constructed an argument list for the super constructor: class TSExt extends TextStyle { //TSExt(Font font, Weight weight, double fontSize, Color color) : super(arguments); TSExt(Font font, Weight weight, double fontSize, Color color){ var args = { // ... }; super(args); } }

            – GoldenJoe
            Nov 26 '18 at 19:15













          • @GoldenJow It seems a good idea , if you tested this approch please, let us know

            – Saed Nabil
            Nov 27 '18 at 9:52














          • 2





            Alternatively, don't even make a class, and just define top-level factory functions that create a TextStyle object.

            – lrn
            Nov 26 '18 at 6:32











          • Yes , great. I was trying to explain that we can not modify the class .we can only extend it.

            – Saed Nabil
            Nov 26 '18 at 7:57











          • This is great. For sake of academics, what if I kept the enums and constructed an argument list for the super constructor: class TSExt extends TextStyle { //TSExt(Font font, Weight weight, double fontSize, Color color) : super(arguments); TSExt(Font font, Weight weight, double fontSize, Color color){ var args = { // ... }; super(args); } }

            – GoldenJoe
            Nov 26 '18 at 19:15













          • @GoldenJow It seems a good idea , if you tested this approch please, let us know

            – Saed Nabil
            Nov 27 '18 at 9:52








          2




          2





          Alternatively, don't even make a class, and just define top-level factory functions that create a TextStyle object.

          – lrn
          Nov 26 '18 at 6:32





          Alternatively, don't even make a class, and just define top-level factory functions that create a TextStyle object.

          – lrn
          Nov 26 '18 at 6:32













          Yes , great. I was trying to explain that we can not modify the class .we can only extend it.

          – Saed Nabil
          Nov 26 '18 at 7:57





          Yes , great. I was trying to explain that we can not modify the class .we can only extend it.

          – Saed Nabil
          Nov 26 '18 at 7:57













          This is great. For sake of academics, what if I kept the enums and constructed an argument list for the super constructor: class TSExt extends TextStyle { //TSExt(Font font, Weight weight, double fontSize, Color color) : super(arguments); TSExt(Font font, Weight weight, double fontSize, Color color){ var args = { // ... }; super(args); } }

          – GoldenJoe
          Nov 26 '18 at 19:15







          This is great. For sake of academics, what if I kept the enums and constructed an argument list for the super constructor: class TSExt extends TextStyle { //TSExt(Font font, Weight weight, double fontSize, Color color) : super(arguments); TSExt(Font font, Weight weight, double fontSize, Color color){ var args = { // ... }; super(args); } }

          – GoldenJoe
          Nov 26 '18 at 19:15















          @GoldenJow It seems a good idea , if you tested this approch please, let us know

          – Saed Nabil
          Nov 27 '18 at 9:52





          @GoldenJow It seems a good idea , if you tested this approch please, let us know

          – Saed Nabil
          Nov 27 '18 at 9:52




















          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%2f53471137%2fhow-to-add-a-constructor-to-an-existing-class-in-dart%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python