Skip to content

Instantly share code, notes, and snippets.

@grinder15
Created April 24, 2020 05:31
Show Gist options
  • Save grinder15/fb908c42b5f968b89476b6216d17953d to your computer and use it in GitHub Desktop.
Save grinder15/fb908c42b5f968b89476b6216d17953d to your computer and use it in GitHub Desktop.
animations package, state of child transitioning fixed using GlobalKeys
import 'package:flutter/material.dart';
import 'package:animations/animations.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool _isCounterPage = true;
final GlobalKey _counterKey = GlobalKey();
final GlobalKey _statefulKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
),
body: PageTransitionSwitcher(
duration: const Duration(milliseconds: 500),
transitionBuilder: (child, animation, secondaryAnimation) {
return FadeThroughTransition(
child: child,
animation: animation,
secondaryAnimation: secondaryAnimation,
);
},
child: _isCounterPage
? CounterPage(
key: _counterKey,
)
: StatefulPage(
key: _statefulKey,
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.sync),
onPressed: () {
setState(() {
_isCounterPage = !_isCounterPage;
});
}),
);
}
}
class CounterPage extends StatefulWidget {
const CounterPage({Key key}) : super(key: key);
@override
_CounterPageState createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
int _counter = 0;
@override
void initState() {
super.initState();
print('CounterPage initState called.');
}
@override
void dispose() {
print('CounterPage dispose called.');
super.dispose();
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Counter value: $_counter'),
RaisedButton(
child: Text('Increment'),
onPressed: () {
setState(() {
_counter++;
});
},
),
],
),
);
}
}
class StatefulPage extends StatefulWidget {
const StatefulPage({Key key}) : super(key: key);
@override
_StatefulPageState createState() => _StatefulPageState();
}
class _StatefulPageState extends State<StatefulPage> {
String _text;
TextEditingController _controller;
@override
void initState() {
super.initState();
_text = '';
_controller = TextEditingController();
print('StatefulPage initState called.');
}
@override
void dispose() {
print('StatefulPage dispose called.');
_controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Saved text: $_text'),
TextField(
controller: _controller,
),
RaisedButton(
child: Text('Save text'),
onPressed: () {
setState(() {
_text = _controller.text;
_controller.text = '';
});
},
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment