简体   繁体   中英

Flutter Dart Error with padding and TextAlign

I'm trying to remove the space between each Card and get the Meal Text in the Center

It comes from the EdgeInsets.only(top:220), but this is needed due to the Space between the AppBar.

Tried to Create a new Widget but this doesn't work then i get a StreamBuilder Error. I also tried to add to the margin MediaQuery.removePadding(removeTop: true), but there i get an error aswell.

My Secound Problem i want that the Meal Text is in the middle, i added the textAlign: TextAlign.center. At the Date it is working but why isn't it at the top one?

Code

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

[ 卡之间的空间 ]

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.

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

eg:

      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: 颤振输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM