行中小部件的动态高度波动

时间:2020-07-23 08:47:31

标签: flutter dart

如何将窗口小部件的高度动态扩展到父窗口小部件。

特别是在我的示例中,我想将红色条扩展到父窗口小部件。我的文字可以更改为x行。

当前代码:

import 'package:flutter/material.dart';

class ExampleView extends StatelessWidget {
  final String title;

  const ExampleView({Key key, this.onTap, this.title}) : super(key: key);

  final text = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et';

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 40),
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      color: Colors.grey[200],
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            margin: const EdgeInsets.only(right: 12, top: 4, bottom: 4),
            width: 6,
            height: 80, // FIXME: Dynamic
            color: Colors.red,
          ),
          Expanded(
            child: Text(text),
          )
        ],
      ),
    );
  }
}

当前解决方案:

Current solution

所需解决方案:

enter image description here

预先感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

您可以在下面复制粘贴运行完整代码
您可以使用IntrinsicHeight
代码段

IntrinsicHeight(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: const EdgeInsets.only(right: 12, top: 4, bottom: 4),
              width: 6,
              color: Colors.red,
            ),

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

class ExampleView extends StatelessWidget {
  final String title;

  const ExampleView({Key key, this.title}) : super(key: key);

  final text = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et';

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 40),
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      color: Colors.grey[200],
      child: IntrinsicHeight(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: const EdgeInsets.only(right: 12, top: 4, bottom: 4),
              width: 6,
              color: Colors.red,
            ),
            Expanded(
              child: Text(text),
            )
          ],
        ),
      ),
    );
  }
}

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> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {     
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {    
    return Scaffold(
      appBar: AppBar(       
        title: Text(widget.title),
      ),
      body: Center(        
        child: Column(          
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ExampleView(),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

答案 1 :(得分:1)

解决方案#1

在这种情况下,您不需要使用“行”窗口小部件。您可以将红色列添加为“边框”而不是“容器”小部件。代码如下:

figure

解决方案2

您可以平移子容器,而不用边框,再用一个带有渐变的容器包装。

id