update SharedPreferences save data and android device layout overflow

This commit is contained in:
xiaojia22326@163.com
2019-01-18 15:42:18 +08:00
parent d6ddaa881d
commit 00e65a99b1
4 changed files with 270 additions and 354 deletions

View File

@ -6,175 +6,164 @@
* email: zhu.yan@alibaba-inc.com
*/
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
//import 'package:flutter_rookie_book/model/collection_general.dart';
//import 'package:flutter_rookie_book/model/collection_general.dart';
const disclaimerText1 = '\r\r\r\r\r\r本APP属于个人的非赢利性开源项目以供开源社区使用凡本APP转载的所有的文章 、图片、音频、视频文件等资料的版权归版权所有人所有本APP采用的非本站原创文章及图片等内容无法一一和版权者联系如果本网所选内容的文章作者及编辑认为其作品不宜上网供大家浏览或不应无偿使用请及时用电子邮件或电话通知我们以迅速采取适当措施避免给双方造成不必要的经济损失。';
const disclaimerText2 = '\n\r\r\r\r\r\r对于已经授权本APP独家使用并提供给本站资料的版权所有人的文章、图片等资料如需转载使用需取得本站和版权所有人的同意。本APP所刊发、转载的文章其版权均归原作者所有如其他媒体、网站或个人从本网下载使用请在转载有关文章时务必尊重该文章的著作权保留本网注明的“稿件来源”并自负版权等法律责任。';
const disclaimerText1 =
'\r\r\r\r\r\r本APP属于个人的非赢利性开源项目以供开源社区使用凡本APP转载的所有的文章 、图片、音频、视频文件等资料的版权归版权所有人所有本APP采用的非本站原创文章及图片等内容无法一一和版权者联系如果本网所选内容的文章作者及编辑认为其作品不宜上网供大家浏览或不应无偿使用请及时用电子邮件或电话通知我们以迅速采取适当措施避免给双方造成不必要的经济损失。';
const disclaimerText2 =
'\n\r\r\r\r\r\r对于已经授权本APP独家使用并提供给本站资料的版权所有人的文章、图片等资料如需转载使用需取得本站和版权所有人的同意。本APP所刊发、转载的文章其版权均归原作者所有如其他媒体、网站或个人从本网下载使用请在转载有关文章时务必尊重该文章的著作权保留本网注明的“稿件来源”并自负版权等法律责任。';
class DisclaimerMsg extends StatefulWidget {
final State pWidget;
DisclaimerMsg({ Key key, this.pWidget }) : super(key: key);
DisclaimerMsg({Key key, this.pWidget}) : super(key: key);
DisclaimerMsgState createState() => DisclaimerMsgState();
}
class DisclaimerMsgState extends State<DisclaimerMsg> {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
Future<bool> _unKnow;
var _valBool = false;
var _page;
//CollectionControlModel _collectionControl = new CollectionControlModel();
//List<CollectionGeneral> _collectionList = [];
// void init(BuildContext context) {
// Toast.show(context: context, message: "👉 APP免责声明",cb:showAlertDialog);
// }
var _readed = false;
//SharedPreferences 存储结果
Future<bool> refs(bool value) async {
final SharedPreferences prefs = await _prefs;
final bool unKnow = value;
setState(() {
_unKnow = prefs.setBool("disclaimer", unKnow).then((bool success) {
return unKnow;
});
});
}
@override
void initState() {
super.initState();
// _collectionList.clear();
// _collectionControl.getAllCollection().then((resultList) {
// resultList.forEach((item) {
// _collectionList.add(item);
// print('=============db=========${item}');
// });
// });
_page = widget.pWidget;
}
void refs(bool value){
if(this.mounted){
setState(() {
_valBool=value;
_page.save(value);
//获取SharedPreferences 存储结果
_unKnow = _prefs.then((SharedPreferences prefs) {
return (prefs.getBool('disclaimer') ?? false);
});
_unKnow.then((bool value) {
_valBool = value;
_readed = value;
});
// _collectionControl
// .insert(CollectionGeneral(key: 'disclaimer', values: value.toString()))
// .then((result) {
// print('result2====${result}');
// }
// );
}
}
void showAlertDialog(BuildContext context) {
// new Future.delayed(Duration(seconds: 5)).then((value) {
// Navigator.of(context).pop();
// });
showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
//title: Text('免责声明'),
content:SingleChildScrollView(
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(5.0, 5.0, 10.0, 10.0),
//width: 100,
height: 35,
child: Text('免责声明',style:TextStyle(fontSize: 18,fontWeight:FontWeight.w700 )),
child: Text('免责声明',
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.w700)),
decoration: BoxDecoration(
//color: Colors.blue,
image: DecorationImage(
fit: BoxFit.fitWidth,
image: AssetImage('assets/images/paimaiLogo.png')
),
image: AssetImage('assets/images/paimaiLogo.png')),
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
//alignment: Alignment.bottomRight,
)
),
SizedBox(height:20),
)),
SizedBox(height: 20),
Text(disclaimerText1),
Text(disclaimerText2),
],
),
),
shape:RoundedRectangleBorder(borderRadius: new BorderRadius.circular(20.0)), // 圆角
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(20.0)), // 圆角
actions: <Widget>[
Container(
width: 270,
alignment: Alignment.centerLeft,
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 30.0, 0.0),
child:
Row(
mainAxisAlignment:MainAxisAlignment.spaceAround,
//crossAxisAlignment:CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment:MainAxisAlignment.center,
children: <Widget>[
Checkbox(
activeColor: Theme.of(context).primaryColor,
tristate:false,
value: _valBool,
onChanged: (bool bol) {
refs(bol);
Navigator.of(context).pop(); // here I pop to avoid multiple Dialogs
showAlertDialog(context); //here i call the same function
}
),
Text('不再自动提示',style:TextStyle(fontSize: 14)),
new Container(
width: 250,
child: _create(),
)
],
),
new Flexible(
flex: 1,
child: Container(width: 100,)
),
);
},
);
}
Row _create() {
//已读
if (_readed) {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
child: Text('知道了',style:TextStyle(fontSize: 16,color: Colors.white)),
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Text('已阅读知晓',
style: TextStyle(fontSize: 16, color: Colors.white)),
//可点击
color: Theme.of(context).primaryColor,
onPressed: () {
Navigator.of(context).pop();
},
),
]
// SizedBox(
// width:150,
// height:55,
// child: CheckboxListTile(
// title: Text('不再显示',style:TextStyle(fontSize: 14)),
// controlAffinity: ListTileControlAffinity.leading,
// activeColor: Colors.red,
// value: _valBool,
// onChanged: (bool value) {
// refs(value);
// Navigator.of(context).pop(); // here I pop to avoid multiple Dialogs
// showAlertDialog(context); //here i call the same function
// }
// )
// //secondary: const Icon(Icons.hourglass_empty),
// ),
// Checkbox(
// activeColor: Colors.red,
// tristate:false,
// value: _valBool,
// onChanged: (bool bol) {
// refs(bol);
// Navigator.of(context).pop(); // here I pop to avoid multiple Dialogs
// showAlertDialog(context); //here i call the same function
// }
// ),
// Text('不再显示',style:TextStyle(fontSize: 14)),
// FlatButton(
// child: Text('知道了',style:TextStyle(fontSize: 16,color: Colors.green)),
// onPressed: () {
// Navigator.of(context).pop();
// },
// ),
// ],
SizedBox(
width: 10.0,
)
)],
);
},
],
);
}
//第一次读取
return Row(mainAxisAlignment: MainAxisAlignment.spaceAround,
//crossAxisAlignment:CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Checkbox(
activeColor: Theme.of(context).primaryColor,
tristate: false,
value: _valBool,
onChanged: (bool bol) {
setState(() {
_valBool = bol;
});
// refs(bol);
Navigator.of(context)
.pop(); // here I pop to avoid multiple Dialogs
showAlertDialog(context); //here i call the same function
}),
Text('不再自动提示', style: TextStyle(fontSize: 14)),
],
),
FlatButton(
child: Text('知道了',
style: TextStyle(fontSize: 16, color: Colors.white)),
//可点击
color: _valBool
? Theme.of(context).primaryColor
: Theme.of(context).primaryColor.withAlpha(800),
onPressed: () {
// if (_valBool) {
refs(_valBool);
Navigator.of(context).pop();
// }
},
),
]);
}
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () {
@ -184,10 +173,11 @@ class DisclaimerMsgState extends State<DisclaimerMsg> {
//alignment: const Alignment(1.6, 1.6),
children: [
new Container(
width:90.0,
width: 90.0,
alignment: Alignment.center,
decoration: new BoxDecoration(
borderRadius:new BorderRadius.horizontal(right: Radius.circular(10)),
borderRadius:
new BorderRadius.horizontal(right: Radius.circular(10)),
color: Colors.black45,
),
child: new Text(
@ -200,66 +190,6 @@ class DisclaimerMsgState extends State<DisclaimerMsg> {
),
),
],
)
);
}
Widget build2(BuildContext context) {
return Container(
padding: new EdgeInsets.all(0.0),
alignment:Alignment.centerRight,
child:FlatButton(
//padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
child: new Text(
'👉 APP免责声明',
style: new TextStyle(
fontSize: 12.0, //textsize
color: Colors.black54, // textcolor
),
),
//color: Theme.of(context).accentColor,
color: Theme.of(context).accentColor,
//elevation: 0.0,//shadow
//splashColor: Colors.blueGrey,
onPressed: () {
showAlertDialog(context);
//Toast.show(context: context, message: "👉 APP免责声明",cb:showAlertDialog);
})
);
}
}
class Toast {
static void show({@required BuildContext context, @required String message,Function cb}) {
//创建一个OverlayEntry对象
OverlayEntry overlayEntry = new OverlayEntry(builder: (context) {
return new Positioned(
top: MediaQuery.of(context).size.height * 0.12,
right:5.0,
child:RaisedButton(
padding: new EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 0.0),
child: new Text(
'👉 APP免责声明',
style: new TextStyle(
fontSize: 14.0, //textsize
color: Colors.black54, // textcolor
),
),
//color: Theme.of(context).accentColor,
color: Colors.red,
//elevation: 0.0,//shadow
//splashColor: Colors.blueGrey,
onPressed: () {
if(cb is Function){
cb(context);
}
})
);
});
//往Overlay中插入插入OverlayEntry
Overlay.of(context).insert(overlayEntry);
new Future.delayed(Duration(seconds: 2)).then((value) {
//overlayEntry.remove();
});
));
}
}

View File

@ -15,14 +15,14 @@ final pages = [
//'assets/world.png',
'assets/images/p1.png',
'FLutterGo的背景',
'🐢 官网文档示例较不够健全,不够直观\n🐞 运行widget demo要到处翻阅资料\n🐌 英文文档翻译生涩难懂,学习资料太少\n',
'🐢 官网文档示例较不够健全,不够直观\n🐞 运行widget demo要到处翻阅资料\n🐌 英文文档翻译生涩难懂,学习资料太少\n🚀 需要的效果不知道用哪个widget\n',
'assets/images/calendar.png'),
new PageViewModel(
const Color(0xFFFF682D),
//'assets/home.png',
'assets/images/p3.png',
'FlutterGo的特点',
'🐡 详解常用widget多达 130+ 个\n🦋 持续迭代追新官方版本\n🐙 配套Demo详解widget用法\n🚀 一站式搞定所有常用widget,开箱即查\n',
'🐡 详解常用widget多达 140+ 个\n🦋 持续迭代追新官方版本\n🐙 配套Demo详解widget用法\n🚀 一站式搞定所有常用widget,开箱即查\n',
'assets/images/house.png',
),
];
@ -36,27 +36,32 @@ class Page extends StatelessWidget {
this.percentVisible = 1.0,
});
_goHomePage(context) {
Navigator.of(context).pushNamedAndRemoveUntil(
'/home', (Route<dynamic> route) => false);
Navigator.of(context)
.pushNamedAndRemoveUntil('/home', (Route<dynamic> route) => false);
}
Widget creatButton(BuildContext context,String txt,IconData iconName,String type){
Widget creatButton(
BuildContext context, String txt, IconData iconName, String type) {
return RaisedButton.icon(
onPressed: () {
if(type == 'start'){
if (type == 'start') {
_goHomePage(context);
}else if(type == 'goGithub'){
Application.router.navigateTo(context, '${Routes.webViewPage}?title=${Uri.encodeComponent(txt)} Doc&&url=${Uri.encodeComponent("https://github.com/alibaba/flutter-go")}');
} else if (type == 'goGithub') {
Application.router.navigateTo(context,
'${Routes.webViewPage}?title=${Uri.encodeComponent(txt)} Doc&&url=${Uri.encodeComponent("https://github.com/alibaba/flutter-go")}');
}
},
elevation: 10.0,
color:Colors.black26,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
color: Colors.black26,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
//如果不手动设置icon和text颜色,则默认使用foregroundColor颜色
icon: new Icon(iconName,color: Colors.white,size:20.0),
label: new Text(txt, maxLines: 1,style: TextStyle(color:Colors.white,fontSize: 16,fontWeight: FontWeight.w700),)
);
icon: new Icon(iconName, color: Colors.white, size: 20.0),
label: new Text(
txt,
maxLines: 1,
style: TextStyle(
color: Colors.white, fontSize: 16, fontWeight: FontWeight.w700),
));
}
@override
@ -67,23 +72,31 @@ class Page extends StatelessWidget {
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: new Opacity(
opacity: percentVisible,
child: new Column(
crossAxisAlignment:CrossAxisAlignment.center,
child:ListView(
children: <Widget>[
layout(context)
],
),
));
}
Column layout(BuildContext context) {
return new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Transform(
transform: new Matrix4.translationValues(0.0, 50.0 * (1.0 - percentVisible) ,0.0),
transform: new Matrix4.translationValues(
0.0, 50.0 * (1.0 - percentVisible), 0.0),
child: new Padding(
padding: new EdgeInsets.only(top: 20.0, bottom: 10.0),
child:
new Image.asset(
viewModel.heroAssetPath,
width: 200.0,
height: 200.0),
child: new Image.asset(viewModel.heroAssetPath,
width: 160.0, height: 160.0),
),
),
new Transform(
transform: new Matrix4.translationValues(0.0, 30.0 * (1.0 - percentVisible) ,0.0),
transform: new Matrix4.translationValues(
0.0, 30.0 * (1.0 - percentVisible), 0.0),
child: new Padding(
padding: new EdgeInsets.only(top: 10.0, bottom: 10.0),
child: new Text(
@ -91,20 +104,21 @@ class Page extends StatelessWidget {
style: new TextStyle(
color: Colors.white,
fontFamily: 'FlamanteRoma',
fontSize: 34.0,
fontSize: 28.0,
),
),
),
),
new Transform(
transform: new Matrix4.translationValues(0.0, 30.0 * (1.0 - percentVisible) ,0.0),
transform: new Matrix4.translationValues(
0.0, 30.0 * (1.0 - percentVisible), 0.0),
child: new Padding(
padding: new EdgeInsets.only(bottom: 10.0),
child: new Text(
viewModel.body,
textAlign: TextAlign.center,
style: new TextStyle(
height:1.2,
height: 1.2,
color: Colors.white,
fontFamily: 'FlamanteRomaItalic',
fontSize: 18.0,
@ -115,11 +129,11 @@ class Page extends StatelessWidget {
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
creatButton(context,'开始使用',Icons.add_circle_outline,'start'),
creatButton(context,'GitHub',Icons.arrow_forward,'goGithub'),
],)
]),
));
creatButton(context, '开始使用', Icons.add_circle_outline, 'start'),
creatButton(context, 'GitHub', Icons.arrow_forward, 'goGithub'),
],
)
]);
}
}

View File

@ -7,7 +7,6 @@ import 'package:flutter_go/components/pagination.dart';
import 'package:flutter_go/components/first_page_item.dart';
import 'package:flutter_go/components/disclaimer_msg.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../common/net_utils.dart';
GlobalKey<DisclaimerMsgState> key;
@ -18,55 +17,36 @@ class FirstPage extends StatefulWidget {
}
class FirstPageState extends State<FirstPage> with AutomaticKeepAliveClientMixin{
Future<SharedPreferences> _prefs=SharedPreferences.getInstance();
Future<bool> _unKnow;
@override
bool get wantKeepAlive => true;
save(bool flag) async{
//print('=============save=========$flag');
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('disclaimer', flag.toString());
}
Future<String> get() async {
var value;
SharedPreferences prefs = await SharedPreferences.getInstance();
value = prefs.getString('disclaimer');
return value;
}
@override
void initState() {
super.initState();
if(key == null) {
//print('=============111=========${key}');
delayed();
}
if (key == null) {
key = GlobalKey<DisclaimerMsgState>();
}
/*
//获取sharePre
_unKnow = _prefs.then((SharedPreferences prefs) {
return (prefs.getBool('disclaimer') ?? false);
});
/**
* 判断是否需要弹出免责声明,已经勾选过不在显示,就不会主动弹
* */
Future delayed() async {
await new Future.delayed(const Duration(seconds: 1));
// if (this.mounted) {
// setState(() {
// print('test=======>${key.currentState}');
// key.currentState.showAlertDialog(context);
// //key.currentState.init(context);
// });
// }
Future<String> flag = get();
flag.then((String value) {
//print('=============get=========$value');
if(value.toString() == 'false'){ // 如果没有勾选下次开启
*/
_unKnow.then((bool value) {
print("==========FirstPageState========_unKnow========${value}");
if (!value) {
key.currentState.showAlertDialog(context);
}
});
}
}
Future<Map> getIndexListData([Map<String, dynamic> params]) async {
const juejin_flutter = 'https://timeline-merger-ms.juejin.im/v1/get_tag_entry?src=web&tagId=5a96291f6fb9a0535b535438';
var pageIndex = (params is Map) ? params['pageIndex'] : 0;

View File

@ -1,4 +1,3 @@
import 'dart:async';
import 'package:flutter/material.dart';
@ -13,34 +12,32 @@ class FourthPage extends StatefulWidget {
}
class FourthPageState extends State<FourthPage> with TickerProviderStateMixin {
StreamController<SlideUpdate> slideUpdateStream;
AnimatedPageDragger animatedPageDragger;
int activeIndex = 0 ;
int activeIndex = 0;
SlideDirection slideDirection = SlideDirection.none;
int nextPageIndex = 0 ;
double slidePercent= 0.0;
int nextPageIndex = 0;
double slidePercent = 0.0;
FourthPageState(){
FourthPageState() {
slideUpdateStream = new StreamController<SlideUpdate>();
slideUpdateStream.stream.listen((SlideUpdate event){
slideUpdateStream.stream.listen((SlideUpdate event) {
setState(() {
if( event.updateType == UpdateType.dragging){
if (event.updateType == UpdateType.dragging) {
slideDirection = event.direction;
slidePercent = event.slidePercent;
if( slideDirection == SlideDirection.leftToRight ){
if (slideDirection == SlideDirection.leftToRight) {
nextPageIndex = activeIndex - 1;
} else if (slideDirection == SlideDirection.rightToLeft){
} else if (slideDirection == SlideDirection.rightToLeft) {
nextPageIndex = activeIndex + 1;
} else{
} else {
nextPageIndex = activeIndex;
}
} else if( event.updateType == UpdateType.doneDragging){
if(slidePercent > 0.5){
} else if (event.updateType == UpdateType.doneDragging) {
if (slidePercent > 0.5) {
animatedPageDragger = new AnimatedPageDragger(
slideDirection: slideDirection,
transitionGoal: TransitionGoal.open,
@ -48,8 +45,7 @@ class FourthPageState extends State<FourthPage> with TickerProviderStateMixin {
slideUpdateStream: slideUpdateStream,
vsync: this,
);
} else{
} else {
animatedPageDragger = new AnimatedPageDragger(
slideDirection: slideDirection,
transitionGoal: TransitionGoal.close,
@ -62,13 +58,10 @@ class FourthPageState extends State<FourthPage> with TickerProviderStateMixin {
}
animatedPageDragger.run();
}
else if( event.updateType == UpdateType.animating){
} else if (event.updateType == UpdateType.animating) {
slideDirection = event.direction;
slidePercent = event.slidePercent;
}
else if (event.updateType == UpdateType.doneAnimating){
} else if (event.updateType == UpdateType.doneAnimating) {
activeIndex = nextPageIndex;
slideDirection = SlideDirection.none;
@ -78,23 +71,22 @@ class FourthPageState extends State<FourthPage> with TickerProviderStateMixin {
}
});
});
}
@override
Widget build(BuildContext context) {
return new Stack(
children: [
new Page( // page 的主要内容
new Page(
// page 的主要内容
viewModel: pages[activeIndex],
percentVisible: 1.0 ,
percentVisible: 1.0,
),
new PageReveal(
revealPercent: slidePercent,
child: new Page(
viewModel: pages[nextPageIndex],
percentVisible: slidePercent ,
percentVisible: slidePercent,
),
),
new PagerIndicator(
@ -106,8 +98,8 @@ class FourthPageState extends State<FourthPage> with TickerProviderStateMixin {
),
),
new PageDragger(
canDragLeftToRight: activeIndex > 0 ,
canDragRightToLeft: activeIndex < pages.length - 1 ,
canDragLeftToRight: activeIndex > 0,
canDragRightToLeft: activeIndex < pages.length - 1,
slideUpdateStream: this.slideUpdateStream,
)
],