Use an IDE to efficiently assemble widgets

In Flutter, the UI is built by nesting layers of widgets. The development process of Flutter inevitably requires frequent changes to the Widget tree to insert or remove widgets.

In addition to manually writing Code and cutting and pasting to modify the Widget tree, there are more efficient methods you can use in Android Studio and Visual Studio Code:

  • The Android in the Studio

  • In Visual Studio Code

2. Understand the meaning of names beginning with underscores

The Dart language used by Flutter has no Java-like publice Protect Private, variables, functions, and classes beginning with _, meaning that it is only visible in the library

Libraries not only provide APIs, but are a unit of privacy: identifiers that start with an underscore (_) are visible only inside the library. provenance

Take a look at these examples to deepen your understanding:

  • variable

After the name variable starts with _, it inherits from Fruit, but because Apple is not in the same library, it does not have access to the name attribute of its parent.

  • function

The getName method, which starts with _, is inherited from Fruit, but because Apple is not in the same library, it does not have access to its parent’s getName method and does not need to implement it.

  • class

Banana can access the _Fruit class in the same library, but Apple cannot access the _Fruit class because it is not in the same library.

3. Distinguish final from const

In Dart, when you do not need to change a variable, you should use final or const instead of var to declare a variable.

A final variable can be assigned only once and must be initialized at definition time or in the constructor argument table.

Const is a compile-time constant whose value is known at compile time and is immutable.

The difference is that const is stricter than final. Here are some examples:

final List<String> list = [];
list.add('1'); // Const List<String> List = []; list.add('1'); Cannot add to an unmodifiable listCopy the code
final timestamp = new DateTime.now().millisecondsSinceEpoch; Const timestamp = new datetime.now (). MillisecondsSinceEpoch; // Const variables must be initialized with a constant valueCopy the code

The official documentation

4. Use the Debug Painting interface

Flutter provides Debug Painting to help us easily observe the layout of the interface and Debug the interface development

The premise is that the App is in debugging running state

  • The Android in the Studio

  • In Visual Studio Code

5. Use FocusScope to shift focus and hide input method

When developing apps, we often encounter the need to hide the input method when clicking on a blank area. When you think about input methods, you might want to use Method Channle, which lets native implementations hide input methods. In fact, we can shift focus by FocusScope and also hide the input method.

Container(height: 500.0, child: new GestureDetector(onTap: () {// Capture click events through GestureDetector, -- new FocusNode() focusScope.of (context).requestFocus(FocusNode()); }, child: Container(margin: EdgeInsets. All (30.0), child: ListView(children: <Widget>) InputDecoration(labelText:'Username'),
                ),
                TextField(
                  decoration: InputDecoration(labelText: 'Password'() [() () () () () (Copy the code

There is also a bug found in Flutter during development that the context passed to FocusScope cannot be under the MaterialApp, i.e. you need to place this part of the code in a separate Widget. Specifically refer to the last comment of this issue.