我如何使用媒体查询来解决以下问题

问题描述

以下颤动代码在6英寸屏幕尺寸上可以完美运行,但只要它在小于6英寸的设备上运行,它就会显示XYZ像素溢出。.我该如何解决此问题,或者是否有任何小部件来解决此问题。 我已经通过使用Media Query将容器的宽度添加为屏幕的宽度,但在较小的设备上其显示仍然溢出

这是我的代码

import 'package:Flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:Flutter/services.dart';
import 'package:kartofill/Homepagewithprice/Homepagewithprice.dart';
import 'package:kartofill/Navigatorpage.dart';
import 'package:Fluttertoast/Fluttertoast.dart';


dynamic totalCartValue =0;

class Cartpage extends StatefulWidget {

  @override
  _CartpageState createState() => _CartpageState();

}


class _CartpageState extends State<Cartpage> {

  String phoneNumber;
  final myController = TextEditingController();  ///Alert Dialog Box input text myController will be use to store the number of qty
  String id;
  var qty;
  var price;
  var total;
  bool qtyUpdated= false;

  Future<String> getCartTotal() async {
    totalCartValue = 0;

    final fireBase = await FirebaseAuth.instance.currentUser();
    print(fireBase.phoneNumber);
    QuerySnapshot snapshot = await Firestore.instance
        .collection('users').document(fireBase.phoneNumber)
    .collection('cart').getDocuments();



    snapshot.documents.forEach((doc) {

      print(doc.data['total'].toString());
      setState((){

        totalCartValue += doc.data['total'];
      });
    });
    return totalCartValue.toString();
  }


  @override
  void initState() {
    // Todo: implement initState
    super.initState();
    getCurrentUser();
    print("Hi $phoneNumber");



    getCartTotal();
    print("Hi $totalCartValue");

  }
  getCurrentUser() async{

    final firebaseUser = await FirebaseAuth.instance.currentUser();
    setState(() {
      phoneNumber = firebaseUser.phoneNumber;
    });


  }




  @override
  Widget build(BuildContext context) {

    return MaterialApp(
     debugShowCheckedModeBanner: false,home:Scaffold(
          bottomNavigationBar:
          StreamBuilder(
              stream: Firestore.instance.collection('users').document(phoneNumber)
                  .collection('cart').snapshots(),builder: (context,snapshot) {
                if(snapshot.hasData) {
                  if(snapshot.data.documents.length!=0){
                  return new Container( //// Bottom Naviagtion Bar for check out and Total price
                    color: Colors.white,child: Row(
                      children: <Widget>[
                        Checkoutclass(),Expanded(
                          child: ListTile(
                            title: Text('Total'),subtitle: Text(totalCartValue.toStringAsFixed(2)),),)
                      ],);}
                  else return Container(
                    height: 100,);
                }
                else{
                  return Text('Loading');
                }
            }
          ),appBar: AppBar(
            leading: IconButton(icon: Icon(Icons.arrow_back),onpressed: (){

                  Navigator.pop(context);

                }),title: Text('Kart-Ofill'),backgroundColor: Colors.red,body: Container(
            width: MediaQuery.of(context).size.width,child: StreamBuilder(
              stream: Firestore.instance.collection('users').document(phoneNumber)
                  .collection('cart').snapshots(),snapshot){
            if(snapshot.hasData){
              if(snapshot.data.documents.length!=0) {
                return ListView.builder(
                  itemCount: snapshot.data.documents.length,itemBuilder: (context,index) {
                    DocumentSnapshot doc = snapshot.data.documents[index];

                    return Container(
                      height: 200,child: Card(

                        child: Row(
                          children: <Widget>[
                            Card(child: Image.network(
                              doc['img'],height: 100,width: 100,elevation: 10,Column(
                              children: <Widget>[
                                Container(
                                    height: MediaQuery
                                        .of(context)
                                        .size
                                        .height / 15,width: MediaQuery
                                        .of(context)
                                        .size
                                        .width / 1.5,color: Colors.white,child: Text(doc['description'] + "Rs " +
                                        doc['price'].toStringAsFixed(2),style: TextStyle(fontSize: 15),)),Center(child: Text(
                                    'Rs ' + doc['total'].toStringAsFixed(2))),Row(
                                  children: <Widget>[
                                    RaisedButton(
                                      color: Colors.grey,onpressed: () {
                                        showDialog(context: context,barrierdismissible: false,builder: (BuildContext context) {
                                              return Dialog(

                                                child: Container(
                                                  height: 250,color: Colors.white10,child: Container(
                                                    margin: EdgeInsets.all(40.0),child: Column(
                                                      children: <Widget>[
                                                        TextField(
                                                          inputFormatters: [
                                                            new BlacklistingTextInputFormatter(
                                                                new RegExp(
                                                                    '[\\.|\\,|\\-|\\ ]'))
                                                          ],controller: myController,keyboardType: TextInputType
                                                              .number,decoration: Inputdecoration(
                                                              hintText: 'Enter the Quantity'),Container(
                                                          height: 50,Row(
                                                          children: <Widget>[
                                                            Padding(
                                                              padding: const EdgeInsets
                                                                  .all(8.0),child: RaisedButton(

                                                                child: Text(
                                                                  'Close',style: TextStyle(
                                                                      color: Colors
                                                                          .white),color: Colors.red,onpressed: () {
                                                                  Navigator.pop(
                                                                      context);
                                                                  myController
                                                                      .clear();
                                                                },SizedBox(width: 25,RaisedButton(

                                                              color: Colors.red,child: Text(
                                                                'Submit',style: TextStyle(
                                                                    color: Colors
                                                                        .white),onpressed: () async {
                                                                qty = myController
                                                                    .text;


                                                                if (int.parse(
                                                                    qty) > 0 &&
                                                                    int.parse(
                                                                        qty) >=
                                                                        5 &&
                                                                    int.parse(
                                                                        qty) <=
                                                                        25) {
                                                                  setState(() {
                                                                    qtyUpdated =
                                                                    true;
                                                                    totalCartValue =
                                                                    0;
                                                                  });
                                                                  CollectionReference col = Firestore
                                                                      .instance
                                                                      .collection(
                                                                      'users')
                                                                      .document(
                                                                      phoneNumber)
                                                                      .collection(
                                                                      'cart');


                                                                  QuerySnapshot querySnapshot = await col
                                                                      .getDocuments();
                                                                  querySnapshot
                                                                      .documents[index]
                                                                      .reference
                                                                      .updateData(
                                                                      {
                                                                        "quantity": qty
                                                                      });

                                                                  price =
                                                                  doc['price'];
                                                                  total =
                                                                      int.parse(
                                                                          qty) *
                                                                          price;
                                                                  querySnapshot
                                                                      .documents[index]
                                                                      .reference
                                                                      .updateData(
                                                                      {
                                                                        "total": total
                                                                      });
                                                                  //================///====================//
                                                                  QuerySnapshot snapshot = await Firestore
                                                                      .instance
                                                                      .collection(
                                                                      'users')
                                                                      .document(
                                                                      phoneNumber)
                                                                      .collection(
                                                                      'cart')
                                                                      .getDocuments();

                                                                  if (qtyUpdated) {
                                                                    snapshot
                                                                        .documents
                                                                        .forEach((
                                                                        doc) {
                                                                      setState(() {
                                                                        totalCartValue +=
                                                                        doc
                                                                            .data['total'];
                                                                        qtyUpdated =
                                                                        false;
                                                                      });
                                                                    });
                                                                  }


                                                                  print(
                                                                      myController
                                                                          .toString());
                                                                  Navigator.of(
                                                                      context)
                                                                      .pop();

                                                                  Fluttertoast
                                                                      .showToast(
                                                                      msg: "Quantity Updated",toastLength: Toast
                                                                          .LENGTH_LONG,gravity: ToastGravity
                                                                          .CENTER,timeInSecForIosWeb: 1,backgroundColor: Colors
                                                                          .red,textColor: Colors
                                                                          .white,fontSize: 16.0
                                                                  );

                                                                  myController
                                                                      .clear();
                                                                }
                                                                else
                                                                if (int.parse(
                                                                    qty) < 5 ||
                                                                    int.parse(
                                                                        qty) <
                                                                        0) {
                                                                  Fluttertoast
                                                                      .showToast(
                                                                      msg: "Minimum 5 quantites",toastLength: Toast
                                                                          .LENGTH_SHORT,fontSize: 16.0
                                                                  );
                                                                  myController
                                                                      .clear();
                                                                }


                                                                else
                                                                if (int.parse(
                                                                    qty) > 25) {
                                                                  Fluttertoast
                                                                      .showToast(
                                                                      msg: "Maximun 25 quantites",fontSize: 16.0
                                                                  );
                                                                  myController
                                                                      .clear();
                                                                }
                                                              },],)
                                                      ],);
                                            });
                                      },child: Text('Add Quanity'),SizedBox(
                                      width: 60,RaisedButton(
                                      child: Text('Delete'),onpressed: () async {
                                        CollectionReference col = Firestore
                                            .instance.collection('users')
                                            .document(phoneNumber).collection(
                                            'cart');


                                        QuerySnapshot querySnapshot = await col
                                            .getDocuments();
                                        querySnapshot
                                            .documents[index]
                                            .reference
                                            .delete();
                                        getCartTotal();
                                        Fluttertoast.showToast(
                                            msg: "Item Deleted",toastLength: Toast.LENGTH_SHORT,gravity: ToastGravity.CENTER,textColor: Colors.white,fontSize: 16.0
                                        );
                                      },Padding(
                                  padding: const EdgeInsets.only(right: 150),child: Text(doc['quantity'].toString()),)
                              ],);
                  },);
              }
              else {
                return Noitems();
              }
            }
            else{
              return Text ('Loading Kart Details...');
            }
            },);
  }
}
class Checkoutclass extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: MaterialButton(onpressed:() {

          print("CheckoutClass");
        //====Navigator=======//
       /* Navigator.push(context,MaterialPageRoute(builder: (context)=>CheckoutScreen(totalQuantity:totalCartValue)));
            */
      },child: Text("Check Out",style: TextStyle(color: Colors.white),);
  }
}

class Noitems extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,home: Scaffold(

        body: Center(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[

                Icon(Icons.hourglass_empty,size: 100,SizedBox(height: 20,Text('Your cart is empty',style: TextStyle(fontSize: 25),RaisedButton(
                    onpressed: (){

                      Navigator.push(context,MaterialPageRoute(builder: (context)=>Homepagewithprice()));


                    },color:Colors.red,child:Text('browse Products',)



                )


              ],);
  }
}



解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)