0

I downloaded a project from GitHub where the app shows a list of item category wise. At the top it shows the Title of the category and then List of items in that category. The problem is both the Title and List of items are assigned to ListView whereas i want to show only items in GridView with the title only showing on the top and not being added to the grid.

categories declared above:

List<Widget> categoriesWidgets;

Child ListView

child: ListView(
                      controller: _scrollController,
                      children: categoriesWidgets),
                ),

if i change this ListView to GridView then both the category title and items are changed to grid.

Get list of widgets for the category

List<Widget> _getCategoriesWidgets(AppLocalizations locale) {
categoriesWidgets = <Widget>[];
final List<MeasuresEnum> favorites = <MeasuresEnum>[];
int prevCount = categoriesWidgets.length;
for (int i = 0; i < MeasureCategoriesEnum.values.length; ++i) {
  final MeasureCategoriesEnum category = MeasureCategoriesEnum.values[i];
  final List<MeasuresEnum> measures = categories[category];
  final String categoryCaption =
      locale.translate(TypeHelpers.getEnumCaption(category));
  final List<MeasuresEnum> subFavorites = _getMeasuresWidgetsForCategory(
      categoryCaption, measures, locale, false);
  if (i != MeasureCategoriesEnum.values.length - 1 &&
      prevCount != categoriesWidgets.length) {
    categoriesWidgets.add(const Divider());
    prevCount = categoriesWidgets.length;
  }

  if (subFavorites.isNotEmpty) {
    favorites.addAll(subFavorites);
  }
}

if (favorites.isNotEmpty) {
  final String caption = locale.translate('Favorites');
  _getMeasuresWidgetsForCategory(caption, favorites, locale, true);
}

return categoriesWidgets;}

Get list of measure elements for the category

List<MeasuresEnum> _getMeasuresWidgetsForCategory(
  String categoryCaption,
  List<MeasuresEnum> measures,
  AppLocalizations locale,
  bool isForFavorite) {
final ThemeData theme = Theme.of(context);
final double textScaleFactor =
    MediaQuery.of(context).size.width > 380 ? 21 : 13;
final TextStyle style = theme.textTheme.bodyText2.copyWith(
    fontSize: DeviceUtils.getScaledText(context, textScaleFactor),
    color: UnitConverterApp.isDarkMode ? Colors.white : Colors.black,
    fontWeight: FontWeight.bold,
);
final List<Widget> result = <Widget>[];
final List<MeasuresEnum> favorites = <MeasuresEnum>[];
for (MeasuresEnum measure in measures) {
  final MeasureInfo info = measuresInfo[measure];

  if (!_filterBySearch(measure, info, locale)) {
    continue;
  }

  result.add(MeasureWidget(measure, onChangeFavoriteState));
  if (info.isFavorite) {
    favorites.add(measure);
  }
}

if (result.isNotEmpty) {
  final Padding categoryTitleWidget = Padding(
      padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
      child: Text(categoryCaption, style: style));
  if (isForFavorite) {
    categoriesWidgets.insert(0, categoryTitleWidget);
    result.add(const Divider());
    categoriesWidgets.insertAll(1, result);
  } else {
    categoriesWidgets.add(categoryTitleWidget);
    categoriesWidgets.addAll(result);
  }
}

return favorites;}

This part in above codes return title and result(items)

else {
    categoriesWidgets.add(categoryTitleWidget);
    categoriesWidgets.addAll(result);
  }
Abu Bakar
  • 67
  • 1
  • 12

0 Answers0