Flutter基础功能插件app_fundamentals的使用
Flutter基础功能插件app_fundamentals的使用
开始使用
使用null-safety
使用方法
如果你正在开发一个复杂的Flutter应用,并且需要通过最简单的方式构建基础的UI组件,那么你可能需要使用app_fundamentals
插件。
这是你可以尝试的一些可用组件:
在你的项目中添加依赖项并开始使用app_fundamentals
:
dependencies:
app_fundamentals: ^0.0.20
导入包:
import 'package:app_fundamentals/app_fundamentals.dart';
示例
让我们以BaseStatefulWidget
为例。如果你扩展BaseStatefulWidget
来构建你的屏幕,你可以重写多个方法,以便更轻松地创建新的屏幕。
class MyPage extends BaseStatefulWidget {
const MyPage({super.key});
@override
BaseState<MyPage> createState() => _MyPageState();
}
class _MyPageState extends BaseState<MyPage> {
@override
void initState() {
super.initState();
// 初始化代码
}
@override
Widget setBody(BuildContext context) {
return Container(); // 设置页面主体
}
@override
String? setScaffoldBackgroundImage() {
return ""; // 设置背景图片路径
}
@override
Widget? setBottomNavigationBar() {
return Container(); // 设置底部导航栏
}
@override
Future<bool> setOnWillPop() {
return Future.value(true); // 设置返回按钮行为
}
@override
bool setResizeToAvoidBottomInset() {
return false; // 设置是否避免底部输入框
}
}
作为另一个例子,app_fundamentals
提供了 NavBarWidget
和 NavBarBloc
来简化底部导航栏的使用。你只需要像这样在你的 StatefulWidget
中使用 NavBarWidget
作为底部导航栏:
@override
Widget? setBottomNavigationBar() {
return NavBarWidget(
height: 64.0,
textFontSize: 14.0,
background: Colors.white,
borderColor: Colors.black,
selectedColor: Colors.blue,
unSelectedColor: Colors.white,
selectedFontWeight: FontWeight.w700,
unSelectedFontWeight: FontWeight.w900,
// 其他配置参数
);
}
你也应该使用 navBarBloc
来识别你的导航栏项列表:
navBarBloc.naveBarItemList = [
NavBarItem(
title: "Home",
widget: Container(),
selectedIcon: SvgPicture.asset(
selectedHomeSvg,
width: 24.0,
height: 24.0,
),
unSelectedIcon: SvgPicture.asset(
'assets/svg/home.svg',
width: 24.0,
height: 24.0,
),
onTap: () {
debugPrint('index 0');
},
),
// 其他导航项
];
其他组件的示例如下:
// 按钮
AppButton(
title: 'Back',
alignment: AppButtonAlign.start,
icon: SvgPicture.asset('assets/svg/backButtonIcon.svg'),
onTap: () => Navigator.pop(context)
);
// 文本
AppText(
label: 'Text',
textAlign: TextAlign.right,
style: TextStyle(
color: Colors.red,
fontSize: 14,
fontWeight: FontWeight.bold,
)
);
// 文本输入框
AppTextFormFieldItem(
controller: TextEditingController(),
readOnly: stateSnapshot.data?.status == RequestStatus.loading ? true : false,
autofocus: true,
title: AppLocalizations.of(context).email,
formFieldItemType: AppFormFieldItemType.email,
stream: _signInBloc.emailStream,
onChanged: _signInBloc.onChangedEmailField,
textInputType: TextInputType.emailAddress,
labelFontColor: Colors.black,
borderColor: Colors.grey,
focusedBorderColor: Colors.grey,
iconColor: Colors.black,
focusedIconColor: Colors.grey,
fillColor: Colors.white,
showHint: true,
validator: (val) => _signInBloc.validateEmail(val),
fontSize: 14.0,
contentPadding: EdgeInsets.symmetric(
horizontal: 8.0,
vertical: 8.0
),
borderRadius: BorderRadius.all(Radius.circular(8.0))
);
// 带边框的下拉菜单
AppBorderedDropDown(
list: ["first", "second", "third"],
selectedStateSubject: selectedStateSubject,
selectedSubject: selectedSubject,
borderColorSubject: borderColorSubject,
onTap: (){},
onChange: (val){},
titleKey: "",
label: "",
hint: ""
);
// 底部弹出窗口
AppBottomSheet(context: context, widget: Container());
// 带标签的容器
AppContainerWithLabel(label: Text("label"), child: widget);
// 对话框
final AppDialog appDialog = AppDialog();
appDialog.child = AppDialogContent(title: "title", description: "description");
Future<void> showAppDialog(AppDialog appDialog) async {
await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) => appDialog
);
}
Future<void>? hideAppDialog(AppDialog appDialog) {
if (appDialog.isShowing()) {
appDialog.pop();
appDialog = AppDialog();
}
return null;
}
// 分割线
AppDivider(
dividerPadding: EdgeInsets.symmetric(horizontal: 16.0),
thickness: 2.0,
dividerColor: Colors.grey
);
// 扩展的单选按钮
AppExpandedRadioButton(
onTap: () {},
label: "label",
labelFontSize: 24.0,
labelFontColor: Colors.black,
isSelected: true
);
// 图片
AppImage(
path: "image_path",
height: 40.0,
width: 40.0,
isCircular: true,
boxFit: BoxFit.cover,
color: Colors.white,
onPressed: () {},
defaultImage: AppImage(path: "default_image_path"),
loadingWidget: LoadingWidget()
);
// 带图标的标签
AppLabelWithIcon(
label: "label",
labelColor: Colors.black,
icon: Icon(Icons.search)
);
// 下拉刷新指示器
AppRefreshIndicator(
child: Container(),
onRefresh: () {}
);
// 网页视图
AppWebView(
title: "title",
url: ""
);
// 加密工具
final Encryptor encryptor = Encryptor();
encryptor.encryptData("hello");
encryptor.decryptData("efgdt-vvxvxxvxvxfgfg-bcbcbcbcbcbcbcbcbcbcb");
更多关于Flutter基础功能插件app_fundamentals的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter基础功能插件app_fundamentals的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter开发中,app_fundamentals
并不是一个官方的或广泛认可的包名。然而,从帖子内容来看,它可能指的是一系列用于实现Flutter应用基础功能的插件集合。为了展示如何使用这些基础功能插件,我将列举几个常见的Flutter插件及其基本用法,这些插件通常用于实现如网络请求、状态管理、本地存储等基础功能。
1. 网络请求:http
包
http
包是Flutter中用于发送HTTP请求的一个常用库。以下是一个简单的GET请求示例:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Network Request'),
),
body: Center(
child: FutureBuilder<String>(
future: fetchData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return Text('Data: ${snapshot.data}');
}
} else {
return CircularProgressIndicator();
}
},
),
),
),
);
}
Future<String> fetchData() async {
final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/todos/1'));
if (response.statusCode == 200) {
return response.body;
} else {
throw Exception('Failed to load data');
}
}
}
2. 状态管理:provider
包
provider
包是Flutter中用于状态管理的一个流行库。以下是一个简单的示例,展示如何使用provider
来管理应用状态:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => Counter()),
],
child: MyApp(),
),
);
}
class Counter with ChangeNotifier {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Provider Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Consumer<Counter>(
builder: (context, counter, child) => Text(
'${counter.count}',
style: Theme.of(context).textTheme.headline4,
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
final counter = Provider.of<Counter>(context, listen: false);
counter.increment();
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
),
);
}
}
3. 本地存储:shared_preferences
包
shared_preferences
包是Flutter中用于本地存储的一个常用库。以下是一个简单的示例,展示如何使用shared_preferences
来存储和读取数据:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter SharedPreferences Example'),
),
body: SharedPreferencesDemo(),
),
);
}
}
class SharedPreferencesDemo extends StatefulWidget {
@override
_SharedPreferencesDemoState createState() => _SharedPreferencesDemoState();
}
class _SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
String _counterValue = '0';
@override
void initState() {
super.initState();
_readCounter();
}
Future<void> _incrementCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int counter = (prefs.getInt('counter') ?? 0) + 1;
await prefs.setInt('counter', counter);
setState(() {
_counterValue = counter.toString();
});
}
Future<void> _readCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int counter = prefs.getInt('counter') ?? 0;
setState(() {
_counterValue = counter.toString();
});
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
_counterValue,
style: Theme.of(context).textTheme.headline4,
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
以上代码展示了如何使用http
包进行网络请求,provider
包进行状态管理,以及shared_preferences
包进行本地存储。这些插件是实现Flutter应用基础功能的关键组件。