简体   繁体   English

“如何在一个行小部件中创建两个文本小部件”

[英]“How to create two text widgets in a row widget”

"I'm a beginner in flutter, I want to display two text in row.where "hello" is the first text widget and "world" is another text widget “我是 flutter 的初学者,我想在行中显示两个文本。其中“hello”是第一个文本小部件,“world”是另一个文本小部件

I tried doing this with my basic knowledge but I came across these errors我尝试用我的基本知识做到这一点,但我遇到了这些错误

Horizontal RenderFlex with multiple children has a null textDirection, so the layout order is undefined. Horizontal RenderFlex with multiple children 有一个 null textDirection,所以布局顺序是未定义的。 'package:flutter/src/rendering/flex.dart': Failed assertion: line 439 pos 18: 'textDirection != null' 'package:flutter/src/rendering/flex.dart':断言失败:第 439 行 pos 18:'textDirection != null'

    class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Row(
      children: <Widget>[
        Text("hello",textDirection: TextDirection.rtl,),
        Text("world",textDirection: TextDirection.rtl,)
      ],
    );
  }
}

Maybe you want to use RichText instead?也许您想改用 RichText?

RichText(
  text: TextSpan(
    children: <TextSpan>[
      TextSpan(text: 'hello', style: TextStyle(color: Colors.red)),
      TextSpan(text: ' world', style: TextStyle(color: Colors.blue)),
    ],
  ),
)

Please try below code:-请尝试以下代码:-

@override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text("Hello",style: TextStyle(color: Colors.blue)),
        Text("World",style: TextStyle(color: Colors.blue))
      ],
    );
}
**Try This Simplest Way**

import 'package:flutter/material.dart';
class Example extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Row(
          children: <Widget>[
        Text("Hello"),
        Text("World"),
          ],
        ),
      ),
    );
  }
}

Please read this link about Row in Flutter:请阅读有关 Flutter 中的行的链接:

https://medium.com/jlouage/flutter-row-column-cheat-sheet-78c38d242041

In a nutshell, you Declare a Row and the items that will be inside that row are the children (Which must be widgets).简而言之,您声明一个行,该行内的项目是子项(必须是小部件)。

I picked an example from here https://flutter.dev/docs/development/ui/layout我从这里选择了一个例子https://flutter.dev/docs/development/ui/layout

something like this像这样的东西

Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: [
    Image.asset('images/pic1.jpg'),
    Image.asset('images/pic2.jpg'),
    Image.asset('images/pic3.jpg'),
  ],
);

In Your case, you just have to erase the image.asset and change it for texts...在您的情况下,您只需删除 image.asset 并将其更改为文本...

Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: [
    Text('Hello'),
    Text('World'),

  ],
);

or whatever you want.或任何你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在行小部件中对齐文本小部件 - Align Text widgets in a row widget 如何在 ListView 中的 flutter 中创建一行可滚动的文本框或小部件? - how to create a row of scrollable text boxes or widgets in flutter inside a ListView? 如何根据其他小部件调整行中小部件的大小? - How to size a Widget in Row based on other widgets? 如何在行中对齐文本小部件? - How to align Text widgets in Row? 如何将小部件放在两个小部件的中心和顶部? - How to put widget in the center and top of two widgets? 如何将省略号添加到行小部件中两个文本小部件之间的文本小部件 - How to add ellipsis to a text widget in between two text widget in a row widget Widget Classes Arguments 如何作为道具在 Flutter 中创建具有行和列的动态 Widget? - How can Widget Classes Arguments be used as props to create dynamic Widgets with Row and Columns in Flutter? 如何在 flutter 中创建一个只有两行文本的文本小部件? - How to create a Text widget with exactly two lines of text in flutter? Flutter 带有两个两个文本小部件的行小部件没有相互跟随 - Flutter Row widget with two two text widget is not following one another 如何在单行小部件上左对齐、居中、右对齐 flutter - How to align widgets left, center, right on single row widget flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM