Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0278658

Browse files
author
wz
committed
[impro]: 实现中间卡片
1 parent 0ce4ccc commit 0278658

3 files changed

Lines changed: 168 additions & 21 deletions

File tree

lib/pages/home_page.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import 'dart:ffi';
33

44
import 'package:flutter/material.dart';
55
import 'package:flutter_demo/dao/home_dao.dart';
6+
import 'package:flutter_demo/model/grid_nav_model.dart';
67
import 'package:flutter_demo/model/home_model.dart';
8+
import 'package:flutter_demo/widget/grid_nav.dart';
79
import 'package:flutter_demo/widget/local_nav.dart';
810
import 'package:flutter_swiper/flutter_swiper.dart';
911

@@ -26,7 +28,8 @@ class _HomePageState extends State {
2628
double _appbarOp = 0.0;
2729

2830
var _homejson = '';
29-
var _localNavList = null;
31+
var _localNavList;
32+
GridNavModel? gridNavModel;
3033

3134
_onScroll(offset) {
3235
double alpha = offset / APPBAR_SCROLL_OFFSET;
@@ -53,6 +56,7 @@ class _HomePageState extends State {
5356
setState(() {
5457
_homejson = json.encode(model);
5558
_localNavList = model.localNavList;
59+
gridNavModel = model.gridNav;
5660
});
5761
} catch (e) {
5862
setState(() {
@@ -96,6 +100,9 @@ class _HomePageState extends State {
96100
Padding(
97101
child: LocalNav(localNavList: _localNavList),
98102
padding: const EdgeInsets.fromLTRB(7, 4, 7, 4)),
103+
Padding(
104+
child: GridNav(gridNavModel: gridNavModel),
105+
padding: const EdgeInsets.fromLTRB(7, 0, 7, 4)),
99106
Container(
100107
child: Text(_homejson),
101108
height: 1000,

lib/widget/grid_nav.dart

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,141 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_demo/model/common_model.dart';
3+
import 'package:flutter_demo/model/grid_nav_model.dart';
4+
import 'package:flutter_demo/widget/webview.dart';
25

36
class GridNav extends StatelessWidget {
7+
final GridNavModel? gridNavModel;
8+
9+
const GridNav({Key? key, this.gridNavModel}) : super(key: key);
10+
411
@override
512
Widget build(BuildContext context) {
6-
return Text('data');
13+
return PhysicalModel(
14+
color: Colors.transparent,
15+
borderRadius: BorderRadius.circular(6),
16+
clipBehavior: Clip.antiAlias,
17+
child: Column(
18+
children: _gridNavItems(context),
19+
));
20+
}
21+
22+
_gridNavItems(BuildContext ctx) {
23+
List<Widget> items = [];
24+
if (gridNavModel == null) return items;
25+
26+
if (gridNavModel?.hotel != null) {
27+
items.add(_gridNavItem(ctx, gridNavModel!.hotel, true));
28+
}
29+
30+
if (gridNavModel!.flight != null) {
31+
items.add(_gridNavItem(ctx, gridNavModel!.flight, false));
32+
}
33+
if (gridNavModel!.travel != null) {
34+
items.add(_gridNavItem(ctx, gridNavModel!.travel, false));
35+
}
36+
37+
return items;
38+
}
39+
40+
Widget _gridNavItem(BuildContext ctx, GridNavItem model, bool first) {
41+
List<Widget> items = [];
42+
items.add(_mainItem(ctx, model.mainItem));
43+
44+
items.add(_doubleItem(ctx, model.item1, model.item2));
45+
46+
items.add(_doubleItem(ctx, model.item3, model.item4));
47+
48+
List<Widget> expandItems = [];
49+
items.forEach((element) {
50+
expandItems.add(Expanded(
51+
child: element,
52+
flex: 1,
53+
));
54+
});
55+
56+
Color startCor = Color(int.parse('0xff' + model.startColor));
57+
Color endCor = Color(int.parse('0xff' + model.endColor));
58+
59+
return Container(
60+
height: 88,
61+
margin: first ? null : EdgeInsets.only(top: 3),
62+
decoration:
63+
BoxDecoration(gradient: LinearGradient(colors: [startCor, endCor])),
64+
child: Row(children: expandItems),
65+
);
66+
}
67+
68+
Widget _mainItem(BuildContext ctx, CommonModel model) {
69+
return GestureDetector(
70+
onTap: () {
71+
Navigator.push(ctx, MaterialPageRoute(builder: (BuildContext ctx) {
72+
return WebView(
73+
url: model.url,
74+
statusBarColor: model.statusBarColor,
75+
title: model.title,
76+
hideAppBar: model.hideAppBar,
77+
backForbid: false);
78+
}));
79+
},
80+
child: Stack(
81+
alignment: AlignmentDirectional.topCenter,
82+
children: [
83+
Image.network(model.icon,
84+
fit: BoxFit.contain,
85+
height: 88,
86+
width: 121,
87+
alignment: AlignmentDirectional.bottomEnd),
88+
Container(
89+
margin: EdgeInsets.only(top: 11),
90+
child: Text(
91+
model.title,
92+
style: TextStyle(fontSize: 14, color: Colors.white),
93+
),
94+
)
95+
],
96+
));
97+
}
98+
99+
Widget _doubleItem(BuildContext ctx, CommonModel model, CommonModel model2) {
100+
return Column(
101+
children: [
102+
Expanded(child: _item(ctx, model, true)),
103+
Expanded(child: _item(ctx, model2, true))
104+
],
105+
);
106+
}
107+
108+
Widget _item(BuildContext ctx, CommonModel model, bool first) {
109+
BorderSide bs = const BorderSide(width: 0.8, color: Colors.white);
110+
return FractionallySizedBox(
111+
widthFactor: 1,
112+
heightFactor: 1,
113+
child: Container(
114+
decoration: BoxDecoration(
115+
border: Border(left: bs, bottom: first ? bs : BorderSide.none)),
116+
child: _wrapGesture(
117+
ctx,
118+
Center(
119+
child: Text(model.title,
120+
textAlign: TextAlign.center,
121+
style:
122+
const TextStyle(fontSize: 14, color: Colors.white))),
123+
model)),
124+
);
125+
}
126+
127+
Widget _wrapGesture(BuildContext ctx, Widget wd, CommonModel model) {
128+
return GestureDetector(
129+
onTap: () {
130+
Navigator.push(ctx, MaterialPageRoute(builder: (BuildContext ctx) {
131+
return WebView(
132+
url: model.url,
133+
statusBarColor: model.statusBarColor,
134+
title: model.title,
135+
hideAppBar: model.hideAppBar,
136+
backForbid: false);
137+
}));
138+
},
139+
child: wd);
7140
}
8141
}

lib/widget/webview.dart

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class WebView extends StatefulWidget {
99
final String url;
1010
final String? statusBarColor;
1111
final String title;
12-
final bool hideAppBar;
12+
bool hideAppBar = false;
1313
final bool backForbid;
1414

15-
const WebView(
15+
WebView(
1616
{Key? key,
1717
required this.url,
1818
this.statusBarColor = 'ffffff',
1919
required this.title,
20-
required this.hideAppBar,
20+
this.hideAppBar = false,
2121
required this.backForbid})
2222
: super(key: key);
2323

@@ -73,11 +73,11 @@ class _WebViewState extends State<WebView> {
7373

7474
@override
7575
void dispose() {
76-
super.dispose();
7776
_onStateChange.cancel();
7877
_onHttpError.cancel();
7978
_onUrlChange.cancel();
8079
webviewReference.dispose();
80+
super.dispose();
8181
}
8282

8383
@override
@@ -111,27 +111,34 @@ class _WebViewState extends State<WebView> {
111111
}
112112

113113
Widget _appBar(Color backgroundColor, Color backButtonColor) {
114-
if (widget.hideAppBar) {
114+
if (widget.hideAppBar == false) {
115115
return Container(
116116
color: backgroundColor,
117117
height: 30,
118118
);
119119
}
120120

121-
return FractionallySizedBox(
122-
child: Stack(
123-
children: [
124-
GestureDetector(
125-
child: Container(
126-
margin: const EdgeInsets.only(left: 10),
127-
child: Icon(Icons.close, color: backButtonColor, size: 26),
121+
return Container(
122+
color: backgroundColor,
123+
padding: const EdgeInsets.fromLTRB(0, 40, 0, 10),
124+
child: FractionallySizedBox(
125+
child: Stack(
126+
children: [
127+
GestureDetector(
128+
onTap: () {
129+
Navigator.pop(context);
130+
},
131+
child: Container(
132+
margin: const EdgeInsets.only(left: 10),
133+
child: Icon(Icons.close, color: backButtonColor, size: 26),
134+
),
128135
),
129-
),
130-
Positioned(
131-
child: Center(
132-
child: Text(widget.title,
133-
style: TextStyle(color: backButtonColor, fontSize: 20))))
134-
],
135-
));
136+
Positioned(
137+
child: Center(
138+
child: Text(widget.title,
139+
style: TextStyle(color: backButtonColor, fontSize: 20))))
140+
],
141+
)),
142+
);
136143
}
137144
}

0 commit comments

Comments
 (0)