如何将 定位到icon
的右下方Container
以包裹button
,而无需将设置height
为Container
?
我尝试使用Stack
,但没有成功。正如许多答案所建议的那样,我无法将 a 设置height
为 the Container
,因为根据我的文本的大小,它会被截断(我需要显示整个文本(这是琐事应用程序中的选择))。请参阅下面的代码片段并打印以查看在这种情况下会发生什么:
1)设置容器高度(不起作用):
转到Icon
右下角,但文本被截断:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget choiceSelectedButton = Container(
height: 50,
width: 100,
child: Stack(
children: [
Align(
child: OutlinedButton(
onPressed: () {},
child: Text('test test test test test test test test')),
),
Align(
alignment: Alignment.bottomRight,
child: Icon(
Icons.check_circle,
color: Colors.green,
size: 30,
),
),
],
),
);
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(child: choiceSelectedButton),
);
}
}
然后,我尝试删除高度,并尝试将Container
和包装Icon
在 Expanded、Flexible、FittedBox 中(即使这没有意义):
2)从容器中删除高度属性(不起作用):
现在,文本不会被截断,但图标会移位:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget choiceSelectedButton = Container(
width: 100,
child: Stack(
children: [
Align(
child: OutlinedButton(
onPressed: () {},
child: Text('test test test test test test test test')),
),
Align(
alignment: Alignment.bottomRight,
child: Icon(
Icons.check_circle,
color: Colors.green,
size: 30,
),
),
],
),
);
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(child: choiceSelectedButton),
);
}
}
我怎样才能做到这一点?
搜索的问题:
(但解决方案是设置高度)
Flutter 位置按钮底部非常右? (spacer()不起作用)
Flutter 容器定位右下角
alignment
属性不起作用
根本不起作用
右下角的图标颤动 (但解决方案是不设置高度,因此出现问题 2)