是否可以从Scaffold小部件中的主体传递AppBar?

时间:2018-12-28 10:47:48

标签: flutter flutter-layout

我有一个let dateText = NSMutableAttributedString.init(string: "This is a test string.") dateText.setAttributes([NSAttributedStringKey.font: UIFont(name: "Poppins-Regular", size: 11)!, NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedString.Key.strikethroughStyle: 2], range: NSMakeRange(0, 8)) dateText.setAttributes([NSAttributedStringKey.font: UIFont(name: "Poppins-SemiBold", size: 18)!, NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedString.Key.strikethroughStyle: 4], range: NSMakeRange(10, 13)) dateText.setAttributes([NSAttributedStringKey.font: UIFont(name: "Poppins-SemiBold", size: 15)!, NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedString.Key.strikethroughStyle: 1], range: NSMakeRange(14, 17)) // set the attributed string to the UILabel object deadline_lbl.attributedText = dateText 的{​​{1}}小部件。我需要在Scaffold中显示的屏幕具有不同的drawer(其中一些具有用于body的指示器,其中有些是单页的,有些中有徽标)。 因此,我没有将任何AppBars传递给我的PageView,而是将其包含在我在AppBar中使用的小部件中。但是抽屉里没有汉堡图标。

是否可以将此Scaffold从我的body小部件传递到包含它的AppBar

或者,如果有一种更好的小部件组合方式来解决这种情况,我想尝试一下。

UPD

有一种很好的方法来用

显示body操作系统Scaffold
BottomSheet

不幸的是,SnackBar并非如此。

1 个答案:

答案 0 :(得分:0)

似乎您希望全局Scaffold在切换页面时保持不变。在这种情况下,您必须编写自己的逻辑以使菜单按钮出现:

import 'package:flutter/material.dart';

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

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: (context, child) {
        // wrap a scaffold around the Navigator so that it is the same for all pages
        return Scaffold(
          body: AppScaffold(child: child),
          drawer: Drawer(),
        );
      },
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: buildLeadingAction(context),
        title: Text('Home'),
      ),
    );
  }
}

/// Makes it easier to access the outer scaffold with the drawer from any context
class AppScaffold extends InheritedWidget {
  const AppScaffold({Key key, @required Widget child})
      : assert(child != null),
        super(key: key, child: child);

  /// nullable
  static ScaffoldState of(BuildContext context) {
    return context
        .ancestorInheritedElementForWidgetOfExactType(AppScaffold)
        ?.ancestorStateOfType(const TypeMatcher<ScaffoldState>());
  }

  @override
  bool updateShouldNotify(AppScaffold old) => false;
}

/// This is a simple method and not a widget
/// so that null can be returned if no AppScaffold with a drawer was found
Widget buildLeadingAction(BuildContext context) {
  final ScaffoldState appScaffold = AppScaffold.of(context);
  if (appScaffold != null && appScaffold.hasDrawer) {
    return IconButton(
      icon: const Icon(Icons.menu),
      onPressed: () => appScaffold.openDrawer(),
      tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
    );
  } else {
    return null;
  }
}

或者,您也可以将Scaffold放置在页面内的抽屉中,并使用GlobalKey来访问其ScaffoldState