这是我的全部flutter代码,你看下为什么字体没有根据textStyle而改变大小:import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return ScreenUtilInit( designSize: const Size(240, 320), minTextAdapt: true, splitScreenMode: true, builder: (context, child) { return myApp(); }, ); } } class myApp extends StatelessWidget { const myApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'ListView widget', theme: ThemeData( textTheme: TextTheme( bodyLarge: TextStyle(fontSize: 50), )), home: Scaffold( body: Container( color: Colors.white, child: SafeArea( child: Column( children: [ Image.asset( 'lib/images/title.png', width: ScreenUtil().setWidth(240), fit: BoxFit.contain, ), Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Image.asset( 'lib/images/aside.png', width: 30, height: ScreenUtil().setWidth(288), fit: BoxFit.cover, alignment: Alignment.topLeft, ), Expanded(flex: 1, child: middleContainer()), Image.asset( 'lib/images/aside.png', width: 30, height: ScreenUtil().setWidth(288), fit: BoxFit.cover, alignment: Alignment.topRight, ), ], ) ], ), ))), ); } } class middleContainer extends StatelessWidget { const middleContainer({super.key}); @override Widget build(BuildContext context) { return Container( height: ScreenUtil().setWidth(288), color: const Color(0xff010042), child: Column(children: [ Expanded( flex: 274, child: Container( color: Colors.amber, )), Expanded( flex: 45, child: Container( color: Color.fromARGB(255, 209, 188, 123), )), Expanded( flex: 60, child: Container( child: Stack( children: [ Positioned( child: Text( '返回', ), bottom: ScreenUtil().setWidth(5), left: ScreenUtil().setWidth(10), ) ], ), color: Color.fromARGB(255, 85, 105, 104), )), ]), ); } }
时间: 2024-01-22 10:19:07 浏览: 28
你在 MyApp 的 theme 中定义了一个名为 bodyLarge 的 TextStyle ,但是你没有在代码中指定使用这个 TextStyle。你可以尝试将你的 Text 组件的 style 属性设置为 bodyLarge,例如:
```
Text(
'返回',
style: Theme.of(context).textTheme.bodyLarge,
),
```
这样,你的 Text 组件就会使用你定义的 bodyLarge TextStyle。另外,由于你在 ScreenUtilInit 中设置了 minTextAdapt 为 true,所以你还需要确保你的 Text 组件的 fontSize 没有超过系统最小字号,否则字体大小不会自动适应屏幕。你可以通过在 ScreenUtilInit 中设置 minTextAdapt 值为 false 来关闭最小字号限制。
相关问题
为什么flutter3.x开发的应用在我的手机上字体总是被默认加粗
可能是因为您的手机设置了字体加粗的全局选项。您可以尝试在您的手机设置中找到“字体大小和样式”或“显示”选项,然后查看是否有“加粗字体”或“粗体文本”选项,如果有,请关闭它。如果您的手机没有这个选项,您可以尝试在应用的代码中设置字体的样式,例如使用`TextStyle`来设置字体的大小和粗细。例如:
```dart
Text(
'Hello World',
style: TextStyle(
fontWeight: FontWeight.normal, // 设置字体样式为正常
fontSize: 16.0,
),
);
```
如果您的应用中的所有文本都被默认加粗,您也可以尝试在应用的主题中设置全局的字体样式,例如:
```dart
MaterialApp(
theme: ThemeData(
textTheme: TextTheme(
bodyText2: TextStyle(
fontWeight: FontWeight.normal, // 设置字体样式为正常
),
),
),
home: MyHomePage(),
);
```
希望这些方法能够帮助您解决问题。
Flutter自带的字体是什么?如何使用
### 回答1:
Flutter默认的字体是Roboto,可以使用MaterialApp的themeData属性来设置字体,例如:MaterialApp(themeData: ThemeData(fontFamily: 'Roboto'))
### 回答2:
Flutter自带的字体是Roboto字体。Roboto是Google设计的一种现代无衬线字体,具有良好的可读性和适用性。
在Flutter中,可以通过在控件的style属性中设置字体来使用Roboto字体。例如,可以使用TextStyle来定义文本的样式,其中的fontFamily属性可以指定使用的字体名称。要使用Roboto字体,只需将fontFamily设置为"Roboto"即可。
以下是一个在Flutter中使用Roboto字体的示例代码:
```
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Roboto Font Example'),
),
body: Center(
child: Text(
'Hello, Flutter!',
style: TextStyle(
fontFamily: 'Roboto',
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
),
));
}
```
在上面的例子中,Text控件使用了Roboto字体,样式由TextStyle定义。属性fontFamily被设置为"Roboto",使得文本使用了该字体。
需要注意的是,如果要在Flutter中使用其他非自带的字体,需要先在项目中添加字体文件,并在pubspec.yaml文件中进行配置。然后,可以在TextStyle中使用该字体。
### 回答3:
Flutter自带的字体是Roboto字体。在Flutter中,可以通过以下步骤来使用它:
1. 在pubspec.yaml文件中,添加字体的配置。找到“fonts”部分,如果没有就手动添加它。例如:
```
flutter:
fonts:
- family: Roboto
fonts:
- asset: fonts/Roboto-Regular.ttf
- asset: fonts/Roboto-Bold.ttf
weight: 700
```
2. 在项目中创建一个“fonts”文件夹,并将字体文件(.ttf或.otf)放在其中。上面的例子中,我们将Roboto-Regular.ttf和Roboto-Bold.ttf放在fonts文件夹中。
3. 使用字体。在代码中,你可以使用TextStyle来指定使用Roboto字体。例如:
```dart
Text(
'Hello, Flutter!',
style: TextStyle(
fontFamily: 'Roboto',
fontSize: 18.0,
fontWeight: FontWeight.w400,
),
)
```
在这个例子中,我们指定了字体系列为"Roboto",字体大小为18,以及字体粗细为400。
通过以上步骤,你可以在Flutter中使用自带的Roboto字体来呈现文本。如果有其他需要,如使用其他字体、自定义字体大小和样式等,也可以通过类似的步骤来实现。当然,在使用自定义的字体时,需要将字体文件放入“fonts”文件夹,并在pubspec.yaml文件中进行相应的配置。