【问题标题】:How to center AppBar and how to reduce leading icon size in PrefrerredSize?如何使 AppBar 居中以及如何减小 PrefrerredSize 中的领先图标大小?
【发布时间】:2019-12-03 11:54:45
【问题描述】:

如何将整个AppBar居中以及如何减少前导图标?

我尝试使用 Center 小部件和 Column 使用 mainAxisAlignment.center 不工作。 我尝试将 widthheight 添加到前导图标容器中。但没有任何效果

appBar: PreferredSize(
    child: AppBar(
        leading: Container(
            decoration: BoxDecoration(..),
            child: Icon(..),
        ),
        title: TextFormField(
            ...
        ),
        actions: <Widget>[
            Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                    Text(
                        ...
                    ),
                    CupertinoSwitch(
                        ...
                    )
                ],
            )
        ],
    ),
preferredSize: Size.fromHeight(80.0)),

As shown here.

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    作为一种选择

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            appBar: AwesomeAppBar(height: 80),
          ),
        );
      }
    }
    
    class AwesomeAppBar extends PreferredSize {
      final double height;
    
      const AwesomeAppBar({Key key, @required this.height});
    
      @override
      Widget build(BuildContext context) {
        return LayoutBuilder(builder: (context, snapshot) {
          return Container(
            padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
            height: height,
            color: Theme.of(context).primaryColor,
            child: Row(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(16),
                  child: Icon(
                    Icons.arrow_back,
                    color: Colors.white,
                  ),
                ),
                Expanded(
                  child: Container(
                    height: 32,
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.symmetric(horizontal: 16),
                    margin: EdgeInsets.only(right: 16),
                    decoration: ShapeDecoration(
                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
                      color: Colors.white,
                    ),
                    child: Text('Search'),
                  ),
                ),
                SwitchWithText(),
                SizedBox(width: 16),
              ],
            ),
          );
        });
      }
    
      @override
      Size get preferredSize => Size.fromHeight(height);
    }
    
    class SwitchWithText extends StatefulWidget {
      @override
      _SwitchWithTextState createState() => _SwitchWithTextState();
    }
    
    class _SwitchWithTextState extends State<SwitchWithText> {
      @override
      Widget build(BuildContext context) {
        return Row(
          children: <Widget>[
            Text('Online', style: TextStyle(color: Colors.white)),
            CupertinoSwitch(
              value: true,
              onChanged: (b) {},
              activeColor: Colors.lightBlueAccent,
            ),
          ],
        );
      }
    }
    

    【讨论】:

    • 你可以随意设置图标大小
    • here里面不能用setState()吧?将其用于switch 的原因
    猜你喜欢
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多