Skip to content

Instantly share code, notes, and snippets.

@pedromassango
Last active December 15, 2020 14:37
Show Gist options
  • Save pedromassango/e0b67d80febf6b3483fdc68ee052a071 to your computer and use it in GitHub Desktop.
Save pedromassango/e0b67d80febf6b3483fdc68ee052a071 to your computer and use it in GitHub Desktop.
Exemplo de Restauração de Estado no Flutter
import 'package:flutter/material.dart';
void main() => runApp(
RootRestorationScope(
restorationId: 'root_id',
child: App(),
),
);
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with RestorationMixin {
RestorableInt _index = RestorableInt(0);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Demo'),),
body: Container(
color: Colors.primaries[_index.value],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _index.value,
selectedItemColor: Colors.blue,
unselectedItemColor: Colors.black,
onTap: (index) => setState(() => _index.value = index),
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Início'
),
BottomNavigationBarItem(
icon: Icon(Icons.rss_feed),
label: 'Notícias'
),
BottomNavigationBarItem(
icon: Icon(Icons.notifications),
label: 'Notificações'
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Configurações'
),
],
),
);
}
@override
String get restorationId => 'home_page';
@override
void restoreState(RestorationBucket oldBucket, bool initialRestore) {
registerForRestoration(_index, 'index');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment