一个提供基础工具的包,例如主题、常用 BuildContext 扩展和简单的错误页面 widget。
非常适合用作作品集来创建大量 flutter 应用。
特点
-
厌倦了手动定义主题?直接使用此包中的默认 ThemeData 即可!
import 'package:core/core.dart' as core; class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: core.appName, theme: core.AppThemes.light, darkTheme: core.AppThemes.dark, ... ); } }
-
需要 Context 的常用简写?尝试使用此包中的 BuildContext 扩展!
import 'package:core/core.dart'; class MyWidget extends StatelessWidget { const MyWidget({super.key}); @override Widget build(BuildContext context) { // Shorthand for Theme.of(context) context.theme; // Shorthand for MediaQuery.of(context) context.mediaQuery; // Shorthand for Scaffold.of(context) context.scaffoldState; // Check "src/utils/extensions.dart" for more return Container(); } }
-
需要一个简单的页面来显示错误信息?尝试使用包中的
ErrorPages!import 'package:core/core.dart'; // Will display status code 400 with 'Bad Request' text below it. ErrorPages.client.badRequest(); // Will display status code 500 with 'Internal Server Error' text below it. ErrorPages.server.internalServerError(); // Will display page based on requested status code. // // The Example below will display status code 404 with // 'Not Found' text below it. ErrorPages.byCode(404); // You can also add custom message to the page // (Default message is 'Sorry for the inconvenience'). // // The message displayed below statusCode and name. ErrorPages.client.notFound(message: 'The Page You Requested Not Found'); ErrorPages.byCode(403, message: 'You are not allowed to access this'); // See preview below for page visual.
错误页面预览
入门
-
将包添加到您的依赖项
dependencies: core: git: url: https://github.com/KeidsID/flutter_app_core_package.git
-
然后,你只需要在
main()中调用一次init()。import 'package:core/core.dart' as core; void main() { core.init(); ... }
你也可以通过
init()自定义主题。import 'package:flutter/material.dart'; import 'package:core/core.dart' as core; void main() { core.init( appName: 'My App Name', appPrimaryColor: Colors.amber, useMaterial3: true, ); ... }
现在,你已准备好使用此包的实用程序。
用法
main.dart
import 'package:flutter/material.dart';
import 'package:core/core.dart' as core;
void main() {
core.init(
appName: 'Flutter Demo',
useMaterial3: true,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: core.appName, // "Flutter Demo"
// Both theme used material 3
theme: core.AppThemes.light,
darkTheme: core.AppThemes.dark,
themeMode: ThemeMode.system,
// Simple page to display 404 Not Found
home: core.ErrorPages.client.notFound(),
);
}
}
