簡體   English   中英

Flutter Dart 填充和 TextAlign 錯誤

[英]Flutter Dart Error with padding and TextAlign

我正在嘗試刪除每張卡之間的空間並在中心獲取膳食文本

它來自EdgeInsets.only(top:220),但由於 AppBar 之間的空間,這是必需的。

試圖創建一個新的小部件,但這不起作用然后我得到一個 StreamBuilder 錯誤。 我還嘗試添加到邊距MediaQuery.removePadding(removeTop: true),但我也得到了一個錯誤。

我的第二個問題我希望 Meal Text 在中間,我添加了 textAlign: TextAlign.center。 在日期它正在工作,但為什么它不在頂部?

代碼

import 'package:flutter/material.dart';
import 'package:mealapp/models/Widgets/whenAndWhatToEat.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:intl/intl.dart';
import 'package:mealapp/models/global.dart';


class MealTile extends StatefulWidget {
  final MealsAndWhen mealsAndWhen;
  MealTile ({ this.mealsAndWhen });
  
  @override
  MealTileState createState() {
    return MealTileState();
  }
}
class MealTileState extends State<MealTile> {
  String id;
  final db = Firestore.instance;
  String meal;

  Widget buildItem(DocumentSnapshot doc) {
    return Padding(
      padding: EdgeInsets.only(top: 220),
      child: Container(
        margin: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              'Meal: ${doc.data['Meal']}',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Colors.white),
              textAlign: TextAlign.center,
            ),
            Text(
              'Date: ${doc.data['Date']}',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 12),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                SizedBox(width: 8),
                FlatButton(
                  onPressed: () => deleteData(doc),
                  child: Text('Delete',
                  style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white)),
                ),
              ],
            ),
          ],
        ),
        decoration: BoxDecoration(
          color: lightBlueColor,
          borderRadius: BorderRadius.all(Radius.circular(12)),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: darkGreyColor,
      body: ListView(
        padding: EdgeInsets.all(8),
        children: <Widget>[
          StreamBuilder<QuerySnapshot>(
            stream: db.collection('mealList').snapshots(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Column(children: snapshot.data.documents.map((doc) => buildItem(doc)).toList());
              } else {
                return SizedBox();
              }
            },
          )
        ],
      ),
    );
  }

  void deleteData(DocumentSnapshot doc) async {
    await db.collection('mealList').document(doc.documentID).delete();
    setState(() => id = null);
  }
}

[卡之間的空間 ]

不要將填充應用於卡片,而是將ListView包裝在Padding小部件中並將其應用到那里。

要對齊Text ,請將其包裝在Center小部件中。

要將填充添加到Text小部件,只需將padding屬性添加到Container

例如:

      padding: EdgeInsets.all(12),

要使您的Text居中,只需將CrossAxisAlignment.start替換為CrossAxisAlignment.center

Column(
    **crossAxisAlignment: CrossAxisAlignment.center,**
    children: <Widget>[
      Text(
        'Meal: Example Meal',
        style: TextStyle(
            fontSize: 24,
            fontWeight: FontWeight.bold,
            color: Colors.white),
        textAlign: TextAlign.center,
      ),
      Text(
        'Date: Example Date',
        style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: Colors.white),
        textAlign: TextAlign.center,
      ),
    ],
  );

這是 output: 顫振輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM