Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using BuildContext outside build function

Tags:

flutter

dart

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),
          ],
        ),
      ),
    );
  }
}

like image 360
Hannes Hultergård Avatar asked Nov 04 '19 22:11

Hannes Hultergård


2 Answers

You have access to BuildContext inside build method.

So you can move your place() function inside build.

Widget build(BuildContext context) {
  page(text, color) {
    return Container(
      color: color,
      child: Align(
        alignment: Alignment(0, 0.5),
        child: Text(
          text,
          style: Theme.of(context).textTheme.headline,
        ),
      ),
    );
  }

  return MaterialApp(
    home: Scaffold(
      appBar: AppBar(),
      body: Column(
        children: <Widget>[
          page('Page 1', Colors.orange),
          page('Page 2', Colors.green),
        ],
      ),
    ),
  );
}

Also in cases like yours a good pattern would be to create your own reusable Page widget and pass your parameters via constructor.

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(),
        body: Column(
          children: <Widget>[
            _Page(text: 'Page 1', color: Colors.orange),
            _Page(text: 'Page 2', color: Colors.green),
          ],
        ),
      ),
    );
  }
}

class _Page extends StatelessWidget {
  const _Page({
    Key key,
    @required this.color,
    @required this.text,
  }) : super(key: key);

  final Color color;
  final String text;
  @override
  Widget build(BuildContext context) {
    return Container(
      color: color,
      child: Align(
        alignment: Alignment(0, 0.5),
        child: Text(
          text,
          style: Theme.of(context).textTheme.headline,
        ),
      ),
    );
  }
}
like image 59
lazos Avatar answered Sep 19 '22 13:09

lazos


You can use builder widget. ... https://api.flutter.dev/flutter/widgets/Builder-class.html

like image 27
Filip P. Avatar answered Sep 20 '22 13:09

Filip P.