为什么CrossAxisAlignment.end在这里不起作用?

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

Why isn't CrossAxisAlignment.end working here?

问题

ListView中,我返回的要么是左对齐的小部件,要么是右对齐的小部件。左对齐正常工作,但用户名的右对齐不起作用。为什么会这样?

Widget myRegularMessageWidget(String uid, String username, int timestamp, String text) {
    return Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Container(
            decoration: BoxDecoration(
              border: Border.all(color: Colors.blueAccent)
            ),
            child: FractionallySizedBox(
              widthFactor: 1, 
              child: ListTile(
                title: Text(username,
                    style: const TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16)
                ),
                subtitle: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(text),
                  ],
                ),
              ),
            ),
          ),
        ],
      );
  }


  Widget otherUserRegularMessageWidget(String uid, String username, int timestamp, String text) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          decoration: BoxDecoration(
              border: Border.all(color: Colors.blueAccent)
          ),
          child: FractionallySizedBox(
            widthFactor: 0.75, 
            child: ListTile(
              leading: const CircleAvatar(
                child: FlutterLogo(),
              ),
              title: Text(username,
                  style: const TextStyle(
                      fontWeight: FontWeight.bold, fontSize: 16)),
              subtitle: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(text),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }

输出的图片如下(由于某种原因,正常添加图片的方式无法使用):

为什么CrossAxisAlignment.end在这里不起作用?

英文:

In a ListView I am returning either a left aligned widget or a right aligned widget. The left alignment is working fine, but the right alignment isn't for the username. Why is this?

Widget myRegularMessageWidget(String uid, String username, int timestamp, String text) {
    return Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Container(
            decoration: BoxDecoration(
              border: Border.all(color: Colors.blueAccent)
            ),
            child: FractionallySizedBox(
              widthFactor: 1, 
              child: ListTile(
                title: Text(username,
                    style: const TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16)
                ),
                subtitle: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(text),
                  ],
                ),
              ),
            ),
          ),
        ],
      );
  }


  Widget otherUserRegularMessageWidget(String uid, String username, int timestamp, String text) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          decoration: BoxDecoration(
              border: Border.all(color: Colors.blueAccent)
          ),
          child: FractionallySizedBox(
            widthFactor: 0.75, 
            child: ListTile(
              leading: const CircleAvatar(
                child: FlutterLogo(),
              ),
              title: Text(username,
                  style: const TextStyle(
                      fontWeight: FontWeight.bold, fontSize: 16)),
              subtitle: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(text),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }

Image of the output (for some reason the normal way to add a pic isn't working):

为什么CrossAxisAlignment.end在这里不起作用?

答案1

得分: 1

不需要为显示您的消息和其他用户消息创建两个单独的函数,只需将Username、Message小部件包装在一个Column中,并添加一个条件来显示左/右。

import 'package:flutter/material.dart';

class ChatScreen extends StatefulWidget {
  const ChatScreen({Key? key}) : super(key: key);

  @override
  State<ChatScreen> createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
  int? selectedIndex;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: ListView.builder(
          itemCount: 5,
          itemBuilder: (BuildContext context, int index) {
            final isSelected = index == selectedIndex;
            return FractionallySizedBox(
              widthFactor: 1, // 设置列表项的宽度
              child: ListTile(
                onTap: () {
                  setState(() {
                    selectedIndex = isSelected ? null : index;
                  });
                },
                leading: if (uid == 'myId')
                  const CircleAvatar(
                    child: FlutterLogo(),
                  )
                else
                  const SizedBox.shrink(),
                title: Column(
                  crossAxisAlignment: uid != 'myId'
                      ? CrossAxisAlignment.end
                      : CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Username ${index + 1}',
                      style: const TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16,
                      ),
                    ),
                  ],
                ),
                trailing: if (uid != 'myId')
                  const CircleAvatar(
                    child: FlutterLogo(),
                  )
                else
                  const SizedBox.shrink(),
                subtitle: Column(
                  crossAxisAlignment: uid != 'myId'
                      ? CrossAxisAlignment.end
                      : CrossAxisAlignment.start,
                  children: [
                    Text('This is message ${index + 1}'),
                    Visibility(
                      visible: isSelected,
                      child: Text(
                        DateTime.now().toString(),
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}

注意: 您需要将上面代码中的 index % 2 这个条件替换为您的用户ID或用户名,例如 if (uid == 'myId') 然后使用 CrossAxisAlignment.end,否则使用 CrossAxisAlignment.start,在上面的代码段中的所有地方以获取所需的输出。

英文:

No need to create two separate functions for showing your message and other user messages just wrap Username, Message widget with a column and add a condition for showing left/right.

为什么CrossAxisAlignment.end在这里不起作用?

import &#39;package:flutter/material.dart&#39;;
class ChatScreen extends StatefulWidget {
const ChatScreen({Key? key}) : super(key: key);
@override
State&lt;ChatScreen&gt; createState() =&gt; _ChatScreenState();
}
class _ChatScreenState extends State&lt;ChatScreen&gt; {
int? selectedIndex;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: ListView.builder(
itemCount: 5,
itemBuilder: (BuildContext context, int index) {
final isSelected = index == selectedIndex;
return FractionallySizedBox(
widthFactor: 1, // set width of listtile
child: ListTile(
onTap: () {
setState(() {
selectedIndex = isSelected ? null : index;
});
},
leading: index % 2 == 0
? const CircleAvatar(
child: FlutterLogo(),
)
: const SizedBox.shrink(),
title: Column(
crossAxisAlignment: index % 2 != 0
? CrossAxisAlignment.end
: CrossAxisAlignment.start,
children: [
Text(
&#39;Username ${index + 1}&#39;,
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
),
],
),
trailing: index % 2 != 0
? const CircleAvatar(
child: FlutterLogo(),
)
: const SizedBox.shrink(),
subtitle: Column(
crossAxisAlignment: index % 2 != 0
? CrossAxisAlignment.end
: CrossAxisAlignment.start,
children: [
Text(&#39;This is message ${index + 1}&#39;),
Visibility(
visible:
isSelected, // used for show/hide date when message is clicked on
child: Text(
DateTime.now().toString(),
),
),
],
),
),
);
}),
),
);
}
}

Note: You need to replace index % 2 this condition with your uid or username like if (uid == &#39;myId&#39;) then CrossAxisAlignment.end or else CrossAxisAlignment.start. Everywhere in the above snippet for getting the desire output.

huangapple
  • 本文由 发表于 2023年5月26日 13:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337843.html
匿名

发表评论

匿名网友

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

确定