When I got familiar with the CodeSnippets feature in XCode, I was fascinated by it, and it saved me from typing code without prompting. So when I create the Dart file with Android Studio, I see the file is blank. It can endure? Follow infinite Google later to find LiveTemplate (class CodeSnippets) more powerful and then define your own shortcut code.

What is a Live template? Literally translated as “real-time template”, its mechanism is simply defined in advance some common code fragments inserted into the editor when writing code, using a method similar to code completion; Also support Groovy function customization, unlimited expansion.

/// groovyScript(<String>, [arg,... )Copy the code

How to customize the Live Template?

  1. Click Add a Live Template blank file;
  2. Fill in the Abbreviation shortcut keys;
  3. Description (optional);
  4. Template Text, Edit variables (essentially strings plus variables);
  5. Support language Settings;
  6. Click Apply, OK takes effect;

🌰 🌰 :

// // $fileName$// $projectName$// // Created by $user$on $date$$time$. // Copyright © $year$$user$.all rights reserved. //Copy the code

The corresponding relationship of the above variables is:

The fileName corresponding fileName ();

The user corresponding to the user ();

The date corresponding to the date ();

The time corresponding to the time ();

Year corresponding to the date ();

ProjectName is a bit more complicated. The official pre-defined method doesn’t provide the corresponding method for the projectName, so we need to write groovyScript code to get the corresponding result, like this;

groovyScript("def list = _1.split('/'); def result = list[4]; return result;" , filePath());Copy the code

Groovy SDK install VSCode install code Runner plugin; Groovy Basic syntax

How to export import? (Sometimes our company and home computers need to export and import for synchronization)

In user.xml is our custom LiveTemplate; If you have a great template, share it on Github. Lazy is nice!

Defined template: hCopyright

// // $fileName$// $projectName$// // Created by $user$on $date$$time$. // Copyright © $year$$user$.all rights reserved. //Copy the code

hstatelessWidget

import 'package:flutter/material.dart';

class $fileName$ extends StatelessWidget {

  final String? title;

  const $fileName$({
  	Key? key,
  	this.title,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    dynamic arguments = ModalRoute.of(context)!.settings.arguments;

    return Scaffold(
        appBar: AppBar(
          title: Text(arguments[1]),
        ),
        body: Text(arguments.toString())
    );
  }
}

Copy the code

hStatefulWidget

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class $fileName$ extends StatefulWidget { final String? title; $fileName$({ Key? key, this.title}) : super(key: key); @override _$fileName$State createState() => _$fileName$State(); } class _$fileName$State extends State<$fileName$> { @override void initState() { super.initState(); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { dynamic arguments = ModalRoute .of(context)! .settings .arguments; return Scaffold( appBar: AppBar( title: Text(arguments[1]), ), body: Text(arguments.toString()) ); }}Copy the code

hswitch_int

switch ($value$) {
  case $pattern$:
    {

    }
    break;
  case $pattern1$:
    {

    }
    break;
  case $pattern2$:
    {
    }
    break;
  default:
    break;
}
Copy the code

hswitch_string

switch ("") {
  case "":
    {

    }
    break;
  case "":
    {

    }
    break;
  case "":
    {
    }
    break;
  default:
    break;
}
Copy the code

hifelse

if () {
  
} else {
  
}
Copy the code