Advertisement
Guest User

Untitled

a guest
May 5th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.86 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4.  
  5. void main() => runApp(new CupertinoTabs());
  6.  
  7. class CupertinoTabs extends StatelessWidget {
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     return new MaterialApp(
  11.         home: new CupertinoTabScaffold(
  12.             tabBar: new CupertinoTabBar(
  13.               items: [
  14.                 new BottomNavigationBarItem(
  15.                   icon: new Icon(Icons.list),
  16.                   title: new Text("Posts"),
  17.                 ),
  18.                 new BottomNavigationBarItem(
  19.                   icon: new Icon(Icons.loyalty),
  20.                   title: new Text("Tags"),
  21.                 ),
  22.                 new BottomNavigationBarItem(
  23.                   icon: new Icon(Icons.calendar_today),
  24.                   title: new Text("Calendar"),
  25.                 ),
  26.                 new BottomNavigationBarItem(
  27.                   icon: new Icon(Icons.location_on),
  28.                   title: new Text("Locations"),
  29.                 ),
  30.               ],
  31.             ),
  32.             tabBuilder: (BuildContext context, int index) {
  33.               return new CupertinoTabView(builder: (BuildContext context) {
  34.                 return new CupertinoPageScaffold(
  35.                   navigationBar: new CupertinoNavigationBar(
  36.                       middle: new Text("${index+1} tab")),
  37.                   child: new Counter(index),
  38.                 );
  39.               });
  40.             }));
  41.   }
  42. }
  43.  
  44. class Counter extends StatefulWidget {
  45.   Counter(this.index, {Key key}) : super(key: key);
  46.  
  47.   final int index;
  48.  
  49.   @override
  50.   CounterState createState() => new CounterState();
  51. }
  52.  
  53. class CounterState extends State<Counter> {
  54.   int counter = 0;
  55.  
  56.  
  57.   void incrementCounter() async {
  58.     setState(() {
  59.       counter++;
  60.     });
  61.  
  62.     SharedPreferences prefs = await SharedPreferences.getInstance();
  63.     await prefs.setInt('counter${widget.index}', counter);
  64.     print("saved counter: $counter for index: ${widget.index}");
  65.   }
  66.  
  67.   void setCounterFromPrefs() async {
  68.     SharedPreferences prefs = await SharedPreferences.getInstance();
  69.     int counterValue = prefs.getInt('counter${widget.index}') ?? 0;
  70.  
  71.     setState(() {
  72.       counter = counterValue;
  73.     });
  74.   }
  75.  
  76.   @override
  77.   void initState() {
  78.     super.initState();
  79.     setCounterFromPrefs();
  80.   }
  81.  
  82.   @override
  83.   Widget build(BuildContext context) {
  84.     return new Center(
  85.       child: new Column(
  86.         mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  87.         children: <Widget>[
  88.           new Text('Counter value: $counter'),
  89.           new CupertinoButton(
  90.               child: const Text("Increment counter"),
  91.               color: CupertinoColors.activeBlue,
  92.               onPressed: () {
  93.                 incrementCounter();
  94.               }),
  95.         ],
  96.       ),
  97.     );
  98.   }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement