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

6,100

Solution 1

Generally I would recommend using a Block as the child of the Scaffold. It has a padding value that you can set.

You can also just use a Padding widget.

Solution 2

I only know ButtonTheme that have a padding value

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.

Maybe I am wrong but I don't know any Widget that automatically apply any Material padding.

Share:
6,100
Admin
Author by

Admin

Updated on December 01, 2022

Comments

  • Admin
    Admin over 1 year

    I have written a Flutter application that makes use of package:flutter/material.dart. Running the app on the iOS Simulator looks as follows. 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. Would I pack my components in a Container and apply padding there?

    Thank you very much in advance.

    Working Flutter application

    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. I merely want to do it right. ;-)