Chris
Chris

Reputation: 2274

Flutter break text and expand container if text is longer

I have a Container that also has a Text as a child. The problem is that my text is overflowing on the right. But actually I want it to break and the parent container should expand.

This is how it looks right now:

enter image description here

And my code:

  Widget build(BuildContext context) {
return GestureDetector(
  onTap: onTap,
  child: Padding(
    padding: EdgeInsets.only(
      bottom: 14.scaled,
    ),
    child: Container(
      // height: 70.scaled,
      decoration: BoxDecoration(
        boxShadow: [verySoftDarkShadow],
        color: white,
        borderRadius: BorderRadius.circular(10.scaled),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            width: 20.scaled,
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                shortArticle.label!,
                style: alternativeText,
              ),
              Text(
                shortArticle.manufacturerLabel ?? '',
                style: alternativeText.copyWith(color: darkGray),
              ),
            ],
          ),
          Spacer(),
          SvgIconButton(
            onTap: () {
              print('tapped');
            },
            svgPath: 'images/vertical_menue_dots.svg',
            width: touchTargetSizeForIconButton,
            height: touchTargetSizeForIconButton,
            svgSize: 16.scaled,
          ),
        ],
      ),
    ),
  ),
);


}
}

I tried wrapping the text-widget inside a Expanded but that just makes it overflowing and not displaying anything. What am I missing here? How can I solve this?

Let me know if you need any more info!

Upvotes: 0

Views: 2887

Answers (2)

KuKu
KuKu

Reputation: 7492

Just wrap Column widget with Expanded and add a 'overflow' parameter to Text widget.
(Because I don't know pre-defined style and asset, I changed my myself.)

Look refer to below 'buildWidget' function based from your code.<br.

enter image description here

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 20),
      child: Column(children: [
        buildWidget(),
        buildWidget(),
      ]),
    );
  }
}

Widget buildWidget() {
  return GestureDetector(
    onTap: () {},
    child: Padding(
      padding: EdgeInsets.only(
        bottom: 14,
      ),
      child: Container(
        // height: 70.scaled,
        // decoration: BoxDecoration(
        //   boxShadow: [verySoftDarkShadow],
        //   color: white,
        //   borderRadius: BorderRadius.circular(10.scaled),
        // ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(
              width: 20,
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'shortArticle.label!shortArticle.label!shortArticle.l!shortArticle.label!',
                    // overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontSize: 13, fontWeight: FontWeight.bold),
                  ),
                  Text(
                    'asdfasdf',
                    style: TextStyle(fontSize: 11),
                  ),
                ],
              ),
            ),
            Icon(
              Icons.favorite,
              color: Colors.pink,
              size: 24.0,
              semanticLabel: 'Text to announce in accessibility modes',
            ),
          ],
        ),
      ),
    ),
  );
}

Upvotes: 2

Anas Nadeem
Anas Nadeem

Reputation: 887

In your Text Widget add:

maxLines: null

Upvotes: 0

Related Questions