【问题标题】:Using BuildContext outside build function在构建函数之外使用 BuildContext
【发布时间】:2019-11-04 22:04:15
【问题描述】:

我有一个构建小部件以便能够重用它的函数,并且在该函数中我想设置文本主题。我的问题是,我需要访问 BuildContext 才能做到这一点。我能想到的唯一方法是在每个函数调用中将其作为参数传递,但感觉必须有一个更简单的方法。或者这是最好的方法吗?

这是当前代码:

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

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以使用构建器小部件。 ... https://api.flutter.dev/flutter/widgets/Builder-class.html

    【讨论】:

      【解决方案2】:

      您可以在 build 方法中访问 BuildContext。

      所以你可以在 build 中移动你的 place() 函数。

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

      在像您这样的情况下,一个好的模式是创建您自己的可重用页面小部件并通过构造函数传递您的参数。

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

      【讨论】:

        【解决方案3】:

        将无状态小部件转换为有状态小部件:)

        错误消失了 :)

        【讨论】:

          猜你喜欢
          • 2018-02-20
          • 2021-06-07
          • 2021-03-18
          • 1970-01-01
          • 2018-06-02
          • 2016-07-16
          • 2022-08-24
          • 2021-02-24
          • 2012-11-10
          相关资源
          最近更新 更多