首页 > 解决方案 > Flutter IconButton 删除左侧的填充以与标题文本对齐

问题描述

我想在 Flutter 中实现附图中的布局。 删除图标按钮一侧的填充,使其与其上方的标题文本对齐,如标题和副标题文本。

到目前为止,即使填充边插图设置为零并且盒子约束,它也只是在中间。

 Row(children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(
                      3, 3, 3, 6),
                  child: Container(
                    width: 150,
                    height: 30,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        "Title Sub-title",
                        textAlign: TextAlign.left,
                        maxLines: 2,
                        style: TextStyle(
                          height: 1.2,
                          fontFamily:
                              'Roboto’,
                          fontSize: 12,
                          letterSpacing: 0.15,
                          color: Colors.black,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
            Row(
              mainAxisAlignment:
                  MainAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(
                      0, 10, 0, 3),
                ),
                IconButton(
                  padding: EdgeInsets.zero,
                  constraints: BoxConstraints(),
                  onPressed: () {},
                  icon: Icon(
                    Icons.bookmark_border,
                    size: 20.0,
                    color: Colors.black,
                  ),
                ),
                Text(
                  "Author",
                  textAlign: TextAlign.left,
                  style: TextStyle(
                    height: 1.1,
                    fontFamily: 'Roboto Light',
                    fontSize: 8,
                    color: Colors.black,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    ),
  ],
),

标签: flutterflutter-layoutflutter-dependenciesflutter-test

解决方案


尝试使用列树并根据需要调整填充

import 'package:flutter/material.dart';
import 'package:so_test/screen/child_page.dart';
import 'package:webview_flutter/webview_flutter.dart';

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

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool visible = true;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        home: Scaffold(
          appBar: AppBar(
            title: Text("Test obviously"),
          ),
          body: SingleChildScrollView(
            padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  height: 100,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Colors.green,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(10, 10, 3, 0),
                  child: Container(
                    width: 150,
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        "Title",
                        textAlign: TextAlign.left,
                        maxLines: 2,
                        style: TextStyle(
                          height: 1.2,
                          fontSize: 12,
                          letterSpacing: 0.15,
                          color: Colors.black,
                        ),
                      ),
                    ),
                  ),
                ),
                Container(
                  padding: const EdgeInsets.fromLTRB(10, 10, 3, 0),
                  width: 150,
                  child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      "Sub Title",
                      textAlign: TextAlign.left,
                      maxLines: 2,
                      style: TextStyle(
                        height: 1.2,
                        fontSize: 12,
                        letterSpacing: 0.15,
                        color: Colors.black,
                      ),
                    ),
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    SizedBox(
                      width: 5,
                    ),
                    IconButton(
                      padding: EdgeInsets.zero,
                      constraints: BoxConstraints(),
                      onPressed: () {},
                      icon: Icon(
                        Icons.bookmark_border,
                        size: 20.0,
                        color: Colors.black,
                      ),
                    ),
                    Text(
                      "Author",
                      textAlign: TextAlign.left,
                      style: TextStyle(
                        height: 1.1,
                        fontFamily: 'Roboto Light',
                        fontSize: 8,
                        color: Colors.black,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ));
  }
}

输出: 在此处输入图像描述


推荐阅读