Expand The App bar in Flutter to Allow Multi-Line Title?

前端 未结 6 957
名媛妹妹
名媛妹妹 2021-02-04 02:28

Does anyone know how I can create an app bar with a multi-line title, as per the material guidelines show here?

https://material.io/design/components/app-bars-top.html#

6条回答
  •  难免孤独
    2021-02-04 03:17

    This piece of code will create a custom Scaffold with an AppBar that supports receiving no title, a title, and a title and a subtitle. In case you don't provide a title, it will show a given text (in the example, the name of the app), while in case you set a title and a subtitle, it will use a two lines style with the proper Material Design text style.

    import 'package:flutter/material.dart';
    
    class TwoLinesAppBarScaffold extends StatelessWidget {
      final Widget body;
      final String title;
      final String subtitle;
    
      TwoLinesAppBarScaffold({this.body, this.title = "QuitNow!", this.subtitle});
    
      @override
      Widget build(BuildContext context) {
        Widget widget;
    
        if (subtitle == null) {
          widget = Text(title);
        } else {
          widget = RichText(
            textAlign: TextAlign.start,
            text: TextSpan(
                text: title,
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.w500,
                ),
                children: [
                  TextSpan(
                    text: '\n$subtitle',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.normal,
                    ),
                  ),
                ]),
          );
        }
    
        return Scaffold(
            appBar: AppBar(
              title: widget,
            ),
            body: Center(child: body));
      }
    }
    

提交回复
热议问题