Basic Example

The basic example gives a good overview over the following features of EZ Flutter:

  • Automatic Configuration Loading
  • Global Messaging
  • Translation

Clone the example from Github or create the following files:

// Create file at assets/app.json 
{
    "foo": "This comes from the app settings!"
}

// Create file at locale/locale_de.json
{
    "hello_world": "Hallo Welt",
    "msg_hello": "Hallo, wie geht es dir?"
}

// Create file at locale/locale_en.json
{
    "hello_world": "Hello World",
    "msg_hello": "Hello, how are you?"
}
        

Copy the code in your main.dart file:

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

void main() => EzRunner.run(
      MyHomePage(),
      applicationPath: "assets/app.json",
      materialThemeData: ThemeData(primarySwatch: Colors.pink),
      locales: [Locale('en'), Locale('de')],
    );

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
  @override
  Widget build(BuildContext context) {
    return EzScaffold(
      appBar: AppBar(
        title: Text('EZ Flutter'),
      ),
      body: Container(
        color: Colors.white,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                EzTranslator.of(context).text("hello_world"),
                style: TextStyle(fontSize: 18),
              ),
              Text(
                EzSettings.app()['foo'],
                style: TextStyle(fontSize: 18),
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            child: const Text('EN'),
            onPressed: () {
              EzTranslator.load(Locale('en')).then((_) {
                setState(() {});
              });
            },
          ),
          SizedBox(width: 24),
          FloatingActionButton(
            child: const Text('DE'),
            onPressed: () {
              EzTranslator.load(Locale('de')).then((_) {
                setState(() {});
              });
            },
          ),
          SizedBox(width: 24),
          FloatingActionButton(
            child: const Icon(Icons.message),
            onPressed: () {
              EzBlocProvider.of(context)
                  .get(EzMessageBloc)
                  .addition
                  .add(EzMessage(EzTranslator.of(context).text("msg_hello"),
                      EzMessageType.SUCCESS));
            },
          ),
        ],
      ),
    );
  }
}