简体   繁体   English

flutter:有没有一种简单的方法来布局我的应用栏?

[英]flutter: is there an easy way to layout my app bar?

I'm new to flutter so please bear with me.我是新手,所以请多多包涵。 When I implement an app bar , sometimes it only has a title, some other time it has a title and returns button on the left of the bar , also, sometimes it adds another button on the right of the bar .当我实现一个app bar ,有时它只有一个标题,有时它有一个标题并在bar的左侧返回按钮,有时它会在bar的右侧添加另一个按钮。 I have to layout differently to suit different situations which are quite troublesome.我必须以不同的方式布局以适应不同的情况,这很麻烦。 Is there a convenient widget that provides three optional properties to allow me to set my title , left button , and right button or any good layout strategy?有没有一个方便的小部件,它提供了三个可选的属性,让我可以设置我的titleleft buttonright button或任何好的布局策略? What I have done is below.我所做的如下。

AppBar(
            backgroundColor: Colors.gray,
            elevation: 0.0,
            title: Container(
              alignment: Alignment.bottomCenter,
              child: Container(
                margin: EdgeInsets.only(bottom: ScreenUtil.dp(11)),
                height: ScreenUtil.dp(22),
                width: ScreenUtil.dp(160),
                child: Text(
                    'title',
                    style: TextStyle(
                        fontSize: ScreenUtil.sp(17), fontFamily: FontFamily.family, color: Colors.black, fontWeight: FontWeight.w600
                    )
                ),
                alignment: Alignment.center,
              ),
            ),
          ),
        ),
```

You should learn more about the Appbar with other properties to assist with the things you need like leading, trailing, ...您应该了解有关Appbar和其他属性的更多信息,以帮助您处理所需的内容,例如前导、尾随...

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: _title,
      home: MyStatelessWidget(),
    );
  }
}

final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
final SnackBar snackBar = const SnackBar(content: Text('Showing Snackbar'));

void openPage(BuildContext context) {
  Navigator.push(context, MaterialPageRoute(
    builder: (BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Next page'),
        ),
        body: const Center(
          child: Text(
            'This is the next page',
            style: TextStyle(fontSize: 24),
          ),
        ),
      );
    },
  ));
}

/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
  MyStatelessWidget({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      appBar: AppBar(
        leading: Icon(Icons.navigate_next),
        title: const Text('AppBar Demo'),
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.add_alert),
            tooltip: 'Show Snackbar',
            onPressed: () {
              scaffoldKey.currentState.showSnackBar(snackBar);
            },
          ),
          IconButton(
            icon: const Icon(Icons.navigate_next),
            tooltip: 'Next page',
            onPressed: () {
              openPage(context);
            },
          ),
        ],
      ),
      body: const Center(
        child: Text(
          'This is the home page',
          style: TextStyle(fontSize: 24),
        ),
      ),
    );
  }
}

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

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