Skip to content

Instantly share code, notes, and snippets.

@wilsonsilva
Last active June 9, 2023 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilsonsilva/00a0945caac2febc50061b4b1325283e to your computer and use it in GitHub Desktop.
Save wilsonsilva/00a0945caac2febc50061b4b1325283e to your computer and use it in GitHub Desktop.
Remove the debug banner from the Flutter app templates
#!/bin/bash
# Get the location of the Flutter SDK
flutter_path=$(which flutter)
# Check if Flutter SDK is found
if [[ -z "$flutter_path" ]]; then
echo "Flutter SDK not found. Please install Flutter and add it to your PATH."
exit 1
fi
flutter_path=${flutter_path%/*/*}
# Get the path to the file to be patched
file_to_patch="$flutter_path/packages/flutter_tools/templates/app/lib/main.dart.tmpl"
# Check if the file exists
if [[ ! -f "$file_to_patch" ]]; then
echo "File not found!"
exit 1
fi
# Check if the file has already been patched
if grep -q "debugShowCheckedModeBanner: false," "$file_to_patch"; then
echo "The file has already been patched."
exit 0
fi
# Use awk to insert debugShowCheckedModeBanner: false, after MaterialApp
echo "Creating patch for ${file_to_patch}"
awk '
/MaterialApp\(/ {
print $0
print " debugShowCheckedModeBanner: false,"
next
}
{ print }
' "$file_to_patch" > temp
# Show a diff before patching
echo "Here is the proposed change:"
diff --color -u "$file_to_patch" temp
# Ask the user if they want to proceed
read -p "Do you want to apply these changes? (y/n) " answer
case ${answer:0:1} in
y|Y )
echo "Applying changes..."
mv temp "$file_to_patch"
;;
* )
echo "Aborting..."
rm temp
;;
esac
@wilsonsilva
Copy link
Author

Run this every time you update the SDK. Use it with flutter create -e myapp to generate a minimal lib/main.dart.

Before:

import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}

After:

import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}

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