Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- void main() => runApp(new CupertinoTabs());
- class CupertinoTabs extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return new MaterialApp(
- home: new CupertinoTabScaffold(
- tabBar: new CupertinoTabBar(
- items: [
- new BottomNavigationBarItem(
- icon: new Icon(Icons.list),
- title: new Text("Posts"),
- ),
- new BottomNavigationBarItem(
- icon: new Icon(Icons.loyalty),
- title: new Text("Tags"),
- ),
- new BottomNavigationBarItem(
- icon: new Icon(Icons.calendar_today),
- title: new Text("Calendar"),
- ),
- new BottomNavigationBarItem(
- icon: new Icon(Icons.location_on),
- title: new Text("Locations"),
- ),
- ],
- ),
- tabBuilder: (BuildContext context, int index) {
- return new CupertinoTabView(builder: (BuildContext context) {
- return new CupertinoPageScaffold(
- navigationBar: new CupertinoNavigationBar(
- middle: new Text("${index+1} tab")),
- child: new Counter(index),
- );
- });
- }));
- }
- }
- class Counter extends StatefulWidget {
- Counter(this.index, {Key key}) : super(key: key);
- final int index;
- @override
- CounterState createState() => new CounterState();
- }
- class CounterState extends State<Counter> {
- int counter = 0;
- void incrementCounter() async {
- setState(() {
- counter++;
- });
- SharedPreferences prefs = await SharedPreferences.getInstance();
- await prefs.setInt('counter${widget.index}', counter);
- print("saved counter: $counter for index: ${widget.index}");
- }
- void setCounterFromPrefs() async {
- SharedPreferences prefs = await SharedPreferences.getInstance();
- int counterValue = prefs.getInt('counter${widget.index}') ?? 0;
- setState(() {
- counter = counterValue;
- });
- }
- @override
- void initState() {
- super.initState();
- setCounterFromPrefs();
- }
- @override
- Widget build(BuildContext context) {
- return new Center(
- child: new Column(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: <Widget>[
- new Text('Counter value: $counter'),
- new CupertinoButton(
- child: const Text("Increment counter"),
- color: CupertinoColors.activeBlue,
- onPressed: () {
- incrementCounter();
- }),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement