【问题标题】:How to use ellipses in TextField如何在 TextField 中使用省略号
【发布时间】:2021-04-17 12:20:22
【问题描述】:

在我的应用程序中,我想从 firebase 检索当前用户的全名,并希望将其显示在 TextField 中,该名称基本上放置在 App Bar 中,而 TextField 中的半名显示以省略号结尾。 我们可以在 Text 中使用省略号,但如何在 TextField 中使用。

这是我的代码:

class _HomePageState extends State<HomeTab> {
  User user = FirebaseAuth.instance.currentUser;
  DatabaseReference dbRef = FirebaseDatabase.instance.reference();
  TextEditingController NameController = TextEditingController();

  @override
  void initState() {
    super.initState();
    getData();
  }

 @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          elevation: 1,
          backgroundColor: Colors.white,
          brightness: Brightness.light,
          leading: Padding(
            padding: const EdgeInsets.all(0.0),
            child: Image.asset(
              "assets/images/logo.png",
              height: 300,
              width: 300,
            ),
          ),
          actions: <Widget>[
            Container(
              child: TextField(
                controller: NameController,
                enabled: false,
                decoration: InputDecoration(
                border: InputBorder.none,
                isDense: true,
                contentPadding: EdgeInsets.all(5),
                ),
                style: TextStyle(fontSize: 22, color: Colors.black),
               // overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.center,
              ),
            )
          ],
        ),
void getData() {
    dbRef.child('Users').child(user.uid).once().then((DataSnapshot snapshot) {
      print('Data : ${snapshot.value}');
      setState(() {
        var name = snapshot.value['name'];
        NameController.text = name;
     
      });
    });
  }
}

当我使用此代码时出现此错误:

════════渲染库捕获的异常═════════════════════════════╕ ══ RenderBox 未布置:RenderRepaintBoundary#10696 NEEDS-LAYOUT NEEDS-PAINT 'package:flutter/src/rendering/box.dart': 断言失败:第 1940 行第 12 行:'hasSize' 相关的导致错误的小部件是 文本字段

【问题讨论】:

    标签: flutter dart textfield


    【解决方案1】:

    如果只需要显示用户名,则无需使用TextField,只需使用Text Widget。

    在这种情况下(TextField),用ContainerSizedBox 包装TextField,用heightwidth 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2014-11-21
      • 2019-04-28
      • 2010-09-12
      • 2016-12-14
      • 2017-01-02
      相关资源
      最近更新 更多