Advertisement
Guest User

counter with sharedpreferences

a guest
Jul 11th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.26 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5.  
  6. void main() => runApp(new MyApp());
  7.  
  8. class MyApp extends StatelessWidget {
  9.   // This widget is the root of your application.
  10.   @override
  11.   Widget build(BuildContext context) {
  12.     return new MaterialApp(
  13.       title: 'Flutter Demo',
  14.       theme: new ThemeData(
  15.         // This is the theme of your application.
  16.         //
  17.         // Try running your application with "flutter run". You'll see the
  18.         // application has a blue toolbar. Then, without quitting the app, try
  19.         // changing the primarySwatch below to Colors.green and then invoke
  20.         // "hot reload" (press "r" in the console where you ran "flutter run",
  21.         // or press Run > Flutter Hot Reload in IntelliJ). Notice that the
  22.         // counter didn't reset back to zero; the application is not restarted.
  23.         primarySwatch: Colors.blue,
  24.       ),
  25.       home: new MyHomePage(title: 'Flutter Demo Home Page'),
  26.     );
  27.   }
  28. }
  29.  
  30. class MyHomePage extends StatefulWidget {
  31.   MyHomePage({Key key, this.title}) : super(key: key);
  32.  
  33.   // This widget is the home page of your application. It is stateful, meaning
  34.   // that it has a State object (defined below) that contains fields that affect
  35.   // how it looks.
  36.  
  37.   // This class is the configuration for the state. It holds the values (in this
  38.   // case the title) provided by the parent (in this case the App widget) and
  39.   // used by the build method of the State. Fields in a Widget subclass are
  40.   // always marked "final".
  41.  
  42.   final String title;
  43.  
  44.   @override
  45.   _MyHomePageState createState() => new _MyHomePageState();
  46. }
  47.  
  48. class _MyHomePageState extends State<MyHomePage> {
  49.  
  50.   Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  51.   Future<int> _counter;
  52.   int counterx;
  53.  
  54.   Future<Null> _incrementCounter() async {
  55.     final SharedPreferences prefs = await _prefs;
  56.     final int counter = (prefs.getInt('counter') ?? 0) + 1;
  57.  
  58.     setState(() {
  59.       _counter = prefs.setInt("counter", counter).then((bool success) {
  60.         return counter;
  61.       });
  62.     });
  63.   }
  64.  
  65.    @override
  66.       void initState() {
  67.         super.initState();
  68.       _prefs.then(ambilNilai);
  69.           }
  70.      
  71.         // void _incrementCounter() {
  72.         //   setState(() {
  73.         //     // This call to setState tells the Flutter framework that something has
  74.         //     // changed in this State, which causes it to rerun the build method below
  75.         //     // so that the display can reflect the updated values. If we changed
  76.         //     // _counter without calling setState(), then the build method would not be
  77.         //     // called again, and so nothing would appear to happen.
  78.         //     _counter++;
  79.         //   });
  80.         // }
  81.      
  82.         @override
  83.         Widget build(BuildContext context) {
  84.           // This method is rerun every time setState is called, for instance as done
  85.           // by the _incrementCounter method above.
  86.           //
  87.           // The Flutter framework has been optimized to make rerunning build methods
  88.           // fast, so that you can just rebuild anything that needs updating rather
  89.           // than having to individually change instances of widgets.
  90.           return new Scaffold(
  91.             appBar: new AppBar(
  92.               // Here we take the value from the MyHomePage object that was created by
  93.               // the App.build method, and use it to set our appbar title.
  94.               title: new Text(widget.title),
  95.             ),
  96.             body: new Center(
  97.               // Center is a layout widget. It takes a single child and positions it
  98.               // in the middle of the parent.
  99.               child: new Column(
  100.                 // Column is also layout widget. It takes a list of children and
  101.                 // arranges them vertically. By default, it sizes itself to fit its
  102.                 // children horizontally, and tries to be as tall as its parent.
  103.                 //
  104.                 // Invoke "debug paint" (press "p" in the console where you ran
  105.                 // "flutter run", or select "Toggle Debug Paint" from the Flutter tool
  106.                 // window in IntelliJ) to see the wireframe for each widget.
  107.                 //
  108.                 // Column has various properties to control how it sizes itself and
  109.                 // how it positions its children. Here we use mainAxisAlignment to
  110.                 // center the children vertically; the main axis here is the vertical
  111.                 // axis because Columns are vertical (the cross axis would be
  112.                 // horizontal).
  113.                 mainAxisAlignment: MainAxisAlignment.center,
  114.                 children: <Widget>[
  115.                   Text(counterx.toString()),
  116.                 ]
  117.               ),
  118.             ),
  119.             floatingActionButton: new FloatingActionButton(
  120.               onPressed: _incrementCounter,
  121.               tooltip: 'Increment',
  122.               child: Icon(Icons.add),
  123.             ), // This trailing comma makes auto-formatting nicer for build methods.
  124.           );
  125.         }
  126.      
  127.         void ambilNilai(SharedPreferences value) {
  128.           setState(() {
  129.              counterx = value.getInt("counter");        
  130.           });
  131.         }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement