Flutter project basic development kit

flutter_common

The base library of the flutter project.

Features

  • package of provider
  • Package used by provider in combination with widget
  • Empty page, error page handling
  • Dio package
  • Introduction of common basic plugins, such as pull_to_refresh, sp, toast, url_launcher, etc.

use

Download the project locally, and then import it in the pubspec.yaml file, such as I put it in the same level directory as the project

dependencies:
  flutter_common:
    git:
      url: https://github.com/jingzhanwu/flutter_common.git

dio initialization

initDio(baseUrl: "http://baidu.com");

StorageManager initialization

/// 初始化本地存储
await StorageManager.init();

MaterialApp configuration

Including OKToast, Provider, Refresh and other configurations, of which OKToast and Refresh are necessary, otherwise errors will occur when using toast and list refresh on the page, and the provider configures it according to its own business needs.

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return OKToast(
      child: MultiProvider(
        providers: [
          ChangeNotifierProvider<ThemeModel>(
            create: (context) => ThemeModel(),
          )
        ],
        child: Consumer<ThemeModel>(
          builder: (context, model, child) {
            return RefreshConfiguration(
              hideFooterWhenNotFull: false,
              child: MaterialApp(
                debugShowCheckedModeBanner: true,
                locale: Locale('zh'),
                theme: model.themeData(),
                localizationsDelegates: [
                  GlobalWidgetsLocalizations.delegate,
                  GlobalMaterialLocalizations.delegate,
                  StringsLocalizationsDelegate.delegate,
                ],
                supportedLocales:
                    StringsLocalizationsDelegate.delegate.supportedLocales,
                home: HomePage(),
              ),
            );
          },
        ),
      ),
    );
  }
}

ProviderWidgetUse in the page

The generic type of ProviderWidget is Provider type

class AdvertListPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var title = Strings.of(context).advertListTitle;
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: ProviderWidget<AdvertModel>(
        model: AdvertModel(),
        onModelReady: (model) => model.initData(),
        builder: (context, model, child) {
          if (model.isLoading) {
            return LoadingStateWidget();
          }
          if (model.isEmpty) {
            return EmptyStateWidget();
          }
          if (model.isError) {
            return ErrorStateWidget(
              error: model.pageErrorState,
              message: '数据解析出错了',
            );
          }
          return ListView.builder(
              itemCount: model.list.length,
              //类IOS 回弹效果,item不足一屏时,设置parent即可
              physics: BouncingScrollPhysics(
                  parent: AlwaysScrollableScrollPhysics()),
              itemBuilder: (context, index) {
                return _listItem(context, model.list[index]);
              });
        },
      ),
    );
  }

  Widget _listItem(BuildContext context, AdvertEntry ad) {
    return Ink(
      color: Colors.white,
      child: InkWell(
        onTap: () {
          showToast("当前id=${ad.id}");
        },
        child: Column(
          children: [
            Container(
              width: double.infinity,
              padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("广告id:${ad.id}"),
                  Text(
                    "链接:${ad.adLink}",
                    style: TextStyle(
                        fontSize: 12.0, color: Theme.of(context).hintColor),
                  ),
                ],
              ),
            ),
            Divider(height: 0.7),
          ],
        ),
      ),
    );
  }
}

Download Details:

Author: jingzhanwu

Source Code: https://github.com/jingzhanwu/flutter_common

#flutter #dart #mobile-apps

Flutter project basic development kit
2.60 GEEK