~cytrogen/fluent-reader-mobile

ref: db17140569bfa3aa542322dbb3c76e7b5fb7d97f fluent-reader-mobile/lib/components/cupertino_toolbar.dart -rw-r--r-- 3.6 KiB
db171405 — Bruce Liu update dependencies 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * cupertino_toolbar
 * Copyright (c) 2019 Christian Mengler. All rights reserved.
 * See LICENSE for distribution and usage details.
 */

import 'package:fluent_reader_lite/utils/colors.dart';
import 'package:fluent_reader_lite/utils/global.dart';
import 'package:flutter/cupertino.dart';

/// Display a persistent bottom iOS styled toolbar for Cupertino theme
///
class CupertinoToolbar extends StatelessWidget {
  /// Creates a persistent bottom iOS styled toolbar for Cupertino
  /// themed app,
  ///
  /// Typically used as the [child] attribute of a [CupertinoPageScaffold].
  ///
  /// {@tool sample}
  ///
  /// A sample code implementing a typical iOS page with bottom toolbar.
  ///
  /// ```dart
  /// CupertinoPageScaffold(
  /// 	navigationBar: CupertinoNavigationBar(
  /// 		middle: Text('Cupertino Toolbar')
  /// 	),
  /// 	child: CupertinoToolbar(
  /// 		items: <CupertinoToolbarItem>[
  /// 			CupertinoToolbarItem(
  /// 				icon: CupertinoIcons.delete,
  /// 				onPressed: () {}
  /// 			),
  /// 			CupertinoToolbarItem(
  /// 				icon: CupertinoIcons.settings,
  /// 				onPressed: () {}
  /// 			)
  /// 		],
  /// 		body: Center(
  /// 			child: Text('Hello World')
  /// 		)
  /// 	)
  /// )
  /// ```
  /// {@end-tool}
  ///
  CupertinoToolbar({
    Key key,
    @required this.items,
    @required this.body
  }) : assert(items != null),
       assert(
        items.every((CupertinoToolbarItem item) => (item.icon != null)) == true,
        'Every item must have an icon and onPressed defined',
       ),
       assert(body != null),
       super(key: key);

  /// The interactive items laid out within the toolbar where each item has an icon.
  final List<CupertinoToolbarItem> items;

  /// The body displayed above the toolbar.
  final Widget body;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: body
        ),
        Container(
          decoration: BoxDecoration(
            border: Border(
              top: BorderSide(color: MyColors.barDivider.resolveFrom(context), width: 0.0)
            )
          ),
          child: SafeArea(
            top: false,
            child: SizedBox(
              height: Global.isTablet ? 50.0 : 44.0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: _createButtons()
              )
            )
          )
        )
      ]
    );
  }

  List<Widget> _createButtons() {
    final List<Widget> children = <Widget>[];
    for (int i = 0; i < items.length; i += 1) {
      children.add(CupertinoButton(
        padding: EdgeInsets.zero,
        child: Icon(
          items[i].icon,
          // color: CupertinoColors.systemBlue,
          semanticLabel: items[i].semanticLabel,
        ),
        onPressed: items[i].onPressed
      ));
    }
    return children;
  }
}

/// An interactive button within iOS themed [CupertinoToolbar]
class CupertinoToolbarItem {
  /// Creates an item that is used with [CupertinoToolbar.items].
  ///
  /// The argument [icon] should not be null.
  const CupertinoToolbarItem({
    @required this.icon,
    this.onPressed,
    this.semanticLabel
  }) : assert(icon != null);

  /// The icon of the item.
  ///
  /// This attribute must not be null.
  final IconData icon;

  /// The callback that is called when the item is tapped.
  ///
  /// This attribute must not be null.
  final VoidCallback onPressed;

  /// Semantic label for the icon.
  ///
  /// Announced in accessibility modes (e.g TalkBack/VoiceOver).
  /// This label does not show in the UI.
  final String semanticLabel;
}