0
0
Fluttermobile~20 mins

Code obfuscation and optimization in Flutter - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Code Obfuscation Demo
This screen shows a simple Flutter app demonstrating code optimization and obfuscation concepts by displaying a list of items with optimized code structure.
Target UI
-------------------------
| Code Obfuscation Demo |
-------------------------
| 1. Item One           |
| 2. Item Two           |
| 3. Item Three         |
-------------------------
Display a list of three items using a ListView
Use a separate widget for list items to optimize code reuse
Add comments explaining how code is optimized
Prepare the app for code obfuscation by avoiding reflection and dynamic code
Use const constructors where possible
Starter Code
Flutter
import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ObfuscationDemoScreen(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    // TODO: Implement the list view with optimized list item widgets
    return Scaffold(
      appBar: AppBar(
        title: const Text('Code Obfuscation Demo'),
      ),
      body: Center(
        child: Text('Loading items...'),
      ),
    );
  }
}

// TODO: Create a reusable ListItem widget here
Task 1
Task 2
Task 3
Task 4
Solution
Flutter
import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ObfuscationDemoScreen(),
    );
  }
}

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

  // List of items to display
  static const List<String> items = [
    'Item One',
    'Item Two',
    'Item Three',
  ];

  @override
  Widget build(BuildContext context) {
    // Using ListView.builder for efficient list rendering
    // Using const ListItem widgets for better performance and easier obfuscation
    return Scaffold(
      appBar: AppBar(
        title: const Text('Code Obfuscation Demo'),
      ),
      body: ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          // Passing item text to reusable ListItem widget
          return ListItem(text: items[index], index: index + 1);
        },
      ),
    );
  }
}

// Reusable list item widget with const constructor
// This helps Flutter optimize rebuilds and supports obfuscation
class ListItem extends StatelessWidget {
  final String text;
  final int index;

  const ListItem({super.key, required this.text, required this.index});

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Text('$index.'),
      title: Text(text),
    );
  }
}

This Flutter app demonstrates code optimization and preparation for code obfuscation by:

  • Using const constructors for widgets where possible to improve performance and reduce runtime overhead.
  • Separating the list item into a reusable ListItem widget to avoid code duplication and make the code cleaner.
  • Using ListView.builder for efficient list rendering, which is scalable for longer lists.
  • Avoiding dynamic code or reflection, which can break obfuscation tools.

This structure makes the code easier to maintain, faster to run, and ready for obfuscation to protect the app's source code.

Final Result
Completed Screen
-------------------------
| Code Obfuscation Demo |
-------------------------
| 1. Item One           |
| 2. Item Two           |
| 3. Item Three         |
-------------------------
User can scroll if the list grows longer (currently only 3 items)
No buttons or taps needed for this demo screen
Stretch Goal
Add a toggle button in the AppBar to switch between light and dark themes
💡 Hint
Use a StatefulWidget or state management to toggle ThemeMode and wrap MaterialApp with themeMode property