Skip to content

Instantly share code, notes, and snippets.

@RyanAfrish7
Created September 18, 2018 19:54
Show Gist options
  • Save RyanAfrish7/c9a9553cf9f821769347f1b7f9ddc9c7 to your computer and use it in GitHub Desktop.
Save RyanAfrish7/c9a9553cf9f821769347f1b7f9ddc9c7 to your computer and use it in GitHub Desktop.
GlobalKey.currentState == null
import 'package:flutter/cupertino.dart';
class FooBarView extends StatefulWidget {
VoidCallback onTap;
String name;
FooBarView({Key key, @required this.name, @required this.onTap}): super(key: key);
@override
FooBarViewState createState() => FooBarViewState();
}
class FooBarViewState extends State<FooBarView> {
int i = 0;
@override
Widget build(BuildContext context) {
return GestureDetector(onTap: widget.onTap, child: Center(child:Container(child: Text("${widget.name} - $i"))));
}
}
import 'package:flutter/material.dart';
import 'package:flutter_app/foobar.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final controller = PageController();
final fooState = GlobalKey<FooBarViewState>();
final barState = GlobalKey<FooBarViewState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
body: PageView(
controller: controller,
children: <Widget>[
FooBarView(
key: fooState,
name: "Flutter",
onTap: () {
controller.animateToPage(1, duration: Duration(milliseconds: 300), curve: Curves.easeOut);
barState.currentState.i++;
},
),
FooBarView(
name: "MDV",
key: barState,
onTap: () {
controller.animateToPage(0, duration: Duration(milliseconds: 300), curve: Curves.easeOut);
fooState.currentState.i++;
},
),
],
),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_app/home_page.dart';
void main() => runApp(new MyApp());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment