简体   繁体   中英

Custom widget constructor in Flutter

In my Flutter course we are building a custom widget, a reusable card as below.

I wanted to know why we are defining final Color color after the constructor ReusableCard({@required this.colour}) and not before?


 class ReusableCard extends StatelessWidget {

 ReusableCard({@required this.colour});

 final Color colour;

 @override
 Widget build(BuildContext context) {
   return Container(
     margin: EdgeInsets.all(15.0),
     decoration: BoxDecoration(
       color: colour,
       borderRadius: BorderRadius.circular(10.0),
     ),
     height: 200,
     width: 170,
   );
 }
}

In fact, is recommended to sort constructor declarations before other members.

https://dart-lang.github.io/linter/lints/sort_constructors_first.html

It really all boils down to preference. There isn't a strict rule that tells you that the variable and constructor has to be defined in a certain order. It doesn't really matter even if your code looks like this:

 class ReusableCard extends StatelessWidget {
 final Color colour;
 ReusableCard({@required this.colour});

 @override
 Widget build(BuildContext context) {
   return Container(
     margin: EdgeInsets.all(15.0),
     decoration: BoxDecoration(
       color: colour,
       borderRadius: BorderRadius.circular(10.0),
     ),
     height: 200,
     width: 170,
   );
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM