Home > Back-end >  Can't view first item on PageView
Can't view first item on PageView

Time:01-11

I am trying to create PageView related to my object but when i load,the first item does not show. It is blank like in the screenshot. Print function works fine ,it prints the existing item but i can't view it .

Print Result item : itemfirst

 Scaffold(
        backgroundColor: colordtmainone,
        body: NestedScrollView(
          controller: _scrollController,
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
            SliverAppBar(
                floating: true,
                elevation: 0,
                backgroundColor: colordtmainone,
                toolbarHeight: 0,
                expandedHeight: 0,
                forceElevated: innerBoxIsScrolled,
              ),
            ];
          },
          body:  PageView.builder(
            onPageChanged: (indexpage){
              if (indexpage   1 == _userservicesForDisplay.length) {
                page = page  1;
                _loadmoreuserservices();
              }
            },
            scrollDirection: Axis.vertical,
            physics: ClampingScrollPhysics(),
            itemBuilder: (context, index) {
         for(final item in _userservicesForDisplay)print('item: ${item.service}');
              return index == 0 ? SizedBox(height:0,width:0) : _listItemUserService(index-1);},
            itemCount: _userservicesForDisplay.length 1,
          ),
        ),
      )

enter image description here

CodePudding user response:

Here your are skipping the first element. If index = 0, it returns a SizedBox(height:0,width:0).

return index == 0 ? SizedBox(height:0,width:0) : _listItemUserService(index-1);},

So you can

itemBuilder: (_, index) {
   return _listItemUserService(index);
},
itemCount: _userservicesForDisplay.length,

CodePudding user response:

you can use this code for your reference hope this will work for you thanks


import 'package:flutter/material.dart';
import 'package:traveling/helpers/AppColors.dart';
import 'package:traveling/screens/Employee/home/Home.dart';
import 'package:traveling/screens/Employee/profile/Profile.dart';
import 'package:traveling/screens/Employee/setting/Setting.dart';



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

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

class _EmployeeBottomNavBarState extends State<EmployeeBottomNavBar> {
  int pageIndex = 0;
  bool visible = true;

  List<Widget> pageList = <Widget>[EmployeeHome(), EmployeeProfile(leadingIcon: false,), Setting()];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: pageList[pageIndex],
        bottomNavigationBar: BottomNavigationBar(
            fixedColor: Colors.redAccent[400],
            currentIndex: pageIndex,
            onTap: (value) {
              setState(() {
                pageIndex = value;
              });
            },
            // type: BottomNavigationBarType.fixed,
            items: [
              BottomNavigationBarItem(

                  activeIcon: Container(
                    height: 40,
                    width: 80,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Color(0xff2161c0),
                    ),
                    child: Icon(
                      Icons.home,
                      color: AppColors.white,
                    ),
                  ),
                  icon: Icon(
                    Icons.home,
                    color: Color(0xff2161c0),
                  ),
                  label: ""),
              BottomNavigationBarItem(
                  activeIcon: Container(
                    height: 40,
                    width: 80,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Color(0xff2161c0),
                    ),
                    child: Icon(
                      Icons.person,
                      color: AppColors.white,
                    ),
                  ),
                  icon: Icon(
                    Icons.person,
                    color: AppColors.baseLightBlueColor,
                  ),
                  label: ""),
              BottomNavigationBarItem(
                  activeIcon: Container(
                    height: 40,
                    width: 80,
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: AppColors.baseLightBlueColor,
                    ),
                    child: Icon(
                      Icons.settings,
                      color: AppColors.white,
                    ),
                  ),
                  icon: Icon(
                    Icons.settings,
                    color: AppColors.baseLightBlueColor,
                  ),
                  label: ""),
            ]
        )
    );
  }
}
  •  Tags:  
  • Related