Home > Back-end >  How to Create a Scrollable Page With A ListView
How to Create a Scrollable Page With A ListView

Time:07-27

I have the following but I would like for the Text title to also scroll with the list. How to do this?

Widget build(BuildContext context) {
    return Column(
      children: [
        Text("hello"),
        ListView.builder(
          itemBuilder: (BuildContext context, int index) {
          return UserWidget(
            firstName: userdetails[index]['first_name'],
            lastName: userdetails[index]['last_name'],
            imageURL: userdetails[index]['image_url'],
          );
        },
        itemCount: userdetails.length,
      ),
      ]
    );
}

CodePudding user response:

Create a SingleChildScrollView and add listview and text as children of it

Widget build(BuildContext context) {
    return SingleChildScrollView (
     child :Column(
      children: [
        Text("hello"),
        ListView.builder(
          shrinkWrap: true,
          itemBuilder: (BuildContext context, int index) {
          return UserWidget(
            firstName: userdetails[index]['first_name'],
            lastName: userdetails[index]['last_name'],
            imageURL: userdetails[index]['image_url'],
          );
        },
        itemCount: userdetails.length,
       physics: NeverScrollableScrollPhysics(),
      ),
      ]
    )
  );
}

CodePudding user response:

Wrap your list with Expanded and add shrinkWrap: true, and physics: ScrollPhysics(), under ListView

Scrolling without text title

Column(children: [
        Text("hello"),
        Expanded(
          child: ListView.builder(
            shrinkWrap: true,
            physics: ScrollPhysics(),
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                title: Text("$index"),
              );
            },
            itemCount: 50,
          ),
        ),
      ])

Scrolling with text title

SingleChildScrollView(
      child: Column(children: [
        Text("hello"),
        ListView.builder(
          shrinkWrap: true,
          physics: ScrollPhysics(),
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              title: Text("$index"),
            );
          },
          itemCount: 50,
        ),
      ]),
    )

CodePudding user response:

put your text title inside the UserWidget

  • Related