mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2026-08-25 01:05:36 +00:00
refactor: split app wrapper from focused example code (single-file apps)
Convert 20 more single-file apps to the validated pattern: lib/main.dart holds only the runApp + MaterialApp bootstrap, while a new lib/example.dart holds the full screen (Scaffold, AppBar) and the focused example code. Apps: enabling_splash_screen, getx_counter_app, image_from_network, infinite_list, load_local_image, load_local_json, persist_key_value, push_notifications, sliver_app_bar_example, stateless_widgets, tic_tac_toe, tip_calculator, using_alert_dialog, using_edittext, using_expansionpanel, using_http_get, using_interactiveviewer, using_snackbar, using_stepper, using_theme
This commit is contained in:
94
persist_key_value/lib/example.dart
Normal file
94
persist_key_value/lib/example.dart
Normal file
@@ -0,0 +1,94 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
// Example: persist a counter across app restarts with shared_preferences.
|
||||
class Example extends StatefulWidget {
|
||||
const Example({super.key});
|
||||
|
||||
@override
|
||||
State<Example> createState() => _ExampleState();
|
||||
}
|
||||
|
||||
class _ExampleState extends State<Example> {
|
||||
var nameOfApp = "Persist Key Value";
|
||||
|
||||
var counter = 0;
|
||||
|
||||
// define a key to use later
|
||||
var key = "counter";
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadSavedData();
|
||||
}
|
||||
|
||||
Future<void> _loadSavedData() async {
|
||||
// Get shared preference instance
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
// Get value
|
||||
counter = (prefs.getInt(key) ?? 0);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _onIncrementHit() async {
|
||||
// Get shared preference instance
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
|
||||
setState(() {
|
||||
// Get value
|
||||
counter = (prefs.getInt(key) ?? 0) + 1;
|
||||
});
|
||||
|
||||
// Save Value
|
||||
prefs.setInt(key, counter);
|
||||
}
|
||||
|
||||
Future<void> _onDecrementHit() async {
|
||||
// Get shared preference instance
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
|
||||
setState(() {
|
||||
// Get value
|
||||
counter = (prefs.getInt(key) ?? 0) - 1;
|
||||
});
|
||||
|
||||
// Save Value
|
||||
prefs.setInt(key, counter);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
// Appbar
|
||||
appBar: AppBar(
|
||||
// Title
|
||||
title: Text(nameOfApp),
|
||||
),
|
||||
// Body
|
||||
body: Container(
|
||||
// Center the content
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
'$counter',
|
||||
textScaler: TextScaler.linear(10.0),
|
||||
),
|
||||
const Padding(padding: EdgeInsets.all(10.0)),
|
||||
ElevatedButton(
|
||||
onPressed: _onIncrementHit,
|
||||
child: const Text('Increment Counter')),
|
||||
const Padding(padding: EdgeInsets.all(10.0)),
|
||||
ElevatedButton(
|
||||
onPressed: _onDecrementHit,
|
||||
child: const Text('Decrement Counter')),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,101 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'example.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
// Disable the debug flag
|
||||
debugShowCheckedModeBanner: false,
|
||||
// Home
|
||||
home: MyHome()));
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyHome extends StatefulWidget {
|
||||
const MyHome({super.key});
|
||||
|
||||
@override
|
||||
MyHomeState createState() {
|
||||
return MyHomeState();
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomeState extends State<MyHome> {
|
||||
var nameOfApp = "Persist Key Value";
|
||||
|
||||
var counter = 0;
|
||||
|
||||
// define a key to use later
|
||||
var key = "counter";
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadSavedData();
|
||||
}
|
||||
|
||||
Future<void> _loadSavedData() async {
|
||||
// Get shared preference instance
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
// Get value
|
||||
counter = (prefs.getInt(key) ?? 0);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _onIncrementHit() async {
|
||||
// Get shared preference instance
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
|
||||
setState(() {
|
||||
// Get value
|
||||
counter = (prefs.getInt(key) ?? 0) + 1;
|
||||
});
|
||||
|
||||
// Save Value
|
||||
prefs.setInt(key, counter);
|
||||
}
|
||||
|
||||
Future<void> _onDecrementHit() async {
|
||||
// Get shared preference instance
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
|
||||
setState(() {
|
||||
// Get value
|
||||
counter = (prefs.getInt(key) ?? 0) - 1;
|
||||
});
|
||||
|
||||
// Save Value
|
||||
prefs.setInt(key, counter);
|
||||
}
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
// Appbar
|
||||
appBar: AppBar(
|
||||
// Title
|
||||
title: Text(nameOfApp),
|
||||
),
|
||||
// Body
|
||||
body: Container(
|
||||
// Center the content
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
'$counter',
|
||||
textScaler: TextScaler.linear(10.0),
|
||||
),
|
||||
Padding(padding: EdgeInsets.all(10.0)),
|
||||
ElevatedButton(
|
||||
onPressed: _onIncrementHit, child: Text('Increment Counter')),
|
||||
Padding(padding: EdgeInsets.all(10.0)),
|
||||
ElevatedButton(
|
||||
onPressed: _onDecrementHit, child: Text('Decrement Counter')),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
return MaterialApp(
|
||||
// Disable the debug flag
|
||||
debugShowCheckedModeBanner: false,
|
||||
// Title
|
||||
title: "Persist Key Value",
|
||||
// Home
|
||||
home: const Example(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user