0

CupertinoActionSheet在我的有状态小部件上实现时调用时出现以下错误SingleChildScrollView

_RenderCupertinoAlertActions 对象在布局期间被赋予了无限大小。相关的导致错误的小部件是 CupertinoActionSheet lib\main.dart:82 引发异常时正在处理以下 RenderObject:_RenderCupertinoAlertActions#56898 relayoutBoundary=up19 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE RenderObject: _RenderCupertinoAlertActions# 56898 relayoutBoundary=up19 需要-布局需要-油漆需要-合成-位-更新约束:BoxConstraints(w=395.4, 0.0<=h<=Infinity) 大小:Size(395.4, Infinity) 子 1:RenderPointerListener#7df6e relayoutBoundary=up20 NEEDS-PAINT parentData: offset=Offset(0.0, 0.0); id=null(可以使用大小)约束:BoxConstraints(w=395.4, 0.0<=h<=Infinity) size: Size(395.4, Infinity) 行为:

完整代码附在此处(根据以下评论进行编辑以解决无限大小错误):

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() => runApp(MyApp());

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

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {

  List<String> leaveTypes = ["Vacation Leave","Ordinary Sick Leave","Childcare Leave"];
  String applyLeaveType;

  @override
  Widget build(BuildContext context) {
    if(applyLeaveType == null){
      applyLeaveType = leaveTypes[0];
    };
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: LayoutBuilder(builder:(context, constraints){
        return SingleChildScrollView(
          /*child: ConstrainedBox(
            constraints: BoxConstraints(
              minWidth: constraints.maxWidth, minHeight: constraints.maxHeight
            ),*/
            child: Container(
              height: constraints.maxHeight,
              color: Color.fromARGB(255, 244, 246, 249),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  //Text for leave type
                  Container(                            
                    alignment: Alignment(-1.0,0.0),
                    padding:EdgeInsets.only(
                      top: 10, bottom: 5, left: 20, right: 20
                    ),
                    child: Text("I want to apply for",
                      style: TextStyle(fontSize: 16),
                      textAlign: TextAlign.left,
                    )
                  ),
                  SizedBox(
                    width: MediaQuery.of(context).size.width,
                    height:80,
                    child: GestureDetector(
                      onTap:() {
                        buildLeaveList(){
                          List<Widget> widgets = List();
                          leaveTypes.forEach((leaveType){
                            print(leaveType);
                            widgets.add(
                              CupertinoActionSheetAction(
                                child: Text(leaveType),
                                onPressed: () {
                                  setState(() {
                                    applyLeaveType = leaveType;
                                  });
                                  Navigator.pop(context);
                                }
                              )
                            );

                          });
                          return widgets;
                        }
                        final action = Container(
                          height: MediaQuery.of(context).size.height,
                          child: CupertinoActionSheet(

                            title: Text(
                              "Leave Type",
                              style: TextStyle(fontSize:16)
                            ),
                            actions: buildLeaveList(),
                            cancelButton: CupertinoActionSheetAction(
                              child: Text("Back"),
                              onPressed:(){
                                Navigator.pop(context);
                              }
                            )
                          )
                        );
                        showCupertinoModalPopup(context: context, builder: (context) => action);
                      },
                      child: Container(
                        alignment: Alignment(0.0,0.0),
                        padding:EdgeInsets.only(
                          top: 10, bottom: 10, left: 20, right: 20
                        ),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          //border: Border.all(),
                          //borderRadius: BorderRadius.all(Radius.circular(10))
                        ),                     
                        child:Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Container(
                              padding: EdgeInsets.all(10),
                              child: Text(applyLeaveType),
                            ),
                            Container(
                              padding: EdgeInsets.all(10),
                              child: Icon(Icons.keyboard_arrow_down)
                            )
                          ],
                        )
                      )
                    ),
                  )
                ]
              )
            )
          //)
        );  
      })  
    );
  }
}
4

1 回答 1

0

问题是,如果您使用任何列表,则它需要其父小部件的特定高度,列表视图可以在其中构建。

如果您将 ListView 与 ShrinkWrap: true 和其他一些小部件一起使用,它将构建,但底页的高度变为整个屏幕的高度。

最终解决方案:我们可以在单独的方法中创建一个变量并将所有项目添加到其中,最后调用该方法。

以下代码可以帮助您更好地理解。

创建以下方法:

 callme() {
    List<Widget> mywidget = List();
    leaveTypes.forEach((e) => mywidget.add(CupertinoActionSheetAction(
        child: Text(e),
        onPressed: () {
          setState(() {
            applyLeaveType = e;
          });
          Navigator.pop(context);
        })));
    return mywidget;
  }

按以下方式调用此方法。

actions: callme()

更新:

工作演示:

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  List<String> leaveTypes = [
    "Vacation Leave",
    "Ordinary Sick Leave",
    "Childcare Leave"
  ];
  String applyLeaveType;
  callme() {
    List<Widget> mywidget = List();
    leaveTypes.forEach((e) => mywidget.add(CupertinoActionSheetAction(
        child: Text(e),
        onPressed: () {
          setState(() {
            applyLeaveType = e;
          });
          Navigator.pop(context);
        })));
    return mywidget;
  }

  @override
  Widget build(BuildContext context) {
    if (applyLeaveType == null) {
      applyLeaveType = leaveTypes[0];
    }

    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: LayoutBuilder(builder: (context, constraints) {
          return SingleChildScrollView(
              child: Container(
                  height: constraints.maxHeight,
                  color: Color.fromARGB(255, 244, 246, 249),
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                            alignment: Alignment(-1.0, 0.0),
                            padding: EdgeInsets.only(
                                top: 10, bottom: 5, left: 20, right: 20),
                            child: Text(
                              "I want to apply for",
                              style: TextStyle(fontSize: 16),
                              textAlign: TextAlign.left,
                            )),
                        SizedBox(
                          width: MediaQuery.of(context).size.width,
                          height: 80,
                          child: GestureDetector(
                              onTap: () {
                                final action = Container(
                                    child: CupertinoActionSheet(
                                        title: Text("Leave Type",
                                            style: TextStyle(fontSize: 16)),
                                        actions: callme(),
                                        cancelButton:
                                            CupertinoActionSheetAction(
                                                child: Text("Back"),
                                                onPressed: () {
                                                  Navigator.pop(context);
                                                })));
                                showCupertinoModalPopup(
                                    context: context,
                                    builder: (context) => action);
                              },
                              child: Container(
                                  alignment: Alignment(0.0, 0.0),
                                  padding: EdgeInsets.only(
                                      top: 10, bottom: 10, left: 20, right: 20),
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                  ),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Container(
                                        padding: EdgeInsets.all(10),
                                        child: Text(applyLeaveType),
                                      ),
                                      Container(
                                          padding: EdgeInsets.all(10),
                                          child:
                                              Icon(Icons.keyboard_arrow_down))
                                    ],
                                  ))),
                        )
                      ])));
        }));
  }
}
于 2020-05-02T08:18:18.543 回答