DEV Community

loading...
Cover image for Flutter & Dart Tips - Week In Review #7

Flutter & Dart Tips - Week In Review #7

Offline Programmer
Full Stack Architect | Problem Solver | Lifelong Learner
・2 min read

Hello Reader,

So far, I have shared with you 38 Flutter\Dart tips. In this 7th post of the Flutter & Dart Tips series, I will be sharing six more.

1- SafeArea is a padding widget that inserts its child with enough padding to keep it from being blocked by the system status bar, notches, holes, rounded corners, and others.


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: true,
        bottom: true,
        left: true,
        right: true,
        minimum: EdgeInsets.all(16.0),
        maintainBottomViewPadding: true,
        child: Text('Check this important text'),
      ),
    );
  }

Enter fullscreen mode Exit fullscreen mode

2- Use MediaQuery to get information about the current device size, its orientation, and user preferences.


...

  var orientation, size, height, width;
  // getting the orientation of the app
  orientation = MediaQuery.of(context).orientation;

  //size of the widow
  size = MediaQuery.of(context).size;
  height = size.height;
  width = size.width;

  ...

  // checking the orientation
  body: orientation == Orientation.portrait
      ? Container(
          color: Colors.green,
          height: height / 4,
          width: width / 4,
        )
      : Container(
          height: height / 3,
          width: width / 3,
          color: Colors.yellow,
        ),

Enter fullscreen mode Exit fullscreen mode

3- Mixins allow you to plug in blocks of code without needing to create subclasses.


...


    mixin Fluttering {
      void flutter() {
        print('fluttering');
      }
    }



    abstract class Bird with Fluttering {
      void flap() {
        print('flap flap');
      }
    }

    class Hawk extends Bird {
      void doHawkThing() {
        flap();
        flutter();
        print('Hawk is Flying');
      }
    }


    main() {
      var hawk = new Hawk();
      hawk.doHawkThing();

    }

Enter fullscreen mode Exit fullscreen mode

4- You can pass a function as a parameter to another function.


  void main() {
    const list = ['London', 'Dublin', 'Belfast'];
   list.forEach(
      (item) => print('City Of $item'));
  }

Enter fullscreen mode Exit fullscreen mode

5- Getters and setters are special methods that provide read and write access to an object’s properties.


  class Rectangle {
    double left, top, width, height;

    Rectangle(this.left, this.top, this.width, this.height);

    // Define two calculated properties: right and bottom.
    double get right => left + width;
    set right(double value) => left = value - width;
    double get bottom => top + height;
    set bottom(double value) => top = value - height;
  }

  void main() {
    var rect = Rectangle(3, 4, 20, 15);
    print(rect.left);
    rect.right = 12;
    print(rect.left);
  }

Enter fullscreen mode Exit fullscreen mode

6- There are four different ways to append or concatenate Strings


  void main() {

  //1- Using ‘+’ operator.

    String str1 = "Dart";
    String str2 = "Is";
    String str3 = "Fun";

    print(str1 + str2 + str3);

  //2- String interpolation.

    print('$str1 $str2 $str3');

  //3- Directly writing string literals.

    print('Dart' 'Is' 'Fun');

  //4- Strings.join() method.

    // Creating List of strings
    List<String> str = ["Dart", "Is", "Fun"];

    String _thestr = str.join();
    print(_thestr);

    _thestr = str.join(" ");
    print(_thestr);
  }

Enter fullscreen mode Exit fullscreen mode

See you next week. 👋🏻

Follow me on Twitter for more tips about #coding, #learning, #technology...etc.

Check my Apps on Google Play & Apple Store

Cover image NASA on Unsplash

Discussion (0)