widgetTemplate function

String widgetTemplate(
  1. String name,
  2. ProjectContext ctx
)

A reusable widget placed in the shared widgets folder, following the library naming style. Uses MyText/AppColors when those modules are present.

Implementation

String widgetTemplate(String name, ProjectContext ctx) {
  final className = Naming.pascal(name);
  final pkg = ctx.packageName ?? 'app';

  final imports = <String>["import 'package:flutter/material.dart';"];
  final String label;
  if (ctx.hasWidgets) {
    imports.add("import 'package:$pkg/app/shared_widgets/my_text.dart';");
    label = "const MyText(title: '$className')";
  } else {
    label = "const Text('$className')";
  }

  return '''
${imports.join('\n')}

/// A reusable $className widget. Generated by river_cli.
class $className extends StatelessWidget {
  const $className({super.key});

  @override
  Widget build(BuildContext context) {
    return $label;
  }
}
''';
}