简体   繁体   中英

“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

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. 'package:flutter/src/rendering/flex.dart': Failed assertion: line 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(
  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:

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

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...

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

  ],
);

or whatever you want.

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