Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

widget - How can i make pageView.Builder occupy all available space in flutter?

I am trying to swap two widgets on button click OR is there any other way to do this?

When i set SizedBox(
height: double.infinity,
width: double.infinity, )
it gives error: BoxConstraints forces an infinite height. it works when manually set height and width.
I tried using
Expanded and flexible it gives error like this

RenderBox was not laid out: RenderRepaintBoundary#8b1a5 relayoutBoundary=up14 NEEDS-PAINT
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1785 pos 12: 'hasSize'

Code

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _uri = TextEditingController();
  bool flag=false;

  bool swap = false;
   int _index = 0;

  @override
  void initState() {
    swap = widget.swap;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {


    Widget swapWidget;
    if (!swap) {
      swapWidget =  Center(
        child: Expanded(
        // card height
          child: PageView.builder(
            itemCount: 2,
            controller: PageController(viewportFraction: 0.95),
            onPageChanged: (int index) => setState(() => _index = index),
            itemBuilder: (_, i) {
              return Transform.scale(
                scale: i == _index ? 1 : 0.9,
                child: Card(
                  elevation: 6,
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                  child: Center(
                    child: Text(
                      "Card ${i + 1}",
                      style: TextStyle(fontSize: 32),
                    ),
                  ),
                ),
              );
            },
          
        
        )
        )
      );
    } else {
      swapWidget =new Card(
        child:Text('Nothing To show')
      );
    }

    var swapTile = new ListTile(
      title: swapWidget,
    );


    return new Scaffold(
      appBar: AppBar( title: Text('My APP')),
      body: new ListView(
          children: <Widget>[
            swapTile,
          ],
      ),
    );
  }

}

What i am doing Wrong? Thankyou


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not entirely sure what you are trying to do here, but Few things. PageView widget expands to the maximum allowed space by default, so you don't need the Expanded or Center widget around it.

Second, I don't know why you would want to wrap Card widget with ListTile. You certainly can, but that's not how ListTile and Card are designed.

Third, because PageView widget can expand, it needs some size constraints, so it cannot be put inside a infinitely large widget like ListView, so you can remove that, and here is what I have. I hope it is close to what you were looking for.

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool swap = true;
  int _index = 0;

  @override
  Widget build(BuildContext context) {
    Widget swapWidget;
    if (swap) {
      swapWidget = PageView.builder(
        itemCount: 2,
        controller: PageController(viewportFraction: 0.95),
        itemBuilder: (_, i) {
          return Transform.scale(
            scale: i == _index ? 1 : 0.9,
            child: Card(
              elevation: 6,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20)),
              child: Center(
                child: Text(
                  "Card ${i + 1}",
                  style: TextStyle(fontSize: 32),
                ),
              ),
            ),
          );
        },
      );
    } else {
      swapWidget = Card(child: Text('Nothing To show'));
    }

    return Scaffold(
      body: swapWidget,
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...