Prototype
Prototype

Reputation: 568

How to add vertical and horizontal list in single list

I want to add vertical and horizontal list in single list but problem is when i want to put horizontal and vertical list in single list i have give height to container but for vertical list i want to scroll in full screen but it is not possible i have to put height to vertical list also please help me.

class mydynamic extends StatefulWidget {
@override
_mydynamicState createState() => _mydynamicState();
}

class _mydynamicState extends State<mydynamic> {

 List<String> my_text = [
"Hello",
"World",
"dasdasd",
"asdasdasd",
"sadasdasda"
 ];

 @override
 void dispose() {
 super.dispose();
 }

 @override
 Widget build(BuildContext context) {
 return Scaffold(
  appBar: AppBar(
    title: Text(
      "Videolist",
      style: TextStyle(color: Colors.white),
    ),
  ),
  body:ListView(

      children: <Widget>[

        _horizantal_list(),
        _vertical_list()
      ],

  ),
);
}



Widget _horizantal_list()
 {

  return Container(

    height:50.0,
    child: ListView.builder(
      
      itemCount: my_text.length,
      scrollDirection: Axis.horizontal,
      itemBuilder:(context,index){
        
          return Card(

            child: Text(my_text[index]),
          );
          

      }),
  );

}

 Widget _vertical_list()
  {

  return Container(
      height: 150,
    child:  ListView.builder(

        itemCount: my_text.length,
        itemBuilder: (context, index){

            return Card(
                
                child:Text(my_text[index]),
            );

        },

    ),
  );
      

}
}

please any solution for this Thank you

Upvotes: 0

Views: 381

Answers (2)

Prototype
Prototype

Reputation: 568

Here i updated my code

 class mydynamic extends StatefulWidget {
 @override
 _mydynamicState createState() => _mydynamicState();
}

class _mydynamicState extends State<mydynamic> {

List<String> my_text = [
"Hello",
"World",
"dasdasd",
"asdasdasd",
"sadasdasda",

  "Hello",
"World",
"dasdasd",
"asdasdasd",
"sadasdasda",
  "Hello",
"World",
"dasdasd",
"asdasdasd",
"sadasdasda",
  "Hello",
"World",
"dasdasd",
"asdasdasd",
"sadasdasda",
  "Hello",
"World",
"dasdasd",
"asdasdasd",
"sadasdasda",

];

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text(
      "Videolist",
      style: TextStyle(color: Colors.white),
    ),
  ),
  body: CustomScrollView(

     slivers: <Widget>[

       SliverList(delegate:SliverChildListDelegate([

          Column(

            children: <Widget>[

               _vertical_list(),
               _horizontal_list(),
                _vertical_list(),
             
            ],
          )

       ]))

     ],

  ),
 
);
 }



Widget _vertical_list()
{
  return Column(

  children: <Widget>[

    ListView.builder(
      scrollDirection: Axis.vertical,
      physics: ClampingScrollPhysics(),
      shrinkWrap: true,
      itemCount: my_text.length,
      itemBuilder: (context,index){

          return Card(child:Text(my_text[index]));


    })
  ],

 );
}

Widget _horizontal_list()
{


  return Container(
  height:50.0,
  child: ListView.builder(
   shrinkWrap: true,
    scrollDirection: Axis.horizontal,
   itemCount: my_text.length,
   itemBuilder:(context,index){
    
    return Card(

      child: Center(child: Container(child: Text(my_text[index]))),
    );

 }),
);
}

}

Upvotes: 0

chunhunghan
chunhunghan

Reputation: 54407

You can copy paste run full code below
Step 1: body use Column
Step 2: _vertical_list use Expanded

 Widget _vertical_list() {
    return Expanded(
      child: ListView.builder(

working demo

enter image description here

full code

import 'package:flutter/material.dart';

class mydynamic extends StatefulWidget {
  @override
  _mydynamicState createState() => _mydynamicState();
}

class _mydynamicState extends State<mydynamic> {
  List<String> my_text = [
    "Hello",
    "World",
    "dasdasd",
    "asdasdasd",
    "sadasdasda",
    "Hello",
    "World",
    "dasdasd",
    "asdasdasd",
    "sadasdasda",
    "Hello",
    "World",
    "dasdasd",
    "asdasdasd",
    "sadasdasda",
    "Hello",
    "World",
    "dasdasd",
    "asdasdasd",
    "sadasdasda",
    "Hello",
    "World",
    "dasdasd",
    "asdasdasd",
    "sadasdasda",
    "Hello",
    "World",
    "dasdasd",
    "asdasdasd",
    "sadasdasda",
  ];

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Videolist",
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Column(
        children: <Widget>[_horizantal_list(), _vertical_list()],
      ),
    );
  }

  Widget _horizantal_list() {
    return Container(
      height: 50.0,
      child: ListView.builder(
          itemCount: my_text.length,
          scrollDirection: Axis.horizontal,
          itemBuilder: (context, index) {
            return Card(
              child: Text(my_text[index]),
            );
          }),
    );
  }

  Widget _vertical_list() {
    return Expanded(
      child: ListView.builder(
        itemCount: my_text.length,
        itemBuilder: (context, index) {
          return Card(
            child: Text(my_text[index]),
          );
        },
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: mydynamic(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Upvotes: 5

Related Questions