Skip to content

Commit 7cc3f07

Browse files
author
Bhavik Makwana
committed
- list grid view
- hardware key clicks
1 parent e1df686 commit 7cc3f07

File tree

4 files changed

+166
-42
lines changed

4 files changed

+166
-42
lines changed

lib/ExampleNameItem.dart

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,58 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_examples/models/ExapmleNames.dart';
3+
import 'package:meta/meta.dart';
34

5+
/// A [ExampleNameItem] to display a [ExampleNames].
46
class ExampleNameItem extends StatelessWidget {
5-
const ExampleNameItem(this.exampleNames);
6-
77
final ExampleNames exampleNames;
8+
final ValueChanged<ExampleNames> onTap;
9+
10+
const ExampleNameItem({
11+
Key key,
12+
@required this.exampleNames,
13+
this.onTap,
14+
})
15+
: assert(exampleNames != null),
16+
super(key: key);
817

918
@override
1019
Widget build(BuildContext context) {
11-
return _buildTiles(exampleNames, context);
12-
}
13-
14-
Widget _buildTiles(ExampleNames root, BuildContext context) {
15-
return Column(
16-
children: <Widget>[
17-
Card(
18-
shape:
19-
RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
20-
elevation: 4.0,
21-
child: Container(
22-
decoration: new BoxDecoration(
23-
border: new Border(
24-
left: new BorderSide(
25-
width: 4.0,
26-
color: Colors.lightGreen,
27-
),
28-
),
20+
return Card(
21+
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
22+
elevation: 4.0,
23+
child: Container(
24+
decoration: new BoxDecoration(
25+
border: new Border(
26+
left: new BorderSide(
27+
width: 4.0,
28+
color: Colors.lightGreen,
2929
),
30-
child: new ListTile(
31-
contentPadding: EdgeInsets.only(left: 16.0, right: 16.0),
32-
title: Row(
33-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
34-
children: <Widget>[
35-
Expanded(
36-
child: new Text(
37-
root.title,
38-
softWrap: true,
39-
),
40-
),
41-
new Icon(Icons.chevron_right)
42-
],
30+
),
31+
),
32+
child: InkWell(
33+
onTap: () {
34+
Navigator.pushNamed(context, "/${exampleNames.title}");
35+
},
36+
child: Row(
37+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
38+
crossAxisAlignment: CrossAxisAlignment.center,
39+
mainAxisSize: MainAxisSize.max,
40+
children: <Widget>[
41+
Expanded(
42+
child: Padding(
43+
padding: const EdgeInsets.all(16.0),
44+
child: new Text(
45+
exampleNames.title,
46+
),
4347
),
44-
onTap: () {
45-
Navigator.pushNamed(context, "/${exampleNames.title}");
46-
}),
48+
),
49+
new Icon(
50+
Icons.chevron_right,
51+
),
52+
],
4753
),
4854
),
49-
],
55+
),
5056
);
5157
}
5258
}

lib/main.dart

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter_examples/ui/app_bar/AppBarExample.dart';
77
import 'package:flutter_examples/ui/bottomnavigation/BottomNavigation.dart';
88
import 'package:flutter_examples/ui/collapsibletoolbar/CollapsibleToolbar.dart';
99
import 'package:flutter_examples/ui/drawer/NavigationDrawer.dart';
10+
import 'package:flutter_examples/ui/hardwarekey/RawKeyboardDemo.dart';
1011
import 'package:flutter_examples/ui/progressbutton/ProgressButton.dart';
1112
import 'package:flutter_examples/ui/staggeredanimation/StaggerDemo.dart';
1213
import 'package:flutter_examples/ui/stepper/StepperExample.dart';
@@ -47,6 +48,8 @@ class MyApp extends StatelessWidget {
4748
StaggerDemo(title: Strings.staggerDemoTitle),
4849
Strings.stepperExampleRoute: (BuildContext context) =>
4950
StepperExample(title: Strings.stepperExampleTitle),
51+
Strings.hardwareKeyExampleRoute: (BuildContext context) =>
52+
RawKeyboardDemo(title: Strings.hardwareKeyExampleTitle),
5053
});
5154
}
5255
}
@@ -79,6 +82,8 @@ class _MyHomePageState extends State<MyHomePage>
7982
super.dispose();
8083
}
8184

85+
bool status = false;
86+
8287
bool get _status {
8388
final AnimationStatus status = _controller.status;
8489
return status == AnimationStatus.completed ||
@@ -94,6 +99,9 @@ class _MyHomePageState extends State<MyHomePage>
9499
new IconButton(
95100
onPressed: () {
96101
_controller.fling(velocity: _status ? -2.0 : 2.0);
102+
setState(() {
103+
status = _status;
104+
});
97105
},
98106
icon: new AnimatedIcon(
99107
icon: AnimatedIcons.view_list,
@@ -105,16 +113,36 @@ class _MyHomePageState extends State<MyHomePage>
105113
body: new Column(
106114
children: <Widget>[
107115
Expanded(
108-
child: new ListView.builder(
109-
itemBuilder: (BuildContext context, int index) =>
110-
new ExampleNameItem(names[index]),
111-
itemCount: names.length,
112-
),
116+
child: _buildExampleItemsWidget(_status),
113117
),
114118
],
115119
),
116120
);
117121
}
122+
123+
_buildExampleItemsWidget(bool status) {
124+
if (status) {
125+
return new ListView.builder(
126+
itemBuilder: (BuildContext context, int index) =>
127+
new ExampleNameItem(
128+
exampleNames: names[index],
129+
),
130+
itemCount: names.length,
131+
);
132+
} else {
133+
return GridView.builder(
134+
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
135+
crossAxisCount: 2,
136+
childAspectRatio: 3.0,
137+
),
138+
itemBuilder: (BuildContext context, int index) =>
139+
new ExampleNameItem(
140+
exampleNames: names[index],
141+
),
142+
itemCount: names.length,
143+
);
144+
}
145+
}
118146
}
119147

120148
// The list displayed by this app.
@@ -129,4 +157,5 @@ final List<ExampleNames> names = <ExampleNames>[
129157
new ExampleNames(Strings.progressButtonTitle),
130158
new ExampleNames(Strings.staggerDemoTitle),
131159
new ExampleNames(Strings.stepperExampleTitle),
160+
new ExampleNames(Strings.hardwareKeyExampleTitle),
132161
];
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter/services.dart';
3+
4+
class RawKeyboardDemo extends StatefulWidget {
5+
final String title;
6+
7+
RawKeyboardDemo({Key key, this.title}) : super(key: key);
8+
9+
@override
10+
_HardwareKeyDemoState createState() => new _HardwareKeyDemoState();
11+
}
12+
13+
class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
14+
final FocusNode _focusNode = new FocusNode();
15+
RawKeyEvent _event;
16+
17+
@override
18+
void dispose() {
19+
_focusNode.dispose();
20+
super.dispose();
21+
}
22+
23+
void _handleKeyEvent(RawKeyEvent event) {
24+
setState(() {
25+
_event = event;
26+
});
27+
}
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
final TextTheme textTheme = Theme.of(context).textTheme;
32+
return Scaffold(
33+
appBar: new AppBar(
34+
title: new Text(widget.title),
35+
),
36+
body: Center(
37+
child: new RawKeyboardListener(
38+
focusNode: _focusNode,
39+
onKey: _handleKeyEvent,
40+
child: new AnimatedBuilder(
41+
animation: _focusNode,
42+
builder: (BuildContext context, Widget child) {
43+
if (!_focusNode.hasFocus) {
44+
return new GestureDetector(
45+
onTap: () {
46+
FocusScope.of(context).requestFocus(_focusNode);
47+
},
48+
child: new Text('Tap to focus', style: textTheme.display1),
49+
);
50+
}
51+
52+
if (_event == null)
53+
return new Text('Press Volume key', style: textTheme.display1);
54+
55+
int flags;
56+
int codePoint;
57+
int keyCode;
58+
int scanCode;
59+
int metaState;
60+
final RawKeyEventData data = _event.data;
61+
62+
if (data is RawKeyEventDataAndroid) {
63+
flags = data.flags;
64+
codePoint = data.codePoint;
65+
keyCode = data.keyCode;
66+
scanCode = data.scanCode;
67+
metaState = data.metaState;
68+
}
69+
70+
return new Column(
71+
mainAxisAlignment: MainAxisAlignment.center,
72+
children: <Widget>[
73+
new Text('${_event.runtimeType}', style: textTheme.subhead),
74+
new Text('flags: $flags', style: textTheme.subhead),
75+
new Text('codePoint: $codePoint', style: textTheme.subhead),
76+
new Text('keyCode: $keyCode', style: textTheme.subhead),
77+
new Text('scanCode: $scanCode', style: textTheme.subhead),
78+
new Text('metaState: $metaState', style: textTheme.subhead),
79+
],
80+
);
81+
},
82+
),
83+
),
84+
),
85+
);
86+
}
87+
}

lib/utils/Strings.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Strings {
2222
static const String progressButtonExampleRoute = "/Progress Button";
2323
static const String staggerDemoExampleRoute = "/Stagger Animation";
2424
static const String stepperExampleRoute = "/Stepper Example";
25+
static const String hardwareKeyExampleRoute = "/Hardware Key Example";
2526

2627
///Strings
2728
//Titles
@@ -37,4 +38,5 @@ class Strings {
3738
static const String progressButtonTitle = "Progress Button";
3839
static const String staggerDemoTitle = "Stagger Animation";
3940
static const String stepperExampleTitle = "Stepper Example";
41+
static const String hardwareKeyExampleTitle = "Hardware Key Example";
4042
}

0 commit comments

Comments
 (0)