flutter-packages icon indicating copy to clipboard operation
flutter-packages copied to clipboard

[Proposal] Add function to await any pending font loads.

Open johnsonmh opened this issue 3 years ago • 4 comments

The addition of the pendingFontLoads function would allow users to request multiple google fonts, then asynchronously wait for the fonts to be loaded. For example, the following snippet will show a progress indicator until the fonts have finished loading, then it will load the page.

https://github.com/material-foundation/google-fonts-flutter/issues/151

giphy-6

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    GoogleFonts.oswald();
    GoogleFonts.pacifico();
    return MaterialApp(
      home: FutureBuilder(
        future: GoogleFonts.pendingFontLoads(),
        builder: (context, snapshot) {
          if (snapshot.connectionState != ConnectionState.done) {
            return Center(child: CircularProgressIndicator());
          }
          return MyHomePage(title: 'Demo');
        },
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    final TextStyle headline4 = Theme.of(context).textTheme.headline4!;
    return Scaffold(
      appBar: AppBar(
        title: Text(title, style: GoogleFonts.pacifico()),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'This is a google font',
              style: GoogleFonts.oswald(textStyle: headline4),
            ),
          ],
        ),
      ),
    );
  }
}

Possible improvements would be:

  • Making it so that the user can tell which font loads when.
  • Making it so that the user doesn't have to call all the fonts before hand, maybe add a way to pass in the fonts they want to load.

johnsonmh avatar Nov 15 '21 23:11 johnsonmh

instead of return a Future of List of fonts, can this return a List of Future fonts? Also, consider renaming to `loadedFonts, since they aren't necessarily pending when this is called

clocksmith avatar Nov 17 '21 15:11 clocksmith

Hey @johnsonmh and @guidezpl, I would really appreciate if someone had the time to finish this PR in the near future.

It's an awesome feature, and you are doing amazing work 💙😄

jhb-dev avatar Feb 04 '22 16:02 jhb-dev

Get me out of here!!

therealjbox avatar Mar 07 '22 01:03 therealjbox

@johnsonmh is this still valid?

clocksmith avatar May 02 '22 15:05 clocksmith

heyla! it's approved!! pls merge it

lpongetti avatar Oct 07 '22 13:10 lpongetti

Hey @guidezpl, can you take another pass at this when you get a chance? Thanks!

esouthren avatar Nov 07 '22 13:11 esouthren

It's been 2 years. What happened? Can we merge it? 😄

arthurbcd avatar May 15 '23 13:05 arthurbcd

Generally how long does it take for a merge like this to be available in the package?

noahjoshua avatar Jun 07 '23 13:06 noahjoshua

I'm going to fix https://github.com/material-foundation/flutter-packages/issues/102 and then release :)

guidezpl avatar Jun 07 '23 18:06 guidezpl