generatePageModule function
Future<void>
generatePageModule(
- String projectPath,
- String pageName, {
- String? jsonInput,
- String? basePathOverride,
- String? uiType,
- String? additionalImports,
- bool addApiCalling = false,
- bool hasBottomBar = false,
- bool hasDrawer = false,
- List<
String> tabNames = const [], - List<
String> drawerNames = const [], - List<
Map< signupFields = const [],String, String> > - bool isCurved = true,
- bool overwritePage = false,
- String? figmaCode,
Command used to generate enterprise page modules.
Implementation
Future<void> generatePageModule(
String projectPath,
String pageName, {
String? jsonInput,
String? basePathOverride,
String? uiType,
String? additionalImports,
bool addApiCalling = false,
bool hasBottomBar = false,
bool hasDrawer = false,
List<String> tabNames = const [],
List<String> drawerNames = const [],
List<Map<String, String>> signupFields = const [],
bool isCurved = true,
bool overwritePage = false,
String? figmaCode,
}) async {
final normalizedPageName = toSnakeCase(pageName);
final template = uiType != null ? TemplateRegistry.getTemplate(uiType) : null;
String? autoImports;
if (template != null) {
await _installTemplateDependencies(projectPath, template);
autoImports =
_generateTemplateImports(getProjectName(projectPath), template);
}
final projectName = getProjectName(projectPath);
final figmaImports = figmaCode != null ? "import 'package:$projectName/presentation/widgets/custom_body.dart';\nimport 'package:$projectName/presentation/widgets/text_widget.dart';\nimport 'package:$projectName/presentation/widgets/text_input_widget.dart';\nimport 'package:$projectName/core/utils/responsive_size.dart';\nimport 'package:$projectName/core/theme/app_colors.dart';\n" : "";
final effectiveAdditionalImports =
(additionalImports ?? "") + (autoImports ?? "") + figmaImports;
final state = detectStateManagement(projectPath);
final basePath = basePathOverride ??
p.join(
projectPath,
'lib',
'presentation',
'pages',
normalizedPageName,
);
final modelDir = p.join(basePath, 'model');
safeCreateDir(basePath);
safeCreateDir(modelDir);
final className = toPascalCase(pageName);
if (state == 'bloc') {
await _ensureBlocInstalled(projectPath);
final blocDir = p.join(basePath, 'bloc');
safeCreateDir(blocDir);
// Bloc
safeWriteFile(
p.join(blocDir, '${normalizedPageName}_bloc.dart'),
'''
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';
${addApiCalling ? "import 'package:$projectName/core/services/api_service.dart';" : ""}
import '${normalizedPageName}_event.dart';
import '${normalizedPageName}_state.dart';
class ${className}Bloc extends Bloc<${className}Event, ${className}State> {
${normalizedPageName == 'dashboard' ? 'final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();' : ''}
${className}Bloc() : super(const ${className}State()) {
on<${className}Started>((event, emit) async {
emit(state.copyWith(status: ${className}Status.loading));
try {
await Future.delayed(const Duration(seconds: 1));
emit(state.copyWith(status: ${className}Status.success));
} catch (e) {
emit(state.copyWith(
status: ${className}Status.error,
message: e.toString(),
));
}
});
${normalizedPageName == 'dashboard' ? '''
on<${className}ChangeTab>((event, emit) {
emit(state.copyWith(selectedIndex: event.index));
});
''' : ''}
${addApiCalling ? '''
on<${className}Submit>((event, emit) async {
emit(state.copyWith(status: ${className}Status.loading));
try {
final response = await ApiService.client.post(
"your api url",
data: {}, // your api request
);
emit(state.copyWith(status: ${className}Status.success));
} catch (e) {
emit(state.copyWith(
status: ${className}Status.error,
message: e.toString(),
));
}
});
''' : ""}
${uiType == 'faq' ? '''
on<${className}ToggleFaq>((event, emit) {
final newIndices = List<int>.from(state.expandedIndices);
if (newIndices.contains(event.index)) {
newIndices.remove(event.index);
} else {
newIndices.add(event.index);
}
emit(state.copyWith(expandedIndices: newIndices));
});
''' : ''}
}
}
''',
overwrite: overwritePage,
projectPath: projectPath);
// Event
safeWriteFile(
p.join(blocDir, '${normalizedPageName}_event.dart'),
'''
abstract class ${className}Event {}
class ${className}Started extends ${className}Event {}
${addApiCalling ? "class ${className}Submit extends ${className}Event {}" : ""}
${normalizedPageName == 'dashboard' ? 'class ${className}ChangeTab extends ${className}Event { final int index; ${className}ChangeTab(this.index); }' : ''}
${uiType == 'faq' ? 'class ${className}ToggleFaq extends ${className}Event { final int index; ${className}ToggleFaq(this.index); }' : ''}
''',
overwrite: overwritePage,
projectPath: projectPath);
// State
safeWriteFile(
p.join(blocDir, '${normalizedPageName}_state.dart'),
'''
import 'package:equatable/equatable.dart';
enum ${className}Status {
initial,
loading,
success,
error
}
class ${className}State extends Equatable {
final ${className}Status status;
final String? message;
${normalizedPageName == 'dashboard' ? 'final int selectedIndex;' : ''}
${uiType == 'faq' ? 'final List<int> expandedIndices;' : ''}
const ${className}State({
this.status = ${className}Status.initial,
this.message,
${normalizedPageName == 'dashboard' ? 'this.selectedIndex = 0,' : ''}
${uiType == 'faq' ? 'this.expandedIndices = const [],' : ''}
});
${className}State copyWith({
${className}Status? status,
String? message,
${normalizedPageName == 'dashboard' ? 'int? selectedIndex,' : ''}
${uiType == 'faq' ? 'List<int>? expandedIndices,' : ''}
}) {
return ${className}State(
status: status ?? this.status,
message: message ?? this.message,
${normalizedPageName == 'dashboard' ? 'selectedIndex: selectedIndex ?? this.selectedIndex,' : ''}
${uiType == 'faq' ? 'expandedIndices: expandedIndices ?? this.expandedIndices,' : ''}
);
}
@override
List<Object?> get props => [
status,
message,
${normalizedPageName == 'dashboard' ? 'selectedIndex,' : ''}
${uiType == 'faq' ? 'expandedIndices,' : ''}
];
}
''',
overwrite: overwritePage,
projectPath: projectPath);
// Page
final pageContent = '''
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:$projectName/presentation/widgets/custom_body.dart';
${effectiveAdditionalImports}
import 'bloc/${normalizedPageName}_bloc.dart';
import 'bloc/${normalizedPageName}_state.dart';
import 'bloc/${normalizedPageName}_event.dart';
class ${className}Page extends StatelessWidget {
const ${className}Page({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => ${className}Bloc()..add(${className}Started()),
child: BlocListener<${className}Bloc, ${className}State>(
listener: (context, state) {},
child: BlocBuilder<${className}Bloc, ${className}State>(
builder: (context, state) {
final isLoading = state.status == ${className}Status.loading;
return ${figmaCode ?? (uiType != null ? _getUiBody(uiType, className, 'isLoading', projectName, addApiCalling: addApiCalling, hasBottomBar: hasBottomBar, hasDrawer: hasDrawer, tabNames: tabNames, drawerNames: drawerNames, signupFields: signupFields, isCurved: isCurved) : '''Scaffold(
appBar: AppBar(title: const Text("${className} Page")),
body: const Center(child: Text("${className} Page")),
)''')};
},
),
),
);
}
}
''';
if (overwritePage) {
forceWriteFile(p.join(basePath, '${normalizedPageName}_page.dart'), pageContent);
} else {
safeWriteFile(p.join(basePath, '${normalizedPageName}_page.dart'), pageContent);
}
} else {
await _ensureRiverpodInstalled(projectPath);
final providerDir = p.join(basePath, 'provider');
safeCreateDir(providerDir);
safeWriteFile(p.join(providerDir, '${normalizedPageName}_provider.dart'), '''
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../utils/string_utils.dart';
final ${normalizedPageName}Provider =
NotifierProvider<${className}Controller, ${className}State>(
${className}Controller.new,
);
class ${className}Controller extends Notifier<${className}State> {
@override
${className}State build() {
return const ${className}State();
}
Future<void> load() async {
state = state.copyWith(loading: true);
await Future.delayed(const Duration(seconds: 1));
state = state.copyWith(loading: false);
}
${addApiCalling ? '''
Future<void> submit() async {
state = state.copyWith(loading: true);
try {
// final response = await ApiService.client.post("your api url", data: {});
await Future.delayed(const Duration(seconds: 1));
state = state.copyWith(loading: false);
} catch (e) {
state = state.copyWith(loading: false);
}
}
''' : ""}
}
class ${className}State {
final bool loading;
const ${className}State({
this.loading = false,
});
${className}State copyWith({
bool? loading,
}) {
return ${className}State(
loading: loading ?? this.loading,
);
}
}
''');
final pageContent = '''
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
${effectiveAdditionalImports}
import 'provider/${normalizedPageName}_provider.dart';
class ${className}Page extends ConsumerWidget {
const ${className}Page({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(${normalizedPageName}Provider);
return ${figmaCode ?? (uiType != null ? _getUiBody(uiType, className, 'state.loading', projectName, addApiCalling: addApiCalling, hasBottomBar: hasBottomBar, hasDrawer: hasDrawer, tabNames: tabNames, drawerNames: drawerNames, signupFields: signupFields, isCurved: isCurved) : '''Scaffold(
appBar: AppBar(
title: const Text("${className}"),
),
body: Center(
child: ElevatedButton(
onPressed: () {
ref.read(${normalizedPageName}Provider.notifier).load();
},
child: const Text("Load"),
),
),
)''')};
}
}
''';
if (overwritePage) {
forceWriteFile(p.join(basePath, '${normalizedPageName}_page.dart'), pageContent);
} else {
safeWriteFile(p.join(basePath, '${normalizedPageName}_page.dart'), pageContent);
}
}
// ===================== MODEL =====================
final modelPath = p.join(modelDir, '${normalizedPageName}_model.dart');
if (jsonInput != null && jsonInput.isNotEmpty) {
final modelCode = generateModelFromJson(className, jsonInput);
/// allow overwrite (API may change)
safeWriteFile(modelPath, modelCode,
overwrite: true, projectPath: projectPath);
print('🔄 ${p.basename(modelPath)} updated.');
} else {
safeWriteFile(modelPath, '''
class ${className}Model {
final String? id;
${className}Model({
this.id,
});
}
''');
}
print(
'✅ Enterprise page module "$normalizedPageName" generated successfully using $state.');
}