简体   繁体   English

如何在Flutter脚手架上应用符合Material Design的填充?

[英]How do I apply Material Design-compliant padding to my Flutter Scaffold?

I have written a Flutter application that makes use of package:flutter/material.dart . 我编写了一个Flutter应用程序,该应用程序使用package:flutter/material.dart Running the app on the iOS Simulator looks as follows. 在iOS模拟器上运行该应用程序如下所示。 As you can see, there is no padding between components in one row, and the components reach to top, bottom, left and right without padding/margin/border. 如您所见,一行中的组件之间没有填充,并且组件在没有填充/边距/边框的情况下到达顶部,底部,左侧和右侧。 My question is: 我的问题是:

What is the recommended way to apply Material-compliant padding, for example for the label-component-gap between Convert to and the dropdown button. 建议采用哪种方法来应用与材料兼容的填充,例如用于“转换为”和下拉按钮之间的label-component-gap。 Would I pack my components in a Container and apply padding there? 我可以将组件包装在容器中并在其中应用填充吗?

Thank you very much in advance. 提前非常感谢您。

工作Flutter应用程序

This is the application code: 这是应用程序代码:

import 'package:flutter/material.dart';
import 'converter.dart';
import 'model.dart';

const _appName = 'Temperature Converter';

void main() {
  runApp(new MyApp());
}

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

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  final Model model = new Model();
  var _currentInput = const InputValue();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(config.title),
      ),
      body: new Column(children: <Widget>[
        new Row(children: <Widget>[
          new Expanded(
              child: new Input(
                  hintText: 'Temperature',
                  value: _currentInput,
                  onChanged: (input) => _currentInput = input)),
          new DropdownButton(
              items: createUnit(),
              onChanged: (temperatureUnit newValue) {
                setState(() {
                  model.inUnit = newValue;
                });
              },
              value: model.inUnit)
        ]),
        new Row(children: <Widget>[
          new Text("Convert to"),
          new DropdownButton(
              items: createUnit(),
              onChanged: (temperatureUnit newValue) {
                setState(() {
                  model.outUnit = newValue;
                });
              },
              value: model.outUnit),
        ]),
        new FlatButton(
            child: new Text('Calculate'),
            onPressed: () {
              setState(() {
                double inTemp = stringToDouble(_currentInput.text);
                model.inTemperature = inTemp;
                model.calculateOutTemperature();
              });
            }),
        new Text(model.outTemperatureAsString)
      ]), // This trailing comma tells the Dart formatter to use
      // a style that looks nicer for build methods.
    );
  }

  List<DropdownMenuItem> createUnit() {
    var l = new List<DropdownMenuItem<temperatureUnit>>();
    // degrees Celsius
    l.add(new DropdownMenuItem<temperatureUnit>(
        value: temperatureUnit.degreesCelsius,
        child: new Text(unitDegreesCelsius)));
    // degrees Fahrenheit
    l.add(new DropdownMenuItem<temperatureUnit>(
        value: temperatureUnit.degreesFahrenheit,
        child: new Text(unitDegreesFahrenheit)));
    // Kelvin
    l.add(new DropdownMenuItem<temperatureUnit>(
        value: temperatureUnit.kelvin, child: new Text(unitKelvin)));
    return l;
  }
}

The question is by no means implying that Flutter is missing something. 问题绝不是暗示Flutter缺少了某些东西。 I merely want to do it right. 我只是想做对。 ;-) ;-)

Generally I would recommend using a Block as the child of the Scaffold. 通常,我建议使用Block作为脚手架的子代。 It has a padding value that you can set. 它具有您可以设置的padding值。

You can also just use a Padding widget. 您也可以只使用Padding小部件。

I only know ButtonTheme that have a padding value 我只知道ButtonTheme具有填充值

ButtonTheme.of(context).padding

So I think the best solution for now is to have constant value and use a Container to apply your padding. 因此,我认为目前最好的解决方案是保持恒定的值并使用Container来应用填充。

Maybe I am wrong but I don't know any Widget that automatically apply any Material padding. 也许我是错的,但我不知道任何会自动应用任何Material填充的Widget。

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

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