Skip to content

Instantly share code, notes, and snippets.

@M97Chahboun
Last active June 8, 2022 12:41

Revisions

  1. M97Chahboun revised this gist Jun 8, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions mini_view.dart
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@ import 'package:mvc_rocket/mvc_rocket.dart';
    class MiniView extends StatelessWidget {
    MiniView({Key? key, required this.title}) : super(key: key);
    final String title;
    // use mini for convert value to RocketValue
    final RocketValue<String> mcString = "Initial value".mini;
    final RocketValue<int> mcNum = 5.mini;
    final RocketValue<List> mcList = [].mini;
    @@ -20,6 +21,7 @@ class MiniView extends StatelessWidget {
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
    const Text("use View for every value"),
    // use your value in RocketMiniView and if value changed will rebuild widget for show your new value
    RocketMiniView(mcString, () => Text(mcString.v)),
    RocketMiniView(
    mcNum,
  2. M97Chahboun created this gist Jun 8, 2022.
    91 changes: 91 additions & 0 deletions mini_view.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    import 'dart:developer';

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

    class MiniView extends StatelessWidget {
    MiniView({Key? key, required this.title}) : super(key: key);
    final String title;
    final RocketValue<String> mcString = "Initial value".mini;
    final RocketValue<int> mcNum = 5.mini;
    final RocketValue<List> mcList = [].mini;
    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: Text(title),
    ),
    body: SingleChildScrollView(
    child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
    const Text("use View for every value"),
    RocketMiniView(mcString, () => Text(mcString.v)),
    RocketMiniView(
    mcNum,
    () => Text(mcNum.v.toString() +
    (mcNum.v.toString() == "11"
    ? " click to remove listener"
    : "")),
    ),
    RocketMiniView(
    mcList,
    () {
    return SizedBox(
    height: MediaQuery.of(context).size.height * 0.4,
    child: ListView.builder(
    itemCount: mcList.v.length,
    itemBuilder: (BuildContext context, int index) {
    return Center(child: Text(mcList.v[index].toString()));
    },
    ));
    },
    ),
    const SizedBox(
    height: 60.0,
    ),
    const Text("merge multiple values"),
    RocketMiniView(
    RocketValue.merge([mcString, mcNum, mcList]),
    () => Wrap(
    runAlignment: WrapAlignment.center,
    children: [
    Text(mcString.v),
    const Text("=>"),
    Text(mcNum.v.toString()),
    const Text("=>"),
    Text(mcList.v.toString())
    ],
    ),
    ),
    ],
    ),
    ),
    floatingActionButton: FloatingActionButton(
    backgroundColor: Theme.of(context).primaryColor,
    onPressed: () {
    mcNum.v++;
    mcString.v = "Value Changed";
    // dont use methods for add items or remove it use instead of it +/-
    mcList.v += [mcNum.v, mcString.v];
    if (mcNum.v == 6) {
    mcNum.registerListener(rocketMiniRebuild, valChanged);
    mcNum.registerListener(rocketMergesRebuild, () {
    log('this listener called when widget of merges values rebuild');
    });
    }
    if (mcNum.v == 12) {
    mcNum.removeListener(rocketMiniRebuild, valChanged);
    log("listener removed!!!");
    }
    },
    tooltip: 'change Value',
    child: const Icon(Icons.change_circle),
    ),
    );
    }

    valChanged() {
    log('this listener called when widget of mcNum rebuild');
    }
    }