molc 0.0.4 copy "molc: ^0.0.4" to clipboard
molc: ^0.0.4 copied to clipboard

outdated

A ui component base on provider.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:molc/molc.dart';

void main() {
  runApp(MaterialApp(
    home: const MainPage(),
  ));
}

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

  @override
  Widget build(BuildContext context) {
    return MoLcWidget<_MainModel, _MainLogic>(
      modelCreate: (_) => _MainModel(),
      logicCreate: (_) => _MainLogic(),
      init: (_, model, logic) => logic.init(model),
      builder: (_, model, logic, __) {
        debugPrint('build==>${this.runtimeType}');
        return Container(
          color: Colors.white,
          alignment: Alignment.center,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextButton(
                onPressed: () {
                  model.refresh();
                },
                child: Text('refresh MainModel'),
              ),
              Part1(),
              SizedBox(
                height: 100,
              ),
              Part2(),
              SizedBox(
                height: 100,
              ),
              Part3(),
            ],
          ),
        );
      },
    );
  }
}

class _MainModel extends Model {}

class _MainLogic extends Logic {
  void init(_MainModel model) {}
}

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

  @override
  Widget build(BuildContext context) {
    return ModelWidget<_Part1Model>(
      create: (_) => _Part1Model(),
      builder: (_, model, __) {
        debugPrint('build==>${this.runtimeType}');
        return Row(
          children: [
            Text(
              'part1:${model.part1Num}',
            ),
            TextButton(
              onPressed: () {
                model.part1Num += 1;
                model.refresh();
              },
              child: Text('refresh _Part1Model'),
            ),
          ],
        );
      },
    );
  }
}

class _Part1Model extends Model {
  int part1Num = 66;
}

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

  @override
  Widget build(BuildContext context) {
    return LogicWidget<_Part2Logic>(
      create: (_) => _Part2Logic(),
      init: (_, logic) => logic.init(),
      builder: (context, logic) {
        debugPrint('build==>${this.runtimeType}');
        return Row(
          children: [
            TextButton(
              onPressed: () {
                logic.request(context);
              },
              child: Text('_Part2Logic.request\nthen refresh _MainModel'),
            ),
          ],
        );
      },
    );
  }
}

class _Part2Logic extends Logic {
  void init() {}

  void request(BuildContext context) async {
    await Future.delayed(Duration(seconds: 1));
    context.read<_MainModel>().refresh();
  }
}

class Part3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return NoMoWidget<int>(
      value: 99,
      builder: (_, model, __) {
        debugPrint('build==>${this.runtimeType}');
        return Row(
          children: [
            Text(
              'nomo:${model.value}',
            ),
            TextButton(
              onPressed: () {
                model
                  ..value += 1
                  ..refresh();
              },
              child: Text('refresh _NoMo'),
            ),
          ],
        );
      },
    );
  }
}
3
likes
0
points
56
downloads

Publisher

unverified uploader

Weekly Downloads

A ui component base on provider.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, provider

More

Packages that depend on molc