带有填充和TextAlign的Flutter Dart错误

我正在尝试删除每张卡之间的空间,并在中心获取膳食文本

它来自EdgeInsets.only(top:220),但是由于AppBar之间的空间,所以需要这样做。

试图创建一个新的小部件,但这不起作用,然后我得到一个StreamBuilder错误。我也尝试将空白添加到MediaQuery.removePadding(removeTop:true),但是在那里也出现错误。

我的Secound问题我希望餐文本在中间,我添加了textAlign:TextAlign.center。在Date上它起作用了,但是为什么它不在最上面呢?

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);
  }
}

[]

评论
stotam
stotam

Instead of applying the padding to the card, wrap your ListView inside a Padding widget and apply it there.

To align the Text, wrap it in a Center widget.

点赞
评论
but
but

To add padding to Text Widgets, just add padding property to Container

例如:

      padding: EdgeInsets.all(12),

To Center your Text just replace CrossAxisAlignment.start with 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,
      ),
    ],
  );

Here's the output: Flutter Output

点赞
评论