Home > Blockchain >  Listview not getting stacked over a Container
Listview not getting stacked over a Container

Time:09-21

I am trying to position a listview over container but getting error of maxbound width. Please refer my code and help me out.

import 'package:flutter/material.dart';

class TryScreen extends StatelessWidget {
  const TryScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Try Screen'),
      ),
      body: Container(
        height: MediaQuery.of(context).size.height / 1.6,
        child: Stack(
          children: [
            Container(
              color: Colors.lightBlueAccent,
              height: 250,
              width: double.maxFinite,
            ),
            Positioned(
              top: 10,
              child: Container(
                height: 800,
                child: ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemCount: 2,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        color: Colors.blue,
                        height: 250,
                        width: 250,
                      );
                    }),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

I dont understand why is it throwing error and not able to resolve the error on my own. Any help would be appreciated

CodePudding user response:

Try adding width to the Container like this,

...
Positioned(
  top: 10,
  child: Container(
    width: double.infinity,
    height: 800,
    child: ListView.builder(
      scrollDirection: Axis.vertical,
      shrinkWrap: true,
...

CodePudding user response:

you use position.fill hope to solve it

import 'package:flutter/material.dart';

class TryScreen extends StatelessWidget {
  const TryScreen({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Try Screen'),
      ),
      body: Container(
        height: MediaQuery.of(context).size.height / 1.6,
        child: Stack(
          children: [
            Container(
              color: Colors.lightBlueAccent,
              height: 250,
              width: double.maxFinite,
            ),
            Positioned.fill(
              top: 10,
              child: Container(
                height: 800,
                child: ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemCount: 2,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        color: Colors.blue,
                        height: 250,
                        width: 250,
                      );
                    }),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

  • Related