fluttercandies/loading_more_list

Custom empty.jpeg picture

trbsi opened this issue · 2 comments

trbsi commented

Is there any way to customize empty.jpeg picture? Or if there is no way, can that feature be added?

trbsi commented

I did it by looking at https://github.com/fluttercandies/loading_more_list#indicatorstatus
The entire code looks like this if this will help someone

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

  @override
  State<ConversationsScreen> createState() => _ConversationsScreenState();
}

class _ConversationsScreenState extends State<ConversationsScreen> {
  final ConversationBusinessLogic _businessLogic = ConversationBusinessLogic();
  final LoadConversationsService _loadConversations =
      LoadConversationsService();

  @override
  Widget build(BuildContext context) {
    return LoadingMoreList(
      ListConfig<ConversationValueObject>(
        indicatorBuilder: _buildIndicator,
        itemBuilder: _itemBuilder,
        sourceList: _loadConversations,
        padding: EdgeInsets.all(0.0),
      ),
    );
  }

  Widget _itemBuilder(
      BuildContext context, ConversationValueObject conversation, int index) {
    return Card(
      color: conversation.isRead ? Colors.white : Colors.teal.shade50,
      child: ListTile(
        leading: _businessLogic.imageFromBase64String(
            base64String: conversation.displayUser.profilePhoto, height: 30),
        title: Text(conversation.displayUser.username),
        subtitle: Text(conversation.newestMessage.bodyExcerpt),
        onTap: () {
          _businessLogic.markAsRead(conversationId: conversation.id);
          setState(() => conversation.isRead = true);
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) =>
                      MessageScreen(conversation: conversation)));
        },
      ),
    );
  }

  /**
   * Taken from https://github.com/fluttercandies/loading_more_list (IndicatorStatus)
   */
  Widget? _buildIndicator(BuildContext context, IndicatorStatus status) {
    //if your list is sliver list ,you should build sliver indicator for it
    //isSliver=true, when use it in sliver list
    bool isSliver = false;

    Widget widget;
    SpinKitRotatingCircle spinner = SpinKitRotatingCircle(
      color: AppHelper.appColor,
      size: 50.0,
    );
    switch (status) {
      case IndicatorStatus.none:
        widget = Container(height: 0.0);
        break;
      case IndicatorStatus.loadingMoreBusying:
        widget = Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(right: 5.0),
              height: 15.0,
              width: 15.0,
              child: spinner,
            ),
            Text("Loading...", style: TextStyle(fontSize: 20))
          ],
        );
        break;
      case IndicatorStatus.fullScreenBusying:
        widget = Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(right: 0.0),
              height: 30.0,
              width: 30.0,
              child: spinner,
            ),
            Text("Loading...")
          ],
        );
        if (isSliver) {
          widget = SliverFillRemaining(
            child: widget,
          );
        } else {
          widget = CustomScrollView(
            slivers: <Widget>[
              SliverFillRemaining(
                child: widget,
              )
            ],
          );
        }
        break;
      case IndicatorStatus.error:
        widget = Text('Something went wrong');
        widget = GestureDetector(
          onTap: () {
            _loadConversations.errorRefresh();
          },
          child: widget,
        );

        break;
      case IndicatorStatus.fullScreenError:
        widget = Text('Something went wrong');
        widget = GestureDetector(
          onTap: () {
            _loadConversations.errorRefresh();
          },
          child: widget,
        );
        if (isSliver) {
          widget = SliverFillRemaining(
            child: widget,
          );
        } else {
          widget = CustomScrollView(
            slivers: <Widget>[
              SliverFillRemaining(
                child: widget,
              )
            ],
          );
        }
        break;
      case IndicatorStatus.noMoreLoad:
        widget = Center(
          child: Text('No more conversations', style: TextStyle(fontSize: 20)),
        );
        break;
      case IndicatorStatus.empty:
        widget = EmptyWidget("No conversations",
            emptyWidget: Container(
                child: Center(
                    child: Text('No conversations',
                        style: TextStyle(fontSize: 20)))));
        if (isSliver) {
          widget = SliverToBoxAdapter(
            child: widget,
          );
        } else {
          widget = CustomScrollView(
            slivers: <Widget>[
              SliverFillRemaining(
                child: widget,
              )
            ],
          );
        }
        break;
    }
    return widget;
  }
}

And LoadConversationsService


class LoadConversationsService
    extends LoadingMoreBase<ConversationValueObject> {
  int pageindex = 1;
  bool _hasMore = true;
  bool forceRefresh = false;
  final GetConversationsService _getConversationsService =
      GetConversationsService();

  @override
  bool get hasMore => (_hasMore) || forceRefresh;

  @override
  Future<bool> refresh([bool clearBeforeRequest = false]) async {
    _hasMore = true;
    pageindex = 1;
    //force to refresh list when you don't want clear list before request
    //for the case, if your list already has 20 items.
    forceRefresh = !clearBeforeRequest;
    var result = await super.refresh(clearBeforeRequest);
    forceRefresh = false;
    return result;
  }

  @override
  Future<bool> loadData([bool isloadMoreAction = false]) async {
    bool isSuccess = false;
    try {
      //to show loading more clearly, in your app,remove this
      await Future.delayed(Duration(milliseconds: 500));

      ConversationCollectionValueObject conversations =
          await _getConversationsService.getConversations(pageindex);

      ConversationMetaValueObject meta =
          conversations.conversationMetaValueObject;

      if (pageindex == 1) {
        this.clear();
      }

      for (var item in conversations.listConversationValueObject) {
        if (!this.contains(item) && hasMore) {
          this.add(item);
        }
      }

      _hasMore = pageindex < meta.lastPage;
      pageindex++;
      isSuccess = true;
    } catch (exception, stack) {
      isSuccess = false;
      print('FREAKING EXCEPTION $exception');
      //print(stack);
    }
    return isSuccess;
  }
}