簡體   English   中英

“如何在一個行小部件中創建兩個文本小部件”

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

“我是 flutter 的初學者,我想在行中顯示兩個文本。其中“hello”是第一個文本小部件,“world”是另一個文本小部件

我嘗試用我的基本知識做到這一點,但我遇到了這些錯誤

Horizontal RenderFlex with multiple children 有一個 null textDirection,所以布局順序是未定義的。 '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,)
      ],
    );
  }
}

也許您想改用 RichText?

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

請嘗試以下代碼:-

@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"),
          ],
        ),
      ),
    );
  }
}

請閱讀有關 Flutter 中的行的鏈接:

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

簡而言之,您聲明一個行,該行內的項目是子項(必須是小部件)。

我從這里選擇了一個例子https://flutter.dev/docs/development/ui/layout

像這樣的東西

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

在您的情況下,您只需刪除 image.asset 並將其更改為文本...

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

  ],
);

或任何你想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM