【问题标题】:How can I make the padding of a widget align with the alignment of a row?如何使小部件的填充与行的对齐方式对齐?
【发布时间】:2022-07-29 08:03:30
【问题描述】:

我有以下小部件结构,其中第一行内的 3 个填充是按钮。这些按钮使用 MainAxisAlignment.spaceEvenly 对齐。是否有可能使卡片适合按钮范围?

这是它的实际显示方式,我想在左侧的卡片位于行两端的按钮之间的位置实现结果。该图像是使用卡上的固定填充拍摄的,这是正确的模拟器未对齐的方式。

【问题讨论】:

    标签: flutter dart user-interface


    【解决方案1】:

    使用MainAxisAlignment.spaceBetween 代替MainAxisAlignment.spaceEvenly 并将水平填充设置为与按钮下方的卡片相同。

    会是这样(也请查看live demo on DartPad):

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    const defaultColor = Color.fromARGB(255, 68, 155, 162);
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            appBarTheme: const AppBarTheme(
              centerTitle: false,
              backgroundColor: Color.fromARGB(255, 68, 155, 162),
            ),
          ),
          home: const MyHomePage(),
          debugShowCheckedModeBanner: false,
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      const MyHomePage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('iPV HealthPlus'),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              const SizedBox(height: 16),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 32),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    buildIntervalButton('Diario', selected: true),
                    buildIntervalButton('Semanal', selected: false),
                    buildIntervalButton('Mensal', selected: false),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(32, 16, 32, 16),
                child: Container(
                  height: 100,
                  decoration: BoxDecoration(
                    color: defaultColor,
                    borderRadius: BorderRadius.circular(12),
                  ),
                ),
              )
            ],
          ),
        );
      }
    
      Widget buildIntervalButton(
        String text, {
        required bool selected,
      }) {
        return SizedBox(
          height: 40,
          child: ElevatedButton(
            onPressed: () {},
            style: ButtonStyle(
                elevation: MaterialStateProperty.all(0),
                backgroundColor: MaterialStateProperty.all(
                    selected ? defaultColor : Colors.white),
                foregroundColor: MaterialStateProperty.all(
                    selected ? Colors.white : defaultColor),
                shape: MaterialStateProperty.all(
                  RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20),
                      side: const BorderSide(color: defaultColor)),
                )),
            child: Text(text),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 2023-02-21
      • 1970-01-01
      • 2020-07-26
      • 2020-02-21
      • 2020-10-08
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多