【问题标题】:Flutter - BoxConstraints forces an infinite heightFlutter - BoxConstraints 强制无限高
【发布时间】:2023-04-09 17:04:02
【问题描述】:

我正在尝试编写一个小型颤振项目,但我遇到了这个错误,不明白为什么。我已经尝试了所有在线解决方案,但仍然出现错误。每次我单击 showCupertinoDialog 中的删除时都会发生这种情况,它会显示此错误而不会导致应用程序崩溃

BoxConstraints forces an infinite height.

These invalid constraints were provided to _RenderColoredBox's layout() function by the following function, which probably computed the invalid constraints in question:
  RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:277:14)
The offending constraints were: BoxConstraints(w=358.0, h=Infinity)

下面是我的代码

class DashboardRestaurant extends StatefulWidget {
  const DashboardRestaurant({Key? key}) : super(key: key);
  static String id = 'dashboard_restaurant';

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

class _DashboardRestaurantState extends State<DashboardRestaurant> {
  final Stream<QuerySnapshot> activities = FirebaseFirestore
      .instance.collection('activities')
      .where('Type', isEqualTo: 'Restaurant')
      .orderBy('ts', descending: true)
      .snapshots();

  bool showSpinner = false;

  @override
  Widget build(BuildContext context) {
    return LoadingOverlay(
      isLoading: showSpinner,
      opacity: 0.5,
      color: Colors.green,
      progressIndicator: const CircularProgressIndicator(
        valueColor: AlwaysStoppedAnimation(Colors.green),
      ),
      child:Column(
                children:[
                  AddButton(
                    title: 'Restaurants',
                    addLabel: 'Add Restaurant',
                    onPressed: (){
                      Navigator.pushNamed(context, RestaurantMainForm.id);
                    },),
                  const SizedBox(
                    height: 16.0,
                  ),
                  const Divider(
                    color: Colors.white70,
                    height:40.0,
                  ),
                  StreamBuilder<QuerySnapshot>(
                    stream: activities,
                    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
                      if(snapshot.connectionState == ConnectionState.waiting){
                        return const Center(
                          child: CircularProgressIndicator(
                            valueColor: AlwaysStoppedAnimation(Colors.green),
                          ),
                        );
                      }
                      final data = snapshot.requireData;
                      if(data != null){
                        return ConstrainedBox(
                          constraints: const BoxConstraints.tightFor(
                              width: 360,
                            height: 250,
                          ),
                          child: ListView.builder(
                              scrollDirection: Axis.vertical,
                              itemCount: data.size,
                              itemBuilder: (context, index){
                                return Column(
                                  children: [
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        CircleAvatar(
                                          backgroundColor: Colors.transparent,
                                          radius: 40,
                                          child:data.docs[index] == null ? Image.asset('assets/images/image.png') : Image.network(
                                            data.docs[index]['PhotoUrl'],
                                            fit: BoxFit.cover,
                                          ) ,
                                        ),
                                        Text( data.docs[index]['Name'], style:const TextStyle(
                                            color: Colors.white,
                                            fontSize: 20,
                                            fontWeight: FontWeight.bold),

                                        ),
                                        if(Responsive.isWeb(context))
                                          const SizedBox(
                                            width: 50,
                                          ),
                                        Responsive.isWeb(context) ? ButtonBlue(
                                          addLabel: 'View Restaurant',
                                          color: Colors.green,
                                          onPressed: (){
                                            Navigator.push(context, MaterialPageRoute(builder: (context){
                                              return ViewRestaurant(
                                                restaurant: data.docs[index],
                                              );
                                            }));
                                          },
                                          icon: const Icon(IconData(61161, fontFamily: 'MaterialIcons')),
                                        ) : InkWell(
                                          onTap: (){
                                            Navigator.push(context, MaterialPageRoute(builder: (context){
                                              return ViewRestaurant(
                                                restaurant: data.docs[index],
                                              );
                                            }));
                                          },
                                          child: const Icon(IconData(61161, fontFamily: 'MaterialIcons')),
                                        ),
                                        Responsive.isWeb(context) ? ButtonBlue(
                                          addLabel: 'Delete Restaurant',
                                          color: Colors.red,
                                          onPressed: () {
                                            showDialog(
                                                context: context,
                                                builder: (BuildContext context){
                                                  return ShowDialog(
                                                    deleteFunction: () async{
                                                      await FirebaseFirestore.instance.runTransaction((Transaction myTransaction) async {
                                                        FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl3']).delete().then((value) {
                                                          FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl2']).delete().then((value){
                                                            FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl']).delete();
                                                          });
                                                        });
                                                        myTransaction.delete(snapshot.data!.docs[index].reference);
                                                      });
                                                    },
                                                    dialogTitle: "Delete",
                                                    dialogContent: "Do you really want to delete ${data.docs[index]['Name']} restaurant?",
                                                  );
                                                });
                                          },
                                          icon: const Icon(Icons.delete_outline,),
                                        ): InkWell(
                                          onTap: (){
                                            if(defaultTargetPlatform == TargetPlatform.iOS){
                                              showCupertinoDialog(
                                                  context: context,
                                                  builder: (BuildContext context){
                                                    return ShowDialog(
                                                      deleteFunction: () async{
                                                        setState(() {
                                                          showSpinner = true;
                                                        });
                                                        await FirebaseFirestore.instance.runTransaction((Transaction myTransaction) async {
                                                          FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl3']).delete().then((value) {
                                                            FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl2']).delete().then((value){
                                                              FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl']).delete();
                                                            });
                                                          });
                                                          myTransaction.delete(snapshot.data!.docs[index].reference);
                                                        }).then((value) {
                                                          setState(() {
                                                            showSpinner = false;
                                                          });
                                                          showToast(message: 'Deleted Successfully', color: Colors.green);
                                                          Navigator.pop(context);
                                                        });
                                                      },
                                                      dialogTitle: "Delete",
                                                      dialogContent: "Do you really want to delete ${data.docs[index]['Name']} restaurant?",
                                                    );
                                                  });
                                            }
                                            else {
                                              showDialog(
                                                  context: context,
                                                  builder: (BuildContext context){
                                                    return ShowDialog(
                                                      deleteFunction: () async{
                                                        setState(() {
                                                          showSpinner = true;
                                                        });
                                                        await FirebaseFirestore.instance.runTransaction((Transaction myTransaction) async {
                                                          FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl3']).delete().then((value) {
                                                            FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl2']).delete().then((value){
                                                              FirebaseStorage.instance.refFromURL(data.docs[index]['PhotoUrl']).delete();
                                                            });
                                                          });
                                                          myTransaction.delete(snapshot.data!.docs[index].reference);
                                                        }).then((value) {
                                                          setState(() {
                                                            showSpinner = false;
                                                          });
                                                          showToast(message: 'Deleted Successfully', color: Colors.green);
                                                          Navigator.pop(context);
                                                        });
                                                      },
                                                      dialogTitle: "Delete",
                                                      dialogContent: "Do you really want to delete ${data.docs[index]['Name']} restaurant?",
                                                    );
                                                  });
                                            }
                                          },
                                          child:  const Icon(Icons.delete_outline,),
                                        ),
                                      ],
                                    ),
                                    const Divider(
                                      color: Colors.white70,
                                      height:40.0,
                                    ),
                                  ],
                                );
                              }),
                        );
                      }
                      return const Center(
                        child: CircularProgressIndicator(),
                      );
                    },
                  )
                ],
      ),
    );
  }
}

我已经尝试了所有在线解决方案,但没有什么真正适合我。 谁能帮帮我

【问题讨论】:

  • 您可以尝试在 listview.builder 中添加shrinkWrap: true 看看是否可以解决问题
  • 我有它但仍然有错误
  • 尝试使用ExpandedFlexible 添加您的Inside Row 小部件,参考我的答案hereherehere 希望对您有所帮助
  • 如果 parentDataWidget 使用不正确
  • 如何申报你?这种格式Row(children:[Expanded(Text('OK'),),],),

标签: flutter


【解决方案1】:

试试下面的格式化代码,希望对你有所帮助。添加你的 Inside Column 小部件用 ExpandedFlexible 包装它

 Column(
  mainAxisSize: MainAxisSize.min,
      children:  [
        Expanded(
          child: Container(
            width: 100.0,
            color: Colors.blue,
            child: Text('Hello Flutter'),
          ),
        ),
      ],
    ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2020-12-22
    • 2020-04-27
    • 2019-11-04
    • 2019-12-02
    相关资源
    最近更新 更多