我正在尝试将ClipRect与其中的Column一起使用,但它似乎效果不佳。
我想要实现的是剪辑列的内容并在溢出时显示文本消息(如果列的内容无法在可用空间内显示)。
你对我如何实现它有什么建议吗?
import 'package:flutter/material.dart';
void main() => runApp(ContentOverflowDetectionApp());
class ContentOverflowDetectionApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Overflow detection"),
),
body: Stack(
fit: StackFit.expand,
children: [
ClipRect(
child: Column(
children: [
Container(
width: 300,
height: 400,
color: Colors.green[200],
child: Text('first widget'),
),
Container(
width: 350,
height: 350,
color: Colors.yellow[200],
child: Text('overflowed widget'),
),
],
),
),
Positioned(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Text("SHOW THIS TEXT ONLY IF CONTENT HAS OVERFLOWED."),
),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 10
您不应该将 ClipRect 用于您的目标。请尝试将溢出参数 Overflow.clip 添加到 Stack 小部件。
import 'package:flutter/material.dart';
void main() => runApp(ContentOverflowDetectionApp());
class ContentOverflowDetectionApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Overflow detection"),
),
body: Stack(
fit: StackFit.expand,
clipBehavior: Clip.antiAlias,
children: [
Positioned(
top: 0,
child: Column(
children: [
Container(
width: 300,
height: 400,
color: Colors.green[200],
child: Text('first widget'),
),
Container(
width: 350,
height: 350,
color: Colors.yellow[200],
child: Text('overflowed widget'),
),
],
),
),
Positioned(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Text("SHOW THIS TEXT ONLY IF CONTENT HAS OVERFLOWED."),
),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案只是修复了一个剪辑。
如何获取小部件的高度?给出了如何检查小部件高度并添加带有溢出消息的文本的答案