如何使用自定义appbar正确放置主体和抽屉?

时间:2019-10-07 15:54:10

标签: android flutter

我尝试使appbar具有透明的模糊效果,like this 我的操作方式:

return Stack(children: <Widget>[
Scaffold(
  key: _scaffoldKey,
  backgroundColor: Theme.of(context).backgroundColor,
  body: Center(
    child: buildProcurementList(),
  ),
),
Positioned(
  top: 0.0,
  left: 0.0,
  right: 0.0,
  child: ClipRect(
    child: BackdropFilter(
      filter: ui.ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
      child: Container(
        child: AppBar(
          backgroundColor: Colors.transparent,
          title: Text(
            'Title',
          ),
          centerTitle: true,
        ),
        decoration:
            BoxDecoration(color: Colors.blue.shade200.withOpacity(0.5)),
      ),
    ),
  ),
),

我得到了预期的效果,但是由于堆栈,主体和抽屉固定在顶部。告诉我一种添加填充物的方式,就像普通的脚手架一样,为我的应用栏增加抽屉大小

what i get with bodyand drawer

1 个答案:

答案 0 :(得分:0)

第1步:将堆栈移动到脚手架的身体上
第2步:将定位的应用栏放置在堆叠的小部件的最后一个

完整代码

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: ListenToDrawerEvent(),
    );
  }
}

class ListenToDrawerEvent extends StatefulWidget {
  @override
  ListenToDrawerEventState createState() {
    return new ListenToDrawerEventState();
  }
}

class ListenToDrawerEventState extends State<ListenToDrawerEvent> {
  GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  static final List<String> _listViewData = [
    "Inducesmile.com",
    "Flutter Dev",
    "Android Dev",
    "iOS Dev!",
    "React Native Dev!",
    "React Dev!",
    "express Dev!",
    "Laravel Dev!",
    "Angular Dev!",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      /*appBar: AppBar(
        title: Text("Listen to Drawer Open / Close Example"),
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: () {
            _scaffoldKey.currentState.openDrawer();
          },
        ),
      ),*/
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.all(10.0),
          children: _listViewData
              .map((data) => ListTile(
                    title: Text(data),
                  ))
              .toList(),
        ),
      ),
      body: Stack(
        children: <Widget>[
          Center(
            child: ListView.builder(
              itemCount: 30,
              itemBuilder: (context, index) {
                return Container(
                  color: Colors.primaries[index % Colors.primaries.length]
                      .withOpacity(0.5),
                  child: ListTile(
                    title: Text('Item $index'),
                  ),
                );
              },
            ),
          ),
          Positioned(
            top: 0.0,
            left: 0.0,
            right: 0.0,
            child: ClipRect(
              child: BackdropFilter(
                filter: ui.ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
                child: Container(
                  child: AppBar(
                      backgroundColor: Colors.transparent,
                      title: Text(
                        'Title',
                      ),
                      centerTitle: true,
                      actions: <Widget>[
                        // action button
                        IconButton(
                          icon: Icon(Icons.access_alarm),
                          onPressed: () {
                            //_select(choices[0]);
                          },
                        ),
                        // action button
                        IconButton(
                          icon: Icon(Icons.memory),
                          onPressed: () {
                            //_select(choices[1]);
                          },
                        ),
                      ]),
                  decoration: BoxDecoration(
                      color: Colors.blue.shade200.withOpacity(0.5)),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

enter image description here