【问题标题】:How to get simple text on screen without Scafflod in Flutter?如何在 Flutter 中没有脚手架的情况下在屏幕上获取简单的文本?
【发布时间】:2018-07-31 17:08:06
【问题描述】:

我已经从 Scafflod 和按钮中清理了 Flutter 示例,并希望我得到相同的内容,但没有标题栏和按钮:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'My app title',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);


  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  @override
  Widget build(BuildContext context) {
    return new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      );

  }
}

不幸的是,我得到的东西不适合任何门:

这是什么?怎么会变成这么大?如何解决?

【问题讨论】:

标签: android-emulator dart flutter scaffolding


【解决方案1】:

您可以将文本包装在“材料”小部件中。另外,使用“style:”参数可以解决问题。

Material(
        color: Colors.transparent,
        child: Text(
            "Your Text Here",
            style: TextStyle(
              fontFamily: 'custom font', // remove this if don't have custom font
              fontSize: 20.0, // text size
              color: Colors.white, // text color
            ),
        ),
      )

【讨论】:

    【解决方案2】:

    Scaffold正在设置基本的material design视觉布局结构,其中很多包括默认的文本样式等。如果您出于某种原因不想使用Scaffold,那么您可以应用相关的文本样式和颜色从当前主题使用Theme.of(context)。例如:

    Text(
      'You have pushed the button this many times:',
      style: Theme.of(context).primaryTextTheme.headline,
      textAlign: TextAlign.center,
    ),
    

    【讨论】:

    • 我应该为任何Text 元素写这个吗?有没有像 HTML 那样的继承?
    • 您可以使用DefaultTextStyle 小部件来定义您想要使用的基本文本样式,然后任何后代Text 元素都将使用该样式。
    【解决方案3】:
    Text(
      'Sign in / Sign up with',
      style: TextStyle(
        fontFamily: appFontFamily,
        fontSize: 20,
        decoration: TextDecoration.none,////set decoration to .none
        fontWeight: FontWeight.bold,
        color: defaultTitleColor,
      ),
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 2021-11-19
      • 2014-03-17
      • 2013-08-26
      • 1970-01-01
      • 2013-05-19
      相关资源
      最近更新 更多