replaceMaterialApp static method

Future<void> replaceMaterialApp(
  1. String filePath,
  2. String newReturnWidget
)



Implementation

static Future<void> replaceMaterialApp(
  String filePath,
  String newReturnWidget,
) async {
  try {
    final file = File(filePath);

    if (!await file.exists()) {
      print('❌ File not found: $filePath');
      return;
    }

    String content = await file.readAsString();

    final materialReturnRegex = RegExp(
      r'return\s+MaterialApp\s*\(([\s\S]*?)\);',
      multiLine: true,
    );

    if (!materialReturnRegex.hasMatch(content)) {
      print('⚠️ No MaterialApp return widget found to replace');
      return;
    }

    final updatedContent = content.replaceAll(
      materialReturnRegex,
      "return $newReturnWidget",
    );

    await file.writeAsString(updatedContent);

    print('✅ MaterialApp replaced correctly');
  } catch (e) {
    print('❌ Error: $e');
  }
}