Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem SingleChildScrollView with PageView flutter

I want to add SingleChildScrollView in my page but i have PageView.

 @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new IneatAppBarPollWidget()
          .getAppBar('Blackblox', 'assets/img/logo_ineat.png', cand),
      body: new Center(
        child: SingleChildScrollView(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              new Expanded(
                child: new Container(
                  child: new FutureBuilder<List<PollQuestionsDto>>(
                      future: _future,
                      // ignore: missing_return
                      builder: (context, snapshot) {
                        switch (snapshot.connectionState) {
                          case ConnectionState.none:
                            return Text('Failed connection API');
                          case ConnectionState.waiting:
                            return new Text('Wait...');
                          case ConnectionState.done:
                            if (snapshot.hasData) {
                              if (snapshot.data != null) {
                                return PageView.builder(
                                  scrollDirection: Axis.horizontal,
                                  itemCount: snapshot.data.length,
                                  itemBuilder: (BuildContext context, index) {
                                    print(this.responses);
                                    return PollListViewItem(
                                      entitled: 
                                      snapshot.data[index].entitled,
                                      answers: 
                                      snapshot.data[index].answers,
                                      currentAnswer: 
                                      this.responses[index],
                                      onSelect: (Answer answer) {
                                        this.responses[index] = answer;
                                      },
                                    );
                                  },
                                );
                              }
                            }
                            break;
                          case ConnectionState.active:
                        }
                      }),
                ),
              ),
              new ProgressBar(),
              new Container(
                margin: EdgeInsets.only(right: 10.0),
                child: new RaisedButton.icon(
                  onPressed: () async {
                    //await Poll().submitPoll(answers: answers);
                    setState(() {
                      Navigator.pushReplacement(context,
                          MaterialPageRoute(builder: (context) => 
                          PollPage()));
                    });
                  },
                  label: Text('Suivant'),
                  icon: Icon(Icons.navigate_next),
                ),
                alignment: Alignment.bottomRight,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

When I added this, I have this error :

RenderFlex children have non-zero flex but incoming height constraints are unbounded.

When a column is in a parent that does not provide a finite height constraint, for example if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the vertical direction. These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent. Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded). This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.

RenderBox was not laid out: RenderFlex#4c442 relayoutBoundary=up12 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': Failed assertion: line 1681 pos 12: 'hasSize'

like image 279
MayuriXx Avatar asked Oct 14 '19 08:10

MayuriXx


1 Answers

I recreated your case with a sample data. Below are the issues that I fixed:

  1. Column used inside SingleChildScrollView expands to fill vertical space by default if we don't set the mainAxisSize for it. So here, you will need to add mainAxisSize: MainAxisSize.min, which tells column to take only the minimum space.

  2. The children to be displayed inside Column were wrapped in Expanded which again takes up default space for its child. Here, instead of Expanded, use Flexible which takes the space as applicable to its child.

  3. The Container used to display the data from future builder again expands to the entire screen if not provided a height. So just provide a custom height to the Container.

Working code below:

body: Center(
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Flexible(
                  child: Container(
                    height: 200,
                    child: FutureBuilder<Post>(
                      future: post,
                      builder: (context, snapshot) {
                        switch (snapshot.connectionState) {
                          case ConnectionState.none:
                            return Text('Failed connection API');
                          case ConnectionState.waiting:
                            return new Text('Wait...');
                          case ConnectionState.done:
                            if (snapshot.hasData) {
                              if (snapshot.data != null) {
                                return PageView.builder(
                                  scrollDirection: Axis.horizontal,
                                  itemCount: 5,
                                  itemBuilder: (BuildContext context, index) {
                                    return Card(
                                      child: Text('test'),
                                    );
                                  },
                                );
                              }
                            }
                            break;
                          case ConnectionState.active:
                        }

                        // By default, show a loading spinner.
                        return CircularProgressIndicator();
                      },
                    ),
                  )
                ),
                Container(
                  margin: EdgeInsets.only(right: 10.0),
                  child: new RaisedButton.icon(
                    onPressed: () {},
                    label: Text('Suivant'),
                    icon: Icon(Icons.navigate_next),
                ),
                  alignment: Alignment.bottomRight,
                )
              ],
            ),
          )

        ),

Based on the sample data I used, I was able to see below result and was able to scroll horizontally to see the data properly.

enter image description here

like image 65
Darshan Avatar answered Oct 08 '22 12:10

Darshan