flutterx_reflection 1.0.2-dev copy "flutterx_reflection: ^1.0.2-dev" to clipboard
flutterx_reflection: ^1.0.2-dev copied to clipboard

unlistedoutdated

Allows to instantiate classes at runtime without using dart mirror

example/lib/main.dart

import 'package:example/framework/flutter_sdk.framework.dart';
import 'package:flutter/material.dart';
import 'package:flutterx_reflection/flutterx_reflection.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutterx Reflection Demo',
      theme: ThemeData(primarySwatch: Colors.deepOrange),
      home: const ReflectionExample());
}

class ReflectionExample extends StatefulWidget {
  const ReflectionExample({Key? key}) : super(key: key);

  @override
  State<ReflectionExample> createState() => _ReflectionExampleState();
}

class _ReflectionExampleState extends State<ReflectionExample> {
  bool _row = false;

  @override
  Widget build(BuildContext context) => Scaffold(
      appBar: AppBar(title: const Text('Reflection example')),
      body: Center(child: _buildContent()),
      floatingActionButton:
          FloatingActionButton(onPressed: () => setState(() => _row = !_row), child: const Icon(Icons.tag)));

  Widget _buildContent() {
    final className = _row ? 'Row' : 'Column';
    final sClass = FlutterSdk.lookUp('package:flutter/material.dart:$className') as FlutterClass;
    final constructor = sClass.constructor();
    final provider = _ColumnInstanceProvider({
      'children': <Widget>[
        const Padding(padding: EdgeInsets.all(8.0), child: Text('HELLO FROM REFLECTION')),
        Text('This is a $className'),
      ],
      'mainAxisAlignment': MainAxisAlignment.center,
    });
    return sClass.newInstance(constructor: constructor, provider: provider);
  }
}

class _ColumnInstanceProvider extends InstanceProvider {
  final Map<String, dynamic> properties;

  const _ColumnInstanceProvider(this.properties);

  @override
  dynamic provide(ParameterContext context) => properties[context.parameter.name] ?? context.parameter.defaultValue;
}
1
likes
0
points
16
downloads

Publisher

unverified uploader

Weekly Downloads

Allows to instantiate classes at runtime without using dart mirror

Repository (GitLab)
View/report issues

License

unknown (license)

Dependencies

collection, flutter, flutterx_utils

More

Packages that depend on flutterx_reflection