I have a function that builds a widget to be able to reuse it, and in that function I want to set the text theme. My problem is that to do that I need to access the BuildContext to do so. The only way I can think of is to pass it as a parameter in every function call, but it feels like there must be a simpler method. Or is this the best way to do it?
Here is the current code:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:snapping_page_scroll/snapping_page_scroll.dart';
void main() => runApp(MaterialApp(
theme: ThemeData(
backgroundColor: Color(0xff121217),
textTheme: TextTheme(
headline: TextStyle(fontSize: 40)
)
),
home: SplashScreen(),
));
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
loadData();
}
//Simulates loading of data
Future<Timer> loadData() async {
return new Timer(Duration(seconds: 1), onDoneLoading);
}
onDoneLoading() async {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => Home()));
}
@override
Widget build(BuildContext context) {
return Container(
color: Color(0xff121217),
child: Padding(
padding: const EdgeInsets.all(20),
child: Image.asset('assets/logo.png'),
),
);
}
}
class Home extends StatelessWidget {
Widget page(text, color, context){
return Container(
color: color,
child: Align(
alignment: Alignment(0, 0.5),
child: Text(text, style: Theme.of(context).textTheme.headline,),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: SnappingPageScroll(
children: <Widget>[
page('Page 1', Colors.orange, context),
page('Page 2', Colors.green, context),
],
),
),
);
}
}


