Skip to content

Instantly share code, notes, and snippets.

@M97Chahboun
Last active June 26, 2022 16:22

Revisions

  1. M97Chahboun revised this gist Jun 26, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions mvcRocket_post_view.dart
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,8 @@ class PostExample extends StatelessWidget {
    // or
    // callType: CallType.callAsStream,
    // secondsOfStream: 1,
    // or
    // callType: CallType.callAsFuture,
    // customized your loading (default widget is CircularProgressIndicator)
    // loader:CustomLoading(),

  2. M97Chahboun revised this gist Jun 25, 2022. 1 changed file with 16 additions and 5 deletions.
    21 changes: 16 additions & 5 deletions mvcRocket_post_view.dart
    Original file line number Diff line number Diff line change
    @@ -79,6 +79,16 @@ class PostExample extends StatelessWidget {
    return ListTile(
    leading: Text(currentPost.id.toString()),
    title: Text(currentPost.title!),
    trailing: IconButton(
    color: Colors.brown,
    icon: const Icon(Icons.update),
    onPressed: () {
    // update post data
    currentPost.updateFields(
    bodyField: "This Body changed",
    titleField: "This Title changed");
    },
    ),
    onTap: () => Navigator.of(context).push(
    MaterialPageRoute(
    builder: (BuildContext context) {
    @@ -101,15 +111,16 @@ class Details extends StatelessWidget {
    Details(this.index, {Key? key}) : super(key: key);
    @override
    Widget build(BuildContext context) {
    Post currentPost = post.multi![index];
    return Scaffold(
    appBar: AppBar(title: Text(post.multi![index].title!)),
    appBar: AppBar(title: Text(currentPost.title!)),
    body: Center(
    child: ListTile(
    leading: Text(post.multi![index].id.toString()),
    title: Text(post.multi![index].title!),
    subtitle: Text(post.multi![index].body!),
    leading: Text(currentPost.id.toString()),
    title: Text(currentPost.title!),
    subtitle: Text(currentPost.body!),
    ),
    ),
    );
    }
    }
    }
  3. M97Chahboun created this gist Jun 8, 2022.
    115 changes: 115 additions & 0 deletions mvcRocket_post_view.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,115 @@
    import 'dart:io';

    import 'package:example/models/post_model.dart';
    import 'package:example/requests/post_request.dart';
    import 'package:flutter/material.dart';
    import 'package:mvc_rocket/mvc_rocket.dart';

    class PostExample extends StatelessWidget {
    // Save your model to use on another screen
    // readOnly means if you close and open this screen you will use same data without update it from Api
    // [rocket] is instance of Mccontroller injected in Object by extension for use it easily anywhere
    final Post post =
    RocketController().add<Post>(postsEndpoint, Post(), readOnly: true);

    PostExample({Key? key, required this.title}) : super(key: key);
    final String title;
    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: const Text(
    "Refresh Posts with swip to down or from here =>",
    style: TextStyle(fontSize: 11.0),
    ),
    actions: [
    IconButton(
    icon: const Icon(Icons.data_usage),
    // Refresh Data from Api
    onPressed: () => GetPosts.getPosts(post))
    ],
    ),
    body: SizedBox(
    height: MediaQuery.of(context).size.height,
    width: MediaQuery.of(context).size.width,
    child: RefreshIndicator(
    onRefresh: () {
    return GetPosts.getPosts(post);
    },
    child: RocketView(
    // call api method
    call: () => GetPosts.getPosts(post),
    // your model generated
    model: post,
    // call call Voidcallback if model empty
    callType: CallType.callIfModelEmpty,
    // or
    // callType: CallType.callAsStream,
    // secondsOfStream: 1,
    // customized your loading (default widget is CircularProgressIndicator)
    // loader:CustomLoading(),

    // handle errors
    onError: (RocketException exception, Function() reload) {
    return Center(
    child: Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
    Text(exception.exception),
    if (exception.statusCode != HttpStatus.ok) ...[
    Text(exception.response),
    Text(rocket
    .get(rocketRequestKey)
    .msgByStatusCode(exception.statusCode))
    ],
    TextButton(
    onPressed: reload, child: const Text("retry"))
    ],
    ),
    );
    },
    builder: (context) {
    return SizedBox(
    height: MediaQuery.of(context).size.height * 0.852,
    child: ListView.builder(
    itemCount: post.multi!.length,
    itemBuilder: (BuildContext context, int index) {
    // your data saved in multi list as Post model
    Post currentPost = post.multi![index];
    return ListTile(
    leading: Text(currentPost.id.toString()),
    title: Text(currentPost.title!),
    onTap: () => Navigator.of(context).push(
    MaterialPageRoute(
    builder: (BuildContext context) {
    return Details(index);
    }),
    ));
    },
    ),
    );
    },
    )),
    ));
    }
    }

    class Details extends StatelessWidget {
    final int index;
    // get your model by key
    final Post post = RocketController().get<Post>(postsEndpoint);
    Details(this.index, {Key? key}) : super(key: key);
    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(title: Text(post.multi![index].title!)),
    body: Center(
    child: ListTile(
    leading: Text(post.multi![index].id.toString()),
    title: Text(post.multi![index].title!),
    subtitle: Text(post.multi![index].body!),
    ),
    ),
    );
    }
    }