Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Dart by davidcookt ( 6 years ago )
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_ta_truyen/bloc/favorite/bloc.dart';
import 'package:flutter_ta_truyen/model/novel.dart';
import 'package:flutter_ta_truyen/router.dart';
import 'package:flutter_ta_truyen/screens/root/home_screen/widgets/novel_item.dart';
import 'package:flutter_ta_truyen/screens/root/library_screen/widgets/shimmer_loading.dart';
import 'package:flutter_ta_truyen/themes/colors.dart';
import 'package:flutter_ta_truyen/themes/fonts.dart';

class FavoriteTab extends StatefulWidget {
  FavoriteTab({Key key}) : super(key: key);

  @override
  _FavoriteTabState createState() => _FavoriteTabState();
}

class _FavoriteTabState extends State<FavoriteTab>
    with AutomaticKeepAliveClientMixin<FavoriteTab> {
  final _favoriteBloc = FavoriteNovelBloc();
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    FavoriteNovelBloc()..add(GetFavoriteNovel(1));
  }

  _ontap(context, Novel novel) {
    Navigator.of(context)
        .pushNamed(Router.NOVEL_DETAIL, arguments: novel)
        .then((value) => FavoriteNovelBloc()..add(GetFavoriteNovel(1)));
  }

  Future _onRefresh() async {
    FavoriteNovelBloc()..add(GetFavoriteNovel(1));
    return;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BlocBuilder<FavoriteNovelBloc, FavoriteNovelState>(
      builder: (context, state) {
        log(state.toString());
        if (state is FavoriteNovelUnInitial) {
          return Container();
        } else if (state is FavoriteNovelLoading) {
          log('Loading');
          return GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              childAspectRatio: 0.6,
            ),
            itemBuilder: (BuildContext ctx, index) {
              return ShimmerLoading();
            },
            itemCount: 9,
          );
        } else if (state is FavoriteNovelSuccess) {
          return RefreshIndicator(
            onRefresh: _onRefresh,
            child: GridView.builder(
              padding: const EdgeInsets.all(10),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                childAspectRatio: 0.4,
                mainAxisSpacing: 10,
                crossAxisSpacing: 10,
              ),
              itemBuilder: (BuildContext ctx, index) {
                return NovelItem(
                    novel: state.detail.results.story[index],
                    ontap: () =>
                        _ontap(context, state.detail.results.story[index]));
              },
              itemCount: state.detail.results.story.length,
            ),
          );
        } else {
          return Center(
            child: Text('Error'),
          );
        }
      },
    );
  }
}

 

Revise this Paste

Your Name: Code Language: