首页 > 解决方案 > 系统状态下的 Flutter 容器

问题描述

flutter默认情况下,当我们在系统状态下使用该AppBar显示时,例如:

appBar: AppBar(
  title: Text(
    Strings.appName,
    style: appBarTextStyle,
  ),
  automaticallyImplyLeading: true,
  leading: Builder(
    builder: (context) => IconButton(
          icon: Icon(Icons.menu),
          onPressed: () => Scaffold.of(context).openDrawer(),
        ),
  ),
),

现在,在我的应用程序中,我没有AppBar,我想Container在这个系统状态下拥有,但我不能这样做

在此处输入图像描述

我的实现:

child: Scaffold(
  floatingActionButton: FloatingActionButton(
    child: Icon(Icons.add),
    onPressed: () {},
  ),
  floatingActionButtonLocation:
  FloatingActionButtonLocation.centerDocked,
  bottomNavigationBar: new BottomAppBar(
    shape: CircularNotchedRectangle(),
    child: Container(
      height: 50.0,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 40.0),
        child: Row(
          ...
        ),
      ),
    ),
  ),
  drawer: AppDrawer(),
  body: Column(
    children: <Widget>[
      Container(
        decoration: BoxDecoration(boxShadow: [
          BoxShadow(
            color: Colors.indigo,
          )
        ]),
        height: 70.0,
        child: Row(
          children: <Widget>[
            Center(child: Text("TOOLBAR", style: defaultAppBarTextStyle)),
          ],
        ),
      ),
      Expanded(child: _fragments[_currentIndex]),
    ],
  ),

标签: flutterflutter-layoutflutter-appbarflutter-container

解决方案


使用 SafeArea 避免将项目保留在状态栏上。

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: 
              child: Text(
                _timeRemaining.toString(),
                style: TextStyle(fontSize: 100),
              ),
          ),
      ),
    );
  }

在这种情况下,您需要使用https://pub.dev/packages/flutter_statusbarcolor手动更改状态栏颜色。

@override
  Widget build(BuildContext context) {
    FlutterStatusbarcolor.setStatusBarColor(Colors.blue);
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }

推荐阅读