rulecms_widget 0.4.0
rulecms_widget: ^0.4.0 copied to clipboard
Flutter SDK for RuleCMS — fetch published/draft widgets by token + key and render the layout tree.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:rulecms_widget/rulecms_widget.dart';
void main() {
runApp(const ExampleApp());
}
/// Minimal library so this example depends only on `rulecms_widget`.
/// A real host registers `rulecms_source_components` from pub.dev.
ComponentLibrary _demoLibrary() {
return ComponentLibrary(
id: 'default',
version: 'example',
components: {
'r-text': LibraryComponentEntry(
id: 'r-text',
builder: (context) {
final text =
context.componentAttributes['content-text-3-resolutions']
?.toString() ??
'';
return Text(text);
},
),
},
);
}
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
// Replace with values from the RuleCMS Integration tab.
const token = String.fromEnvironment('RULECMS_TOKEN', defaultValue: '');
const publishedKey =
String.fromEnvironment('RULECMS_PUBLISHED_KEY', defaultValue: '');
final live = token.isNotEmpty && publishedKey.isNotEmpty;
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('rulecms_widget example')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: live
? RuleCMSWidgetProvider(
token: token,
libraries: {'default': _demoLibrary()},
child: const RuleCMSWidget(publishedKey: publishedKey),
)
: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Pass --dart-define=RULECMS_TOKEN=… and '
'RULECMS_PUBLISHED_KEY=… to load a live widget.\n\n'
'Offline demo:',
),
SizedBox(height: 16),
OfflineDemo(),
],
),
),
),
);
}
}
/// Offline demo used by widget tests / quick visual check without network.
class OfflineDemo extends StatelessWidget {
const OfflineDemo({super.key});
@override
Widget build(BuildContext context) {
final data = prepareRuleCMSWidgetData(
widget: const WidgetData(
publishedKey: 'demo',
name: 'Demo',
itemList: {
'rows': [
{
'id': 'r1',
'columns': [
{
'id': 'c1',
'type': 'r-text',
'componentAttributes': {
'content-text-3-resolutions': {
'all': 'Offline RuleCMS Flutter demo',
},
'color-3-resolutions': {
'all': {'unit': 'color', 'value': '#0F172A'},
},
'font-size-3-resolutions': {
'all': {'unit': 'px', 'value': 22},
},
},
},
],
},
],
},
),
childCollections: const {},
);
return RuleCMSWidgetProvider(
token: 'demo',
libraries: {'default': _demoLibrary()},
child: RuleCMSWidget(
publishedKey: 'demo',
mode: RuleCMSWidgetMode.preFetched,
initialData: data,
),
);
}
}