“Bottom Overflowed in ListTile – Flutter” 可以翻译为 “在Flutter中的ListTile底部溢出”。

huangapple go评论65阅读模式
英文:

Bottom Overflowed in ListTile - Flutter

问题

我已经创建了一个ListTile来从Firestore中的集合中返回所有我的文档。但我想在数字之前放置“R$”,而不是在数字上面。

是否有方法可以实现这个?另外,我想知道是否有办法在ListTile之前放置更多信息。当我尝试创建一个Text时,它给我一个错误。

这是代码:

class FinancasMensais extends StatefulWidget {
  const FinancasMensais({super.key});
  
  @override
  State<FinancasMensais> createState() => _FinancasMensaisState();
}

class _FinancasMensaisState extends State<FinancasMensais> {
  final _fireStore = FirebaseFirestore.instance;
  final ref = FirebaseFirestore.instance.collection('addsaidas').snapshots();

  Future<void> getData() async {
    QuerySnapshot querySnapshot =
        await _fireStore.collection('addsaidas').get();
    final allData = querySnapshot.docs.map((doc) => doc.data()).toList();

    for (var dataMap in allData) {
      if (dataMap is Map) {
        for (var key in dataMap.keys) {
          print('$key: ${dataMap[key]}');
        }
        print('----------------------');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromARGB(255, 92, 172, 178),
        centerTitle: true,
        title: Text('Finanças Mensais'),
        toolbarHeight: 90,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(40),
        ),
        elevation: 15,
      ),
      body: StreamBuilder<QuerySnapshot>(
        stream: ref,
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return const Center(child: CircularProgressIndicator());
          }
          final documents = snapshot.data!.docs;

          return ListView.builder(
            itemCount: documents.length,
            itemBuilder: (context, index) {
              final document = documents[index];
              final data = document.data() as Map<String, dynamic>;

              return ListTile(
                contentPadding:
                    EdgeInsets.only(left: 15, right: 15, top: 15, bottom: 4),
                leading: Icon(
                  Icons.where_to_vote_outlined,
                  color: Colors.pink,
                  size: 36,
                ),
                title: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      data['nomesaida'],
                      style: TextStyle(
                        fontSize: 22,
                      ),
                    ),
                    Text(data['tipocategoria']),
                  ],
                ),
                trailing: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      "R\$",
                      style: TextStyle(
                        fontSize: 15,
                      ),
                    ),
                    Text(
                      data['valorsaida'] as String,
                      style: TextStyle(
                        color: Colors.pink,
                        fontSize: 28,
                      ),
                    ),
                    Text(
                      data['datasaida'],
                      style: TextStyle(
                        color: Colors.pink,
                      ),
                    ),
                  ],
                ),
                dense: false,
              );
            },
          );
        },
      ),
    );
  }
}

如果您有其他问题,请随时提出。

英文:

I have created a ListTile to return all my documents from my collection in Firestore. But I want to put "R$" before the number, not above it:

“Bottom Overflowed in ListTile – Flutter” 可以翻译为 “在Flutter中的ListTile底部溢出”。

Is there a way to do it? Also, I would like to know if there is how to put more information before the ListTile. When I try to create a Text, it gives me an error:

“Bottom Overflowed in ListTile – Flutter” 可以翻译为 “在Flutter中的ListTile底部溢出”。

This is the code:

class FinancasMensais extends StatefulWidget {
const FinancasMensais({super.key});
@override
State&lt;FinancasMensais&gt; createState() =&gt; _FinancasMensaisState();
}
class _FinancasMensaisState extends State&lt;FinancasMensais&gt; {
final _fireStore = FirebaseFirestore.instance;
final ref =
FirebaseFirestore.instance.collection(&#39;addsaidas&#39;).snapshots();
Future&lt;void&gt; getData() async {
QuerySnapshot querySnapshot =
await _fireStore.collection(&#39;addsaidas&#39;).get();
final allData = querySnapshot.docs.map((doc) =&gt; doc.data()).toList();
for (var dataMap in allData) {
if (dataMap is Map) {
for (var key in dataMap.keys) {
print(&#39;$key: ${dataMap[key]}&#39;);
}
print(&#39;----------------------&#39;);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color.fromARGB(255, 92, 172, 178),
centerTitle: true,
title: Text(&#39;Finan&#231;as Mensais&#39;),
toolbarHeight: 90,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40)
),        
elevation: 15,
),
body: StreamBuilder&lt;QuerySnapshot&gt;(
//child: Center(child: Text(&#39;Todo Task&#39;),),
stream: ref,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
final documents = snapshot.data!.docs;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (context, index) {
final document = documents[index];
final data = document.data() as Map&lt;String, dynamic&gt;;
return ListTile(
contentPadding: EdgeInsets.only(left: 15, right: 15, top: 15, bottom: 4),
leading: Icon(Icons.where_to_vote_outlined,
color: Colors.pink,
size: 36,
),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(data[&#39;nomesaida&#39;],
style: TextStyle(
fontSize: 22,
),),
Text(data[&#39;tipocategoria&#39;]),
],
),
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(&quot;R\$&quot;,
style: TextStyle(
fontSize: 15
),
),
Text(data[&#39;valorsaida&#39;] as String,
style: TextStyle(
color: Colors.pink,
fontSize: 28,
),),
Text(data[&#39;datasaida&#39;],
style: TextStyle(
color: Colors.pink
),),
],
),
dense: false,
);
},
);
},
),
);
}
}

答案1

得分: 2

  • 在上述代码中,在数字前面放置特定文本
  • 在List Tile Widget前面添加文本

让我们讨论一下你的方法可能存在的问题。

  1. 你将特定文本和数字分别编码。为了避免这样做,你应该在单个Text Widget中添加两个字段,如下所示(假设没有样式约束):
Text('R\$ ${data['valorsaida']}',
  style: TextStyle(
    color: Colors.pink,
    fontSize: 28,
  ),
),

如果有样式约束,请尝试使用RichText Widget。

  1. 要在ListTile前添加信息,请尝试使用Column:
// 返回一个Column而不是ListTile,如下所示:
return Column(
  mainAxisSize: MainAxisSize.min,
  children: [
    // 在这里添加你的文本
    Center(child: Text('Todo Task')),
    // 在这里添加你的ListTile
    ListTile(...),
  ],
);

希望这对你有帮助。

英文:

Let me outline what you want to do with the above code:

  • Put specific text before the number
  • Add text before List Tile Widget

Let's talk about where you may be wrong with your approach.

  1. You are coding both the specific text and number separately. To avoid doing this you should add both fields in a single Text Widget like this(assuming there are no styling constraints):
Text(&#39;R\$ ${data[&#39;valorsaida&#39;]}&#39;,
style: TextStyle(
color: Colors.pink,
fontSize: 28,
),
),

Assuming you have styling constraints to adhere to, try using RichText Widget.

  1. To add information before ListTile, try using Column:
// return column instead of ListTile like this:
return Column(
mainAxisSize: MainAxisSize.min,
children: [
//Your Text here
Center(child: Text(&#39;Todo Task&#39;)),
//Your ListTile here
ListTile(...),
],
);

Hope this helps you.

答案2

得分: 1

  // const Text(
  //   "R\$",
  //   style: TextStyle(fontSize: 15),
  // ),
  // Text(
  //   data['valorsaida'] as String,
  //   style: const TextStyle(
  //     color: Colors.pink,
  //     fontSize: 28,
  //   ),
  // ),
  /// Put it inside a row to align horizontally
  Row(
    /// Align the children to the right.
    mainAxisAlignment: MainAxisAlignment.end,

    /// Make the row as small as possible.
    /// If you don't do this, the row will take up the entire width of the screen.
    /// And you'll get a flutter exception.
    mainAxisSize: MainAxisSize.min,
    children: [
      const Text(
        "R\$",
        style: TextStyle(fontSize: 15),
      ),
      Text(
        (data['valorsaida'] as num).toString(),
        style: const TextStyle(
          color: Colors.pink,
          fontSize: 28,
        ),
      ),
    ],
  ),

But you can also use the intl library to format currency for you. Give a try to that.

英文:

Duda!

The R$ is above the number cause you're putting all of them inside a Column - that aligns the children always vertically.

To make the R$ appears at the left of the number, you can wrap both inside a Row.

  // const Text(
  //   &quot;R\$&quot;,
  //   style: TextStyle(fontSize: 15),
  // ),
  // Text(
  //   data[&#39;valorsaida&#39;] as String,
  //   style: const TextStyle(
  //     color: Colors.pink,
  //     fontSize: 28,
  //   ),
  // ),
  /// Put it inside a row to align horizontally
  Row(
    /// Align the children to the right.
    mainAxisAlignment: MainAxisAlignment.end,

    /// Make the row as small as possible.
    /// If you don&#39;t do this, the row will take up the entire width of the screen.
    /// And you&#39;ll get a flutter exception.
    mainAxisSize: MainAxisSize.min,
    children: [
      const Text(
        &quot;R\$&quot;,
        style: TextStyle(fontSize: 15),
      ),
      Text(
        (data[&#39;valorsaida&#39;] as num).toString(),
        style: const TextStyle(
          color: Colors.pink,
          fontSize: 28,
        ),
      ),
    ],
  ),

But you can also use the intl library to format currency for you. Give a try to that.

huangapple
  • 本文由 发表于 2023年2月16日 04:10:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定