Structure of code
In this tutorial, we will tell you how about the structure of our code.
First, We should create app to contain our pages.
1
void main() => runApp(FlutterOpenApp());
2
3
class FlutterOpenApp extends StatelessWidget {
4
// This widget is the root of your application.
5
@override
6
Widget build(BuildContext context) {
7
return MaterialApp(
8
title: FLUTTER_OPEN,
9
theme: ThemeData(primaryColor: BLUE_DEEP),
10
home: HomePage(),//this is our home page.
11
routes: {
12
PageName.CONTAINER: (context) => ContainerPage(),//this is our choose content
13
//other widgets pages will be put in here. Now I just put one as above.
14
},
15
);
16
}
17
}
Copied!
Then, I create a home page to show all widgets that we can choose to jump to.
1
class HomePage extends StatefulWidget {
2
@override
3
_HomeState createState() => _HomeState();
4
}
5
6
class _HomeState extends State<HomePage> {
7
Widget _item(context, index) {
8
return InkWell(
9
child: Card(
10
child: Stack(
11
children: <Widget>[
12
Center(
13
child: Image.asset(
14
PAGE_ITEMS[index]["img"],
15
fit: BoxFit.cover,
16
),
17
),
18
Container(
19
constraints: BoxConstraints.expand(),
20
decoration: BoxDecoration(gradient: _itemGradient(index)),
21
),
22
Center(
23
child: Text(
24
PAGE_ITEMS[index]["title"],
25
style: TextStyle(
26
color: TEXT_BLACK_LIGHT,
27
fontSize: TEXT_LARGE,
28
fontWeight: FontWeight.w700),
29
),
30
)
31
],
32
)),
33
onTap: () {
34
Navigator.pushNamed(context, PAGE_ITEMS[index]["click"]);
35
},
36
);
37
}
38
@override
39
Widget build(BuildContext context) {
40
return Scaffold(
41
appBar: AppBar(
42
title: Text(FLUTTER_OPEN),
43
),
44
body: GridView.builder(
45
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
46
crossAxisCount: 1,
47
mainAxisSpacing: 4,
48
crossAxisSpacing: 4,
49
childAspectRatio: 1.2),
50
itemBuilder: (context, index) {
51
return _item(context, index);
52
},
53
itemCount: PAGE_ITEMS.length,
54
),
55
);
56
}
57
}
Copied!
Facebook Page
Twitter
GitHub
NieBin
NieBin
Whole code in the GitHub : Flutter-Widgets
Last modified 2yr ago
Copy link