Home > Blockchain >  Make Listeview work nested inside of column inside of a container in flutter
Make Listeview work nested inside of column inside of a container in flutter

Time:07-24

I am trying and make a Listeview work, which is nested inside of column that is nested inside of a container in flutter. The container is supposed to be a dialog. I think the problem is that the container has no defined hight (it is supposed to adapt to the screen size). With the current code I get a bottom overflow. Maybe because of the padding of the container? I tried differen variants with expanded, flexible and singlechildscrollview but I can't make it work. The standard tip, wrap the ListView with Expanded seems not to work. Thanks for your help!

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dialog',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Column(
            children: [
              MaCard(mitarbeiterName: "Name"),
              CustomDialogBoxRow(
                  title: "Sturmtruppler",
                  descriptions: "wird noch weichen",
                  text: "text")
            ],
          ),
        ),
      ),
    );
  }
}

class Constants {
  Constants._();
  static const double padding = 20;
  static const double avatarRadius = 100;
  static const double buttonHight = 100;
}

class CustomDialogBoxRow extends StatefulWidget {
  final String title, descriptions, text;

  const CustomDialogBoxRow({
    Key? key,
    required this.title,
    required this.descriptions,
    required this.text,
  }) : super(key: key);

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

class _CustomDialogBoxRowState extends State<CustomDialogBoxRow> {
  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(Constants.padding),
      ),
      elevation: 0,
      backgroundColor: Colors.transparent,
      child: contentBox(context),
    );
  }

  contentBox(context) {
    return Stack(
      children: <Widget>[
        Container(
          constraints: const BoxConstraints(minWidth: 400, maxWidth: 800),
          padding: const EdgeInsets.only(
              left: Constants.padding,
              right: Constants.padding,
              bottom: Constants.padding),
          margin: const EdgeInsets.only(top: Constants.avatarRadius),
          decoration: BoxDecoration(
              shape: BoxShape.rectangle,
              color: Colors.white,
              borderRadius: BorderRadius.circular(Constants.padding),
              boxShadow: [
                const BoxShadow(
                    color: const Color.fromARGB(255, 79, 73, 73),
                    offset: const Offset(0, 5),
                    blurRadius: 5),
              ]),
          child: Column(
            children: [
              SizedBox(
                height: Constants.avatarRadius,
                child: Row(
                  children: [
                    const SizedBox(
                      child: const Placeholder(),
                    ),
                    const Expanded(
                      child: Placeholder(),
                    ),
                  ],
                ),
              ),
              SingleChildScrollView(
                child: Column(
                  children: [
                    SizedBox(
                      height: 50,
                      child: Text("bla"),
                    ),
                    SizedBox(
                      height: 50,
                      child: Text("bla"),
                    ),
                    SizedBox(
                      height: 50,
                      child: Text("bla"),
                    ),
                    SizedBox(
                      height: 50,
                      child: Text("bla"),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
        Positioned(
          left: Constants.padding,
          right: Constants.padding,
          child: Stack(
            children: [
              Container(
                child: Align(
                  alignment: Alignment.topLeft,
                  child: Container(
                    width: Constants.avatarRadius * 2,
                    height: Constants.avatarRadius * 2,
                    child: const CircleAvatar(
                      radius: Constants.avatarRadius * 2,
                      backgroundImage: AssetImage('assets/images/SBE.jpg'),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

class MaCard extends StatefulWidget {
  MaCard({
    Key? key,
    required this.mitarbeiterName,
  }) : super(key: key);

  final String mitarbeiterName;

  @override
  State<MaCard> createState() => _MaCardState();
}

class _MaCardState extends State<MaCard> {
  @override
  Widget build(BuildContext context) {
    return Card(
        child: InkWell(
      onTap: () {
        print("card taped");
        /*showDialog(context: context, builder: (BuildContext context) {
          return
        })*/

        showDialog(
            context: context,
            builder: (BuildContext context) {
              return CustomDialogBoxRow(
                title: "Stormtrouper",
                descriptions: "Jojo, this is card",
                text: "Roger Roger",
              );
            });
      },
      child: SizedBox(
        height: Constants.buttonHight,
        width: 300,
        child: Center(child: Text(widget.mitarbeiterName)),
      ),
    ));
  }
}

Here is picture of what it should look like. My wonderful handdrawing is supposed to be the scrollable content. Goal

CodePudding user response:

It would be better using LayoutBuilder to get parent constraints and size the inner elements. Also You need to wrap with Expaned to get available spaces for infinite size widget.

I will highly recommend to check this video from Flutter.

Changes are made

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dialog',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Column(
            children: [
              MaCard(mitarbeiterName: "Name"),
              Expanded(  //here
                child: CustomDialogBoxRow(
                    title: "Sturmtruppler",
                    descriptions: "wird noch weichen",
                    text: "text"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

And on contentBox

  contentBox(context) {
    return LayoutBuilder(builder: (context, constraints) {
      print(constraints);
      return SizedBox(
        width: constraints.maxWidth,
        height: constraints.maxHeight,
        child: Stack(
          children: <Widget>[
            Container(
              constraints: const BoxConstraints(minWidth: 400, maxWidth: 800),
              padding: const EdgeInsets.only(
                  left: Constants.padding,
                  right: Constants.padding,
                  bottom: Constants.padding),
              margin: const EdgeInsets.only(top: Constants.avatarRadius),
              decoration: BoxDecoration(
                  shape: BoxShape.rectangle,
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(Constants.padding),
                  boxShadow: const [
                    BoxShadow(
                        color: Color.fromARGB(255, 79, 73, 73),
                        offset: Offset(0, 5),
                        blurRadius: 5),
                  ]),
              child: Column(
                children: [
                  SizedBox(
                    height: Constants.avatarRadius,
                    child: Row(
                      children: [
                        const Expanded(
                          child: const Placeholder(),
                        ),
                        const Expanded(
                          child: Placeholder(),
                        ),
                      ],
                    ),
                  ),
                  Expanded(
                    // or sizedBOx ( constraints.maxHeight- Constants.avatarRadius,)
                    child: SingleChildScrollView(
                      child: Column(
                        children: [
                          SizedBox(
                            height: 450,
                            child: Text("bla"),
                          ),
                          SizedBox(
                            height: 50,
                            child: Text("bla"),
                          ),
                          SizedBox(
                            height: 50,
                            child: Text("bla"),
                          ),
                          SizedBox(
                            height: 50,
                            child: Text("bla"),
                          )
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
            //     Positioned(
            //       left: Constants.padding,
            //       right: Constants.padding,
            //       child: Stack(
            //         children: [
            //           Container(
            //             child: Align(
            //               alignment: Alignment.topLeft,
            //               child: Container(
            //                 width: Constants.avatarRadius * 2,
            //                 height: Constants.avatarRadius * 2,
            //                 child: const CircleAvatar(
            //                   radius: Constants.avatarRadius * 2,
            //                   backgroundImage: AssetImage('assets/images/SBE.jpg'),
            //                 ),
            //               ),
            //             ),
            //           ),
            //         ],
            //       ),
            //     ),
            //
          ],
        ),
      );
    });
  }
  • Related