Skip to content

Instantly share code, notes, and snippets.

@RobertBrunhage
Last active August 20, 2022 14:40
Show Gist options
  • Save RobertBrunhage/7131854b077105763b8b53532069ef09 to your computer and use it in GitHub Desktop.
Save RobertBrunhage/7131854b077105763b8b53532069ef09 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(HomePage());
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
final mainKey = GlobalKey();
final servicesKey = GlobalKey();
final portfolioKey = GlobalKey();
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(actions: [
ElevatedButton(
onPressed: () {
Scrollable.ensureVisible(portfolioKey.currentContext!);
},
child: const Text('Scroll To Portfolio',
style: TextStyle(color: Colors.yellow)),
),
]),
body: SingleChildScrollView(
child: Column(children: [
MainSection(key: mainKey),
ServicesSection(key: servicesKey),
PortfolioSection(key: portfolioKey, mainSectionKey: mainKey),
]),
),
));
}
}
class MainSection extends StatefulWidget {
const MainSection({Key? key}) : super(key: key);
@override
State<MainSection> createState() => MainSectionState();
}
class MainSectionState extends State<MainSection> {
@override
Widget build(BuildContext context) {
return Container(
height: 800,
width: double.infinity,
color: Colors.blue,
child: Text(
'Hello, Main!',
style: Theme.of(context).textTheme.headline4,
));
}
}
class ServicesSection extends StatefulWidget {
const ServicesSection({Key? key}) : super(key: key);
@override
State<ServicesSection> createState() => _ServicesSectionState();
}
class _ServicesSectionState extends State<ServicesSection> {
@override
Widget build(BuildContext context) {
return Container(
height: 800,
width: double.infinity,
color: Colors.red,
child: Text(
'Hello, Services!',
style: Theme.of(context).textTheme.headline4,
));
}
}
class PortfolioSection extends StatefulWidget {
const PortfolioSection({Key? key, required this.mainSectionKey}) : super(key: key);
final GlobalKey mainSectionKey;
@override
State<PortfolioSection> createState() => _PortfolioSectionState();
}
class _PortfolioSectionState extends State<PortfolioSection> {
@override
Widget build(BuildContext context) {
return Container(
height: 650,
width: double.infinity,
color: Colors.green,
child: Column(
children: [
Expanded(child: Text('Portfolio Section')),
ElevatedButton(
onPressed: () {
// i want to scroll to MainSection when pressing the button but
// in my flutter project im getting "null Expression called on a null value" error
// for the "_globalKey.currentContext!" call
Scrollable.ensureVisible(widget.mainSectionKey.currentContext!);
},
child: Text('Scroll To Main!'),
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment