Флаттер Гибкая не обтекание текста внутри столбца - PullRequest
0 голосов
/ 31 октября 2019

Я пытаюсь создать виджет с длинным текстом, который я хотел бы заключить в несколько строк.

Я пытаюсь использовать виджет " Flexible " для переносамой текст, но он все еще переполнен, и я не знаю, что идет не так.

Вот что происходит: enter image description here

Вот мой код длястолбцы, которые будут иметь отношение к тексту:

Container(
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        'My Title text',
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 18.0,
            color: Colors.black),
      ),
      Text(
        'This is lower text',
        style: TextStyle(
            fontWeight: FontWeight.w200,
            fontSize: 16.0,
            color: Colors.black),
      ),
      Flexible(
        child: Text(
          'Here is some long text that I am expecting will go off of the screen.',
          style: TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: 16.0,
              color: Colors.black),
        ),
      )
    ],
  ),
),

И в случае, если это уместно, вот целый виджет

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Material(
        color: Colors.transparent,
        child: Container(
          height: 100.0,
          child: Padding(
            padding: EdgeInsets.all(8.0),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Padding(
                  padding: EdgeInsets.all(16.0),
                  child: Icon(
                    Icons.cake,
                    size: 60.0,
                  ),
                ),
                Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'My Title text',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 18.0,
                            color: Colors.black),
                      ),
                      Text(
                        'This is lower text',
                        style: TextStyle(
                            fontWeight: FontWeight.w200,
                            fontSize: 16.0,
                            color: Colors.black),
                      ),
                      Flexible(
                        child: Text(
                          'Here is some long text that I am expecting will go off of the screen.',
                          style: TextStyle(
                              fontWeight: FontWeight.normal,
                              fontSize: 16.0,
                              color: Colors.black),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

Ответы [ 4 ]

1 голос
/ 31 октября 2019

Вам нужно поместить контейнер в расширенный виджет внутри строки. Вот так


child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.all(16.0),
                  child: Icon(
                    Icons.cake,
                    size: 60.0,
                  ),
                ),
                Expanded(child: Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'My Title text',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 18.0,
                            color: Colors.black),
                      ),
                      Text(
                        'This is lower text',
                        style: TextStyle(
                            fontWeight: FontWeight.w200,
                            fontSize: 16.0,
                            color: Colors.black),
                      ),
                      Flexible(
                        child: Text(
                          'Here is some long text that I am expecting will go off of the screen.',
                          softWrap: true,
                          style: TextStyle(
                              fontWeight: FontWeight.normal,
                              fontSize: 16.0,
                              color: Colors.black),
                        ),
                      )
                    ],
                  ),
                ),
             ) ],
            ),

Проблема в том, что если вы не поместите контейнер в расширенный виджет, то виджет строки будет расширяться до горизонтального положения, и он будетпереполнение.

0 голосов
/ 31 октября 2019

Вы можете попробовать этот подход, поместив width контейнера в 70% и для изображения 30%. Здесь нет необходимости в гибком виджете

Container(
 width:MediaQuery.of(context).size.width*0.7
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Text(
        'Here is some long text that I am expecting will go off of the screen.',
        style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 18.0,
            color: Colors.black),
      ),
      Text(
        'Here is some long text that I am expecting will go off of the screen.',
        style: TextStyle(
            fontWeight: FontWeight.w200,
            fontSize: 16.0,
            color: Colors.black),
      ),
      Text(
          'Here is some long text that I am expecting will go off of the screen.',
          style: TextStyle(
              fontWeight: FontWeight.normal,
              fontSize: 16.0,
              color: Colors.black
         )
    ],
  ),
),
0 голосов
/ 31 октября 2019

Вы можете использовать Расширенное Здесь. Expanded , который заставляет ребенка расширяться, чтобы заполнить доступное пространство. Вы можете развернуть столбец здесь.

Вот фрагмент кода для столбца: -

Expanded(
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'My Title text',
                          style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 18.0,
                              color: Colors.black),
                        ),
                        Text(
                          'This is lower text',
                          style: TextStyle(
                              fontWeight: FontWeight.w200,
                              fontSize: 16.0,
                              color: Colors.black),
                        ),
                        Flexible(
                          child: Text(
                            'Here is some long text that I am expecting will go off of the screen.',
                            style: TextStyle(
                                fontWeight: FontWeight.normal,
                                fontSize: 16.0,
                                color: Colors.black),
                          ),
                        )
                      ],
                    ),
                  ),
                )

Вот что вы ожидаете: - enter image description here

Надеюсь, это сработает.

0 голосов
/ 31 октября 2019

Попробуйте, вам нужно отрегулировать ширину столбца, содержащего здесь текст.

Рабочий код: -


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Material(
        color: Colors.transparent,
        child: Container(
          height: 100.0,
          child: Padding(
            padding: EdgeInsets.all(8.0),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Padding(
                  padding: EdgeInsets.all(16.0),
                  child: Icon(
                    Icons.cake,
                    size: 60.0,
                  ),
                ),
                Container(
                  child: Flexible(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          'My Title text',
                          style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 18.0,
                              color: Colors.black),
                        ),
                        Text(
                          'This is lower text',
                          style: TextStyle(
                              fontWeight: FontWeight.w200,
                              fontSize: 16.0,
                              color: Colors.black),
                        ),
                        Flexible(
                          child: Text(
                            'Here is some long text that I am expecting will go off of the screen.',
                            style: TextStyle(
                                fontWeight: FontWeight.normal,
                                fontSize: 16.0,
                                color: Colors.black),
                            maxLines: 2,
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

enter image description here

...