Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flutter / Stacked snippets for productivity
{
"Freezed model": {
"prefix": "frz",
"body": [
"@freezed",
"class ${1:${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g}} with _$${1} {",
" factory ${1}({",
" @required ${2:String id},",
" }) = _${1};",
"}"
],
"description": "Freezed model"
},
"Freezed model Json": {
"prefix": "frzjs",
"body": [
"@freezed",
"class ${1:${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g}} with _$${1} {",
" factory ${1}({",
" @required ${2:String id},",
" }) = _${1};",
"",
"factory ${1}.fromJson(Map<String, dynamic> json) => ",
"_$${1}FromJson(json);",
"}"
],
"description": "Freezed model with Json"
},
"Stacked View": {
"prefix": "stkv",
"body": [
"import 'package:flutter/material.dart';",
"import 'package:stacked/stacked.dart';",
"",
"class ${1} extends StatelessWidget {",
" const ${1}({Key? key}) : super(key: key);",
"",
" @override",
" Widget build(BuildContext context) {",
" return ViewModelBuilder<${1}Model>.reactive(",
" builder: (context, model, child) => Scaffold(),",
" viewModelBuilder: () => ${1}Model(),",
" );",
" }",
"}"
],
"description": "Stacked View"
},
"Stacked BaseViewModel": {
"prefix": "stkbvm",
"body": [
"import 'package:stacked/stacked.dart';",
"",
"class ${1}Model extends BaseViewModel {}",
],
"description": "Stacked BaseViewModel"
},
"Mock Registration": {
"prefix": "testr",
"body": [
"${1} getAndRegister${1}() {",
" _removeRegistrationIfExists<${1}>();",
" final service = Mock${1}();",
" locator.registerSingleton<${1}>(service);",
" return service;",
"}"
],
"description": "Creating a Mock Registration Function"
},
"Main Test Suite Setup": {
"prefix": "testm",
"body": [
"import 'package:flutter_test/flutter_test.dart';",
"",
"void main() {",
" group('${1:${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g}} -', (){",
"",
" });",
"}"
],
"description": "Main Test Suite Setup"
},
"Main Test Suite Setup with Services": {
"prefix": "testmr",
"body": [
"import 'package:flutter_test/flutter_test.dart';",
"",
"void main() {",
" group('${1:${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g}} -', (){",
" setUp(() => registerServices());",
" tearDown(() => unregisterServices());",
" });",
"}"
],
"description": "Main Test Suite Setup with Services"
},
"Test Group Setup": {
"prefix": "testg",
"description": "Creates a Test group with a test",
"body": [
"group('${1} -', () {",
" test('${2}', () {",
"",
" });",
"});",
]
},
"Single Test Setup": {
"prefix": "tests",
"description": "Creates a single test",
"body": [
" test('${1}', () {",
"",
" });",
]
},
}
@mingsai
Copy link

mingsai commented Mar 13, 2022

@demolaf @djoshua21 @Edamijueda

How to convert these snippets for use in Android Studio

  1. Create a Dart scratch file (Cmd+Shift+N)
  2. Paste in the body of any snippet into the scratch file
  3. Remove the double quotes and extra commas (Cmd+R is the replace command)
  4. Replace the token placeholders with variable names (${1} =&gt; $placeholderName$)
  5. Highlight the transformed code, and go to Tools > Save as Live Template
  6. Add the abbreviation and description as seen in the dialog box below
  7. Make sure to add a default value to each variable used (see the edit variables button on live template dialog below)
  8. Once saved you can use the stkv template anywhere in the Flutter/Dart scope

For example (transform the body of stkv - line 31 from this)

"import 'package:flutter/material.dart';",
      "import 'package:stacked/stacked.dart';",
      "",
      "class ${1} extends StatelessWidget {",
      " const ${1}({Key? key}) : super(key: key);",
      "",
      " @override",
      " Widget build(BuildContext context) {",
      "   return ViewModelBuilder<${1}Model>.reactive(",
      "     builder: (context, model, child) => Scaffold(),",
      "     viewModelBuilder: () => ${1}Model(),",
      "   );",
      " }",
      "}"

The translated snippet should look something like this:

import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';

class $classname$ extends StatelessWidget {
 const $classname$({Key? key}) : super(key: key);

 @override
 Widget build(BuildContext context) {
   return ViewModelBuilder<$viewName$Model>.reactive(
     builder: (context, model, child) => Scaffold(),
     viewModelBuilder: () => $viewName$Model(),
   );
 }
}

Live Template Dialog box

Screen Shot 2022-03-13 at 10 49 40 AM

P.S.
Pretty sure this will work for IntelliJ as well since AS is built on top

References
Scratch Files
Android Studio Code Snippets
Editing live templates - IntelliJ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment