Flutter 如何在小部件层次结构中约束ListTile

Flutter 如何在小部件层次结构中约束ListTile,flutter,flutter-layout,Flutter,Flutter Layout,如何使listile在此小部件层次结构中工作: SingleChildScrollView( 滚动方向:轴垂直, 子:列( 儿童:[ 划船( mainAxisSize:mainAxisSize.min, 儿童:[ 无线电( 价值:1, 组值:1, 一旦更改:(sel){}, ), 手势检测器( 孩子:排( 儿童:[ //Text('Title'),--这是可以的,但ListTile失败 列表砖( 标题:文本(“标题”), 字幕:文本(“字幕”), ), ], ), onTap:(){}, ),

如何使
listile
在此小部件层次结构中工作:

SingleChildScrollView(
滚动方向:轴垂直,
子:列(
儿童:[
划船(
mainAxisSize:mainAxisSize.min,
儿童:[
无线电(
价值:1,
组值:1,
一旦更改:(sel){},
),
手势检测器(
孩子:排(
儿童:[
//Text('Title'),--这是可以的,但ListTile失败
列表砖(
标题:文本(“标题”),
字幕:文本(“字幕”),
),
],
),
onTap:(){},
),
],
),
],
),
),
请理解,我并不是完全控制小部件层次结构——一个框架正在构建
SingleChildScrollView
Radio
GestureDetector
,等等。我只是试图为一个选项提供一个子小部件。(我以简化的形式复制了层次结构以进行调试和实验。)

我得到一个例外:

flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performLayout():
flutter: BoxConstraints forces an infinite width.
flutter: These invalid constraints were provided to RenderParagraph's layout() function by the following
flutter: function, which probably computed the invalid constraints in question:
flutter:   _RenderListTile._layoutBox (package:flutter/src/material/list_tile.dart:1318:9)
flutter: The offending constraints were:
flutter:   BoxConstraints(w=Infinity, 0.0<=h<=Infinity)
颤振:══╡ 呈现库捕获到异常╞═════════════════════════════════════════════════════════
颤振:在performLayout()期间抛出了以下断言:
颤振:BoxConstraints强制无限宽。
颤振:这些无效约束是由以下程序提供给RenderParagraph的layout()函数的
颤振:函数,它可能计算了所讨论的无效约束:
颤振:_renderList._layoutBox(包装:颤振/src/material/list_tile.dart:1318:9)
颤振:有问题的约束是:

颤振:BoxConstraints(w=无穷大,0.0您可以将
列表磁贴
包装在
容器
约束框
中,并设置其
最大宽度

ConstrainedBox(
      constraints: BoxConstraints(
        maxWidth: MediaQuery.of(context).size.width - 50,
      ),
      child: ListTile(
        title: Text('Title'),
        subtitle: Text('Subtitle'),
        trailing: Text('Trailing'),
      ),
    ),

您可以复制粘贴运行下面的完整代码
您可以使用
IntrinsicWidth
wrap
Row
Expanded
wrap
ListTile
,并且可以有多个
Expaned ListTile

GestureDetector(
                  child: IntrinsicWidth(
                    child: Row(
                      children: <Widget>[
                        //Text('Title'), -- This is OK, but ListTile fails
                        Expanded(
                          child: ListTile(
                            title: Text('Title'),
                            subtitle: Text('Subtitle'),
                          ),
                        ),
                      ],
                    ),
                  ),
手势检测器(
子:内部宽度(
孩子:排(
儿童:[
//Text('Title'),--这是可以的,但ListTile失败
扩大(
孩子:ListTile(
标题:文本(“标题”),
字幕:文本(“字幕”),
),
),
],
),
),
工作演示

完整代码

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Column(
          children: [
            Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Radio(
                  value: 1,
                  groupValue: 1,
                  onChanged: (sel) {},
                ),
                GestureDetector(
                  child: IntrinsicWidth(
                    child: Row(
                      children: <Widget>[
                        //Text('Title'), -- This is OK, but ListTile fails
                        Expanded(
                          child: ListTile(
                            isThreeLine: true,
                            title: Text('Title'),
                            subtitle: Text('this is long text this is long text this is long text \n'* 10),
                          ),
                        ),
                      ],
                    ),
                  ),
                  onTap: () {},
                ),
              ],
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:SingleChildScrollView(
滚动方向:轴垂直,
子:列(
儿童:[
划船(
mainAxisSize:mainAxisSize.min,
儿童:[
无线电(
价值:1,
组值:1,
一旦更改:(sel){},
),
手势检测器(
子:内部宽度(
孩子:排(
儿童:[
//Text('Title'),--这是可以的,但ListTile失败
扩大(
孩子:ListTile(
伊斯特里琳:是的,
标题:文本(“标题”),
字幕:Text('这是长文本这是长文本这是长文本\n'*10),
),
),
],
),
),
onTap:(){},
),
],
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}
完整代码2重新播放

    import 'package:flutter/cupertino.dart';
    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> {
      int _counter = 0;

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

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            child: Column(
              children: [
                Row(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: Radio(
                        value: 1,
                        groupValue: 1,
                        onChanged: (sel) {},
                      ),
                    ),
                    Expanded(
                      flex: 5,
                      child: GestureDetector(
                        child: LayoutBuilder(
                          builder:(context, constraints) {
                            print(constraints.maxWidth);
                            print(constraints.minWidth);
                            return ConstrainedBox(
                              constraints: BoxConstraints(                            
                                  maxWidth: constraints.maxWidth),
                              child: ListTile(
                                //isThreeLine: true,
                                title: Text('Title'),
                                subtitle: Text(
                                  'Textlargeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 2,
                                  style: TextStyle(
                                    fontSize: 13.0,
                                    fontFamily: 'Roboto',
                                    color: Color(0xFF212121),
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            );
                          }
                        ),
                        onTap: () {},
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
F