-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,68 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:provide/provide.dart'; | ||
import '../provide/counter.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
|
||
class CartPage extends StatelessWidget { | ||
class CartPage extends StatefulWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: Text('购物车'),), | ||
body: Container( | ||
margin: EdgeInsets.only(top: 200), | ||
child: Center( | ||
child: Column( | ||
children: <Widget>[ | ||
Number(), | ||
MyButton() | ||
], | ||
), | ||
) | ||
), | ||
); | ||
} | ||
_CartPageState createState() => _CartPageState(); | ||
} | ||
|
||
class Number extends StatelessWidget { | ||
class _CartPageState extends State<CartPage> { | ||
List<String> testList =[]; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
_show(); //每次进入前进行显示 | ||
return Container( | ||
child: Center( | ||
child: Provide<Counter>( | ||
builder: (context,child,counter){ | ||
return Text( | ||
"${counter.value}", | ||
style: Theme.of(context).textTheme.display1 | ||
); | ||
}, | ||
), | ||
child: Column( | ||
children: <Widget>[ | ||
Container( | ||
height: 500, | ||
child: ListView.builder( | ||
itemCount: testList.length, | ||
itemBuilder: (context,index){ | ||
return ListTile( | ||
title: Text(testList[index]), | ||
); | ||
}, | ||
), | ||
), | ||
RaisedButton( | ||
onPressed: (){_add();}, | ||
child: Text("增加"), | ||
), | ||
RaisedButton( | ||
onPressed: (){_clear();}, | ||
child: Text("清空"), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} | ||
|
||
class MyButton extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
child: RaisedButton( | ||
onPressed: (){ | ||
Provide.value<Counter>(context).increment(); | ||
}, | ||
child: Text("递增"), | ||
) | ||
); | ||
void _add() async { | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
String temp="技术胖是最胖的!"; | ||
testList.add(temp); | ||
prefs.setStringList('testInfo', testList); | ||
_show(); | ||
} | ||
|
||
void _show() async{ | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
setState(() { | ||
if(prefs.getStringList('testInfo')!=null){ | ||
testList=prefs.getStringList('testInfo'); | ||
} | ||
}); | ||
} | ||
|
||
void _clear() async{ | ||
SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
//prefs.clear(); //全部清空 | ||
prefs.remove('testInfo'); //删除key键 | ||
setState((){ | ||
testList=[]; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters