简体   繁体   English

Flutter中文本小部件下的黄线?

[英]Yellow lines under Text Widgets in Flutter?

The main app screen doesn't have this issue, all the texts show up as they should.主应用程序屏幕没有此问题,所有文本均按应有的方式显示。

However, in the new screen, all the text widget have some weird yellow line / double-line underneath.但是,在新屏幕中,所有文本小部件下面都有一些奇怪的黄线/双线。

Any ideas on why this is happening?关于为什么会发生这种情况的任何想法?

黄线

The problem is having a Scaffold or not.问题是有没有Scaffold Scaffold is a helper for Material apps ( AppBar , Drawer , that sort of stuff). ScaffoldMaterial应用程序( AppBarDrawer之类的)的助手。 But you're not forced to use Material .但是您不必被迫使用Material

What you're missing is an instance of DefaultTextStyle as a parent:您缺少的是作为父级的DefaultTextStyle实例:

DefaultTextStyle(
  style: TextStyle(...),
  child: Text('Hello world'),
)

Various widgets add one to change the default text theme, such as Scaffold, Dialog, AppBar, ListTile, ...各种小部件添加一个以更改默认文本主题,例如 Scaffold、Dialog、AppBar、ListTile、...

It's DefaultTextStyle that allows your app-bar title to be bold by default for example.例如,它是DefaultTextStyle ,它允许您的应用栏标题默认为粗体。

Add Material widget as root element.Material小部件添加为根元素。

@override
  Widget build(BuildContext context) {
    return Material(
        type: MaterialType.transparency,
        child: new Container(

All you need to do is provide a Material widget, or a Scaffold which internally covers this widget.您需要做的就是提供一个Material小部件,或者一个内部覆盖该小部件的Scaffold You can do that in the following ways:您可以通过以下方式做到这一点:

  • Use Material (simple and better):使用Material (简单且更好):

     Material( color: Colors.transparent, // <-- Add this, if needed child: Text('Hello'), )
  • Set Text.style property:设置Text.style属性:

     Text( 'Hello', style: TextStyle(decoration: TextDecoration.none), // Set this )
  • Provide Scaffold :提供Scaffold

     Scaffold(body: Text('Hello'))

Fixing yellow text issues when using Hero :修复使用Hero时的黄色文本问题

As aaronvargas mentioned, you can wrap your child in Material when using Hero on both sides.正如aaronvargas 所提到的,您可以在两侧使用Hero时将您的child包裹在Material中。 For example:例如:

Hero(
  tag: 'fooTag',
  child: Material( // <--- Provide Material
    type: MaterialType.transparency,
    child: YourWidget(),
  ),
);

Text style has a decoration argument that can be set to none文本样式有一个可以设置为无的装饰参数

Text("My Text",
  style: TextStyle(
    decoration: TextDecoration.none,
  )
);

Also, as others have mentioned, if your Text widget is in the tree of a Scaffold or Material widget you won't need the decoration text style.此外,正如其他人所提到的,如果您的 Text 小部件位于 Scaffold 或 Material 小部件的树中,则您不需要装饰文本样式。

您也可以使用装饰:TextDecoration.none 删除下划线

Just adding another way I encounter to these answers.只是在这些答案中添加我遇到的另一种方式。

Wrap the root Widget around a DefaultTextStyle widget.将根 Widget 包裹在DefaultTextStyle小部件周围。 Altering each Text widget is not a necessity here.在这里更改每个 Text 小部件不是必需的。

DefaultTextStyle(
    style: TextStyle(decoration: TextDecoration.none), 
    child : Your_RootWidget
)

Hope it helps someone.希望它可以帮助某人。

Inside your Text Widget , use the style property and set decoration to null like shown:Text Widget ,使用style属性并将decoration设置为null如下所示:

Text( 
   "hello world",
   style: TextStyle(
       decoration: TextDecoration.none
   ),
)

This will remove the yellow lines under the text.这将删除文本下方的黄线。

You should add Material and Scaffold widgets in main.dart file您应该在 main.dart 文件中添加 Material 和 Scaffold 小部件

 MaterialApp(
  home: Scaffold(
    body: Text('Hello world'),
  ),
);

2 ways is avalible: 2种方式可用:

use scaffuld in parent of screen在屏幕的父级中使用脚手架

Scaffold(body: Container(child:Text("My Text")))

use material for parent of widget使用材质作为小部件的父级

Material(child: Text("My Text"))

You just need to add Material root widget.您只需要添加 Material 根小部件。

      @override
       Widget build(BuildContext context) {
      return Material(
         child: new Container(),
        );
       }

There is an other solution for this , especially if you are using multiple pages wrapped under main.dart file You can do something like this:对此还有其他解决方案,特别是如果您使用包含在main.dart文件下的多个页面,您可以执行以下操作:

  child: MaterialApp(
    home: Material(child: Wrapper()),
  ),

This will remove the yellow lines under text that is present in any pages referenced/used under wrapper.这将删除在包装器下引用/使用的任何页面中存在的文本下的黄线。

problem : your widget doesn't have default text style,问题:您的小部件没有默认文本样式,

solution : wrap it in one!解决方案:把它包在一个!

DefaultTextStyle(
    style: TextStyle(),
    child: yourWidget,
  );

remember if you dont set any color, default text color is white!请记住,如果您不设置任何颜色,则默认文本颜色为白色!

The yellow lines come from _errorTextStyle .黄线来自_errorTextStyle The documentation states that you should define a DefaultTextStyle parent (or use Material , which does this for you):文档说明您应该定义一个DefaultTextStyle父级(或使用Material ,它会为您执行此操作):

MaterialApp uses this TextStyle as its DefaultTextStyle to encourage developers to be intentional about their DefaultTextStyle. MaterialApp 使用这个 TextStyle 作为它的 DefaultTextStyle 来鼓励开发者注意他们的 DefaultTextStyle。

In Material Design, most Text widgets are contained in Material widgets, which sets a specific DefaultTextStyle.在 Material Design 中,大多数 Text 小部件都包含在 Material 小部件中,它设置了特定的 DefaultTextStyle。 If you're seeing text that uses this text style, consider putting your text in a Material widget (or another widget that sets a DefaultTextStyle).如果您看到使用此文本样式的文本,请考虑将您的文本放入 Material 小部件(或另一个设置 DefaultTextStyle 的小部件)中。

Developing Flutter apps without material is not something most people do, but if that's your use case, you should use DefaultTextStyle .大多数人不会在没有材料的情况下开发 Flutter 应用程序,但如果这是你的用例,你应该使用DefaultTextStyle

Contrary to the accepted answer, Theme does not set a DefaultTextStyle , so it does not solve your problem.与接受的答案相反, Theme没有设置DefaultTextStyle ,因此它不能解决您的问题。 Scaffold does solve the problem, as it contains Material , which in turn defines DefaultTextStyle , but Scaffold is a bit more than you need for a Dialog , Hero , etc. Scaffold确实解决了这个问题,因为它包含Material ,它又定义了DefaultTextStyle ,但是Scaffold比你需要的DialogHero等要多一点。

To permanently solve this for your whole app, you could set the DefaultTextStyle in your MaterialApp builder .要为您的整个应用永久解决此问题,您可以在MaterialApp builder中设置DefaultTextStyle This solves the issue for all the components of your app, not just the current screen you're working on.这解决了应用程序所有组件的问题,而不仅仅是您正在处理的当前屏幕。

Before

在此处输入图像描述

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Center(
          child: Text(
            "21:34",
            style: TextStyle(fontSize: 50),
          ),
        ),
        Center(
          child: Text(
            "Wakey - wakey",
            style: TextStyle(fontSize: 20),
          ),
        )
      ],
    );
  }

After (Solution):之后(解决方案):

Here Wrap the current top or parent widget with Scaffold widget这里用Scaffold小部件包裹当前顶部或父小部件

在此处输入图像描述

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(
            child: Text(
              "21:34",
              style: TextStyle(fontSize: 50),
            ),
          ),
          Center(
            child: Text(
              "Wakey - wakey",
              style: TextStyle(fontSize: 20),
            ),
          )
        ],
      ),
    );
  }

Full code: Dartpad or Live code完整代码: Dartpad 或 Live 代码

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: sta()));

class sta extends StatefulWidget {
  const sta({Key? key}) : super(key: key);

  @override
  State<sta> createState() => _staState();
}

var isShow = false;


class _staState extends State<sta> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(
            child: Text(
              "21:34",
              style: TextStyle(fontSize: 50),
            ),
          ),
          Center(
            child: Text(
              "Wakey - wakey",
              style: TextStyle(fontSize: 20),
            ),
          )
        ],
      ),
    );
  }
}

You can also have your scaffold as the home for your MaterialApp.您还可以将脚手架作为 MaterialApp 的家。 This worked for me.这对我有用。

return MaterialApp(
  home: Scaffold(
    body: Container(
      child: SingleChildScrollView(child: Text('Test')),
    ),
  ),
);

I recommend this approach because you could do it once and it will override your whole app.我推荐这种方法,因为你可以这样做一次,它会覆盖你的整个应用程序。

Add DefaultTextStyle under builder of our MaterialApp like so:MaterialAppbuilder下添加DefaultTextStyle ,如下所示:

child: MaterialApp(      
  ...
  ...
  theme: yourThemeData,
  builder: (context, child) => DefaultTextStyle(
    style: yourThemeData.textTheme.bodyText1,
    child: child,
  ),
),

By doing so we don't need to specify a style or having DefaultTextTheme every time we want to use showDialog or Overlay .通过这样做,我们不需要在每次想要使用showDialogOverlay时指定style或使用DefaultTextTheme

The text has a hidden default text style .The problem arises because you can't provide this to any parent widget like Scaffold .该文本具有隐藏的默认文本样式。出现问题是因为您无法将其提供给任何父小部件,例如Scaffold Text widget takes the default style.文本小部件采用默认样式。 for your solution either you can change the DefaultTextStyle like this.对于您的解决方案,您可以像这样更改DefaultTextStyle

DefaultTextStyle(
    style: TextStyle(),
    child: yourTextWidget,
  );

or just wrap into Scaffold, Scaffold is a widget.或者只是包装成 Scaffold,Scaffold 是一个小部件。 that provides scaffolding for pages in your app.为您的应用程序中的页面提供脚手架。 like this像这样

 MaterialApp(
  home: Scaffold(
    body: Text('Wakey Wakey!'),
  ),
);

for more info just walk through this Flutter Official video.欲了解更多信息,请浏览这个 Flutter 官方视频。

Yellow underline text |黄色下划线文字 | Decoding Flutter解码颤振

Use a DefaultTextStyle() widget to set the textstyle of your text.使用 DefaultTextStyle() 小部件来设置文本的文本样式。

DefaultTextStyle(
  style: kTempTextStyle,
  child: Text('Hello world'),
)

you can give your own textstyle as you want.您可以根据需要提供自己的文本样式。

const kTempTextStyle = TextStyle(
  fontFamily: 'Spartan MB',
  fontSize: 100.0,
  decoration: null,
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM