Flutter UI框架插件flutter_bootstrap的使用
Flutter UI框架插件flutter_bootstrap的使用
flutter_bootstrap
flutter_bootstrap
是Flutter中Bootstrap网格系统的部分实现,用于响应式布局。
Getting Started
要在你的Flutter项目中使用 flutter_bootstrap
,你需要确保在项目的依赖中添加以下内容:
dependencies:
flutter_bootstrap: "^2.0.0"
然后运行 flutter packages upgrade
或者在IntelliJ中更新你的包。
在Dart代码中使用它:
import 'package:flutter_bootstrap/flutter_bootstrap.dart';
Documentation
这个包暴露了4个新的 Widgets:
- BootstrapContainer
- BootstrapRow
- BootstrapCol
- BootstrapVisibility
…和一系列 helper 方法:
- bootstrapGridParameters
- bootstrapPrefixBasedOnWidth
- bootstrapMaxWidthNonFluid
- bootStrapValueBasedOnSize
它实现了以下Bootstrap4特性:
- .container
- .container-fluid
- .row
- .col-, .col-sm-, .col-md-, .col-lg-, .col-xl-*
- .offset-, .offset-sm-, .offset-md-, .offset-lg-, .offset-xl-*
- .order-, .order-sm-, .order-md-, .order-lg-, .order-xl-*
- 基于设备宽度的条件可见性和列定义
完整的文档和示例可以在这里找到。
Example
下面是一个完整的示例demo,展示了如何使用 flutter_bootstrap
创建一个简单的响应式布局:
import 'package:flutter/material.dart';
import 'package:flutter_bootstrap/flutter_bootstrap.dart';
void main() => runApp(Application());
class Application extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter_bootstrap Demo',
home: DemoPage(),
);
}
}
class DemoPage extends StatefulWidget {
@override
_DemoPageState createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
@override
void initState() {
super.initState();
bootstrapGridParameters(
gutterSize: 30,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('flutter_bootstrap Demo', style: TextStyle(color: Colors.black)),
backgroundColor: Colors.white,
),
body: SingleChildScrollView(
child: BootstrapContainer(
fluid: true,
decoration: BoxDecoration(color: Colors.blue),
children: [
BootstrapContainer(
fluid: false,
decoration: BoxDecoration(color: Colors.white),
padding: const EdgeInsets.only(top: 50),
children: <Widget>[
BootstrapRow(
height: 60,
children: <BootstrapCol>[
BootstrapCol(
sizes: 'col-6',
child: ContentWidget(
text: 'col 1 of 2',
color: Colors.red,
),
),
BootstrapCol(
sizes: 'col-6',
child: ContentWidget(
text: 'col 2 of 2',
color: Colors.red,
),
),
],
),
SizedBox(height: 8),
BootstrapRow(
height: 60,
children: <BootstrapCol>[
BootstrapCol(
sizes: 'col-3',
child: ContentWidget(
text: 'col 1 of 3',
color: Colors.green,
),
),
BootstrapCol(
sizes: 'col-6',
child: ContentWidget(
text: 'col 2 of 3 (wider)',
color: Colors.red,
),
),
BootstrapCol(
sizes: 'col-3',
child: ContentWidget(
text: 'col 3 of 3',
color: Colors.green,
),
),
],
),
SizedBox(height: 8),
Divider(),
BootstrapRow(
height: 120,
children: <BootstrapCol>[
BootstrapCol(
sizes: 'col-12 col-md-8',
child: ContentWidget(
text: 'col-12 col-md-8',
color: Colors.yellow,
),
),
BootstrapCol(
sizes: 'col-6 col-md-4',
child: ContentWidget(
text: 'col-6 col-md-4',
color: Colors.green,
),
),
],
),
Divider(),
BootstrapRow(
height: 120,
children: <BootstrapCol>[
BootstrapCol(
sizes: 'col-9',
child: ContentWidget(
text: 'col-9',
color: Colors.yellow,
),
),
BootstrapCol(
sizes: 'col-4',
child: ContentWidget(
text: 'col-4\nSince 9+4 = 13 & 13 > 2, this 4-columns gets wrapped onto a new line',
color: Colors.green,
),
),
],
),
Divider(),
BootstrapRow(
height: 60,
children: <BootstrapCol>[
BootstrapCol(
sizes: 'col-md-4',
child: ContentWidget(
text: 'col-md-8',
color: Colors.red,
),
),
BootstrapCol(
sizes: 'col-md-4',
offsets: 'offset-md-4',
child: ContentWidget(
text: 'col-md-4 offset-md-4',
color: Colors.green,
),
),
],
),
BootstrapRow(
height: 60,
children: <BootstrapCol>[
BootstrapCol(
sizes: 'col-md-3',
offsets: 'offset-md-3',
child: ContentWidget(
text: 'col-md-3 offset-md-3',
color: Colors.yellow,
),
),
BootstrapCol(
sizes: 'col-md-3',
offsets: 'offset-md-3',
child: ContentWidget(
text: 'col-md-3 offset-md-3',
color: Colors.purple,
),
),
],
),
BootstrapRow(
height: 60,
children: <BootstrapCol>[
BootstrapCol(
sizes: 'col-md-6',
offsets: 'offset-md-3',
child: ContentWidget(
text: 'col-md-6 offset-md-3',
color: Colors.yellow,
),
),
],
),
Divider(),
BootstrapRow(
height: 200,
children: <BootstrapCol>[
BootstrapCol(
sizes: 'col-12 col-md-8',
orders: 'order-2 order-sm-2 order-md-1 order-lg-1 order-xl-1',
child: ContentWidget(
text: 'col-12 col-md-8 order2(xs,sm)',
color: Colors.red,
),
),
BootstrapCol(
sizes: 'col-12 col-sm-12 col-md-6 col-lg-3 col-xl-3',
orders: 'order-1 order-sm-1 order-md-2 order-lg-2 order-xl-2',
child: ContentWidget(
text: 'col 2 order1(xs,sm)',
color: Colors.green,
),
),
BootstrapCol(
sizes: 'col-12 col-sm-12 col-md-6 col-lg-3 col-xl-3',
offsets: 'offset-0 offset-sm-0 offset-md-2 offset-lg-4 offset-xl-5',
orders: 'order-3 order-sm-3 order-md-3 order-lg-3 order-xl-3',
child: ContentWidget(
text: 'col 3',
color: Colors.yellow,
),
),
BootstrapCol(
sizes: 'col-12 col-sm-12 col-md-6 col-lg-3 col-xl-3',
orders: 'order-4 order-sm-4 order-md-4 order-lg-4 order-xl-4',
invisibleForSizes: 'sm xl',
child: ContentWidget(
text: 'invisible for sm and xl',
color: Colors.purple,
),
),
],
),
],
),
],
),
),
);
}
}
class ContentWidget extends StatelessWidget {
const ContentWidget({
Key? key,
required this.text,
required this.color,
}) : super(key: key);
final String text;
final Color color;
@override
Widget build(BuildContext context) {
return Container(
height: 50,
color: color,
child: Center(
child: Text(text, textAlign: TextAlign.center),
),
);
}
}
以上代码展示了一个包含多个响应式布局的页面,你可以根据需要调整和扩展这些布局。希望这能帮助你更好地理解和使用 flutter_bootstrap
插件。
更多关于Flutter UI框架插件flutter_bootstrap的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter UI框架插件flutter_bootstrap的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,flutter_bootstrap
是一个帮助开发者快速创建具有 Bootstrap 风格的 Flutter 应用的插件。它提供了一系列预定义的组件和样式,使得开发者可以更容易地构建美观的用户界面。以下是如何在 Flutter 项目中使用 flutter_bootstrap
插件的一个简单示例。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 flutter_bootstrap
依赖:
dependencies:
flutter:
sdk: flutter
flutter_bootstrap: ^x.y.z # 请使用最新版本号替换 x.y.z
2. 导入插件
在你需要使用 flutter_bootstrap
组件的 Dart 文件中导入插件:
import 'package:flutter_bootstrap/flutter_bootstrap.dart';
3. 使用组件
flutter_bootstrap
提供了多种组件,例如按钮、导航栏、卡片等。以下是一个简单的示例,展示如何使用这些组件:
import 'package:flutter/material.dart';
import 'package:flutter_bootstrap/flutter_bootstrap.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BootstrapApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: BootstrapAppBar(
title: Text('Flutter Bootstrap Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
BSButton(
color: BootstrapColors.primary,
onPressed: () {
// 按钮点击事件
print('Primary Button Clicked');
},
child: Text('Primary Button'),
),
SizedBox(height: 16.0),
BSButton(
color: BootstrapColors.secondary,
onPressed: () {
// 按钮点击事件
print('Secondary Button Clicked');
},
child: Text('Secondary Button'),
),
SizedBox(height: 32.0),
BSTextField(
labelText: 'Email',
placeholder: 'Enter your email',
prefixIcon: Icons.email,
),
SizedBox(height: 16.0),
BSTextField(
labelText: 'Password',
placeholder: 'Enter your password',
prefixIcon: Icons.lock,
obscureText: true,
),
SizedBox(height: 32.0),
BSNavLink.list(
links: [
BSNavLink(
title: 'Home',
icon: Icons.home,
onPressed: () {
// 导航到首页
print('Navigated to Home');
},
),
BSNavLink(
title: 'Profile',
icon: Icons.person,
onPressed: () {
// 导航到个人页面
print('Navigated to Profile');
},
),
BSNavLink(
title: 'Settings',
icon: Icons.settings,
onPressed: () {
// 导航到设置页面
print('Navigated to Settings');
},
),
],
),
],
),
),
);
}
}
4. 运行应用
确保你的 Flutter 环境已经正确配置,然后在命令行中运行以下命令来启动你的应用:
flutter run
这个示例展示了如何使用 flutter_bootstrap
插件创建一个简单的用户界面,包括带有 Bootstrap 样式的按钮、输入框和导航链接。你可以根据需要进一步自定义和扩展这些组件。