[英]How to add an icon after text widget in an expanded widget?
我认为这可以解决它。
Row(
children: const [
Expanded(
child: Text(
'this line is overflow, but icon is in right position',
style: TextStyle(fontSize: 12),
softWrap: false,
maxLines: 2,
overflow: TextOverflow.ellipsis, // new
),
),
],
)
或者
Row(
children: const [
Expanded(
child: FittedBox(
child: Text(
'this line is overflow, but icon is in right position',
style: TextStyle(fontSize: 12),
softWrap: false,
overflow: TextOverflow.ellipsis,
),
),
),
],
)
代码示例
return Scaffold(
body: SafeArea(
child: Expanded(
child: ColoredBox(
color: Colors.deepOrangeAccent,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Line 1',
style: TextStyle(fontSize: 24),
),
Row(
children: const [
Container(
child: Text(
'this line might overflow',
style: TextStyle(fontSize: 12),
),
),
Icon(Icons.arrow_right),
],
),
Row(
children: const [
Expanded(
child: Text(
'this line is overflow, but icon is in right position // this line is overflow, but icon is in right position',
style: TextStyle(fontSize: 12),
softWrap: false,
//maxLines: 2,
overflow: TextOverflow.ellipsis, // new
),
),
Icon(Icons.arrow_right),
],
),
],
),
),
),
),
);
我会告诉你我知道的两种方法。
Container
小部件width:
此方法允许您在文本旁边附加一个图标。Expanded
小部件Expanded
小部件,因此Text
小部件占用了除Icon
小部件空间之外的所有剩余空间。您可以根据您的应用程序的条件使用它。
你应该试试这个解决方案
主要.dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
SizedBox(
height: 130,
child: Row(
children: [
Expanded(
flex: 2,
child: Container(
color: Colors.yellowAccent,
),
),
Expanded(
flex: 5,
child: Column(
children: [
Expanded(
flex: 5,
child: Container(
color: Colors.deepOrangeAccent,
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Expanded(
flex: 2,
child: FittedBox(
child: Text("line 1"),
),
),
Expanded(
flex: 3,
child: Column(
children: [
Expanded(
child: Row(
children: const [
Expanded(
child: Text(
"this line might overflow",
overflow: TextOverflow.ellipsis,
),
),
AspectRatio(
aspectRatio: 1,
child: FittedBox(
child: Icon(Icons.arrow_right)),
),
],
)),
Expanded(
child: Row(
children: const [
Expanded(
child: Text(
"this line is overflow, the icon is in right position",
overflow: TextOverflow.ellipsis,
),
),
AspectRatio(
aspectRatio: 1,
child: FittedBox(
child: Icon(Icons.arrow_right)),
),
],
)),
],
),
),
],
),
),
),
Expanded(
flex: 3,
child: Container(
color: Colors.deepPurpleAccent,
),
),
],
),
),
],
),
),
],
),
);
}
}
注意:此解决方案完全响应。
终于,Flexible 可以了!!!
******* 尝试这个 ********
Text("Any Text Add",
textScaleFactor: 0.85,
style: TextStyle(
fontSize: 12
),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.