1.Align组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ALign组件',
home: Scaffold(
appBar: AppBar(
title: Text('Align组件')
),
body: Container(
width: 200.0,
height: 300.0,
color: Colors.red,
child: Align(
alignment: Alignment(0, 1.0),
widthFactor: 3.0,
heightFactor: 4.0,
child: Container(
color: Colors.green,
width: 100.0,
height: 50.0,
child: Text(
'bottomCentent',
style: TextStyle(
color: Colors.white
)
)
),
)
)
),
);
}
}
复制代码
2.AppBar
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ALign组件',
home: Scaffold(
appBar: AppBar(
title: Text('Align组件')
),
body: DemoPage()
),
);
}
}
class DemoPage extends StatefulWidget {
@override
_DemoPageState createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage>{
@override
Widget build(BuildContext context) {
return SizedBox(
height: 200.0,
child: AppBar(
title: Text('应用'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.print),
onPressed: null,
tooltip: '打印',
),
IconButton(
icon: Icon(Icons.plus_one),
onPressed: null,
tooltip: '更多',
),
IconButton(
icon: Icon(Icons.share),
onPressed: null,
tooltip: '分享',
),
],
),
);
}
}
复制代码
import 'package:flutter/material.dart';
void main(){
runApp(
MyApp()
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ALign组件',
home: Scaffold(
appBar: AppBar(
title: Text('标题'),
leading: Icon(Icons.home),
backgroundColor: Colors.green,
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.share),
onPressed: null,
tooltip: '分享',
),
PopupMenuButton<String>(
itemBuilder: (BuildContext content) => <PopupMenuItem<String>>[
PopupMenuItem<String>(
value: 'friend',
child: Text('分享到朋友圈'),
),
PopupMenuItem<String>(
value: 'download',
child: Text('下载到本地'),
)
],
)
],
),
body: DemoPage()
),
);
}
}
class DemoPage extends StatefulWidget {
@override
_DemoPageState createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage>{
@override
Widget build(BuildContext context) {
return SizedBox(
height: 500,
child: AppBar(
title: Text('标题'),
leading: Icon(Icons.home),
backgroundColor: Colors.green,
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.share),
onPressed: null,
tooltip: '分享',
),
PopupMenuButton<String>(
itemBuilder: (BuildContext content) => <PopupMenuItem<String>>[
PopupMenuItem<String>(
value: 'friend',
child: Text('分享到朋友圈'),
),
PopupMenuItem<String>(
value: 'download',
child: Text('下载到本地'),
)
],
)
],
),
);
}
}
复制代码
3.BottomAppBar组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BottomAppBar组件',
home: Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: null,
),
bottomNavigationBar: BottomAppBar(
notchMargin: 10.0,
color: Colors.pink,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.menu),
onPressed: (){},
),
IconButton(
icon: Icon(Icons.search),
onPressed: (){},
)
]
),
),
body: null
),
);
}
}
复制代码
4.ButtonBar组件
@override
Widget build(BuildContext context) {
return Center(
child: ButtonBar(
alignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
child: Text('首页'),
color: Colors.yellowAccent,
onPressed: null
),
RaisedButton(
child: Text('发布'),
color: Colors.yellow,
onPressed: null
),
RaisedButton(
child: Text('个人'),
color: Colors.yellowAccent,
onPressed: null
)
]
),
);
}
复制代码
5.FlexibleSpace组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'FlexibleSpace组件',
home: Scaffold(
appBar: AppBar(
title: Text('FlexibleSpace组件')
),
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled){
return <Widget>[
SliverAppBar(
expandedHeight: 150.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text(
'可折叠组件',
style: TextStyle(
color: Colors.white
),
),
background: Image.network('https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1590326522&di=83b46be42e8b62d86b43ecc4d9731e17&src=http://www.bizhidaquan.com/d/file/1/1159829.jpg'),
)
)
];
},
body: Center(
child: Text('向上拉')
)
)
),
);
}
}
复制代码
6.SliverAppBar组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SliverAppBar组件',
home: Scaffold(
appBar: AppBar(
title: Text('SliverAppBar组件')
),
body: Domes()
),
);
}
}
class Domes extends StatelessWidget {
@override
Widget build(BuildContext context) {
return NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxScrolled){
return <Widget>[
SliverAppBar(
primary: true,
leading: Icon(Icons.hdr_off),
actions: <Widget>[
Icon(Icons.add)
],
elevation: 10.0,
pinned: false,
expandedHeight: 200.0,
snap: false,
floating: false,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text(
'这是一个标题',
style: TextStyle(
color: Colors.yellow,
fontSize: 16.0
)
),
background: Image.network(
'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1590337388247&di=6637f1e38c9da6fa343735f749080e24&imgtype=0&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D3004831859%2C2611273449%26fm%3D214%26gp%3D0.jpg'),
),
)
];
},
body: Center(
child: Text('拖动')
)
);
}
}
复制代码
7.SnackBar组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SnackBar组件',
home: Scaffold(
appBar: AppBar(
title: Text('SnackBar组件')
),
body: Domes()
),
);
}
}
class Domes extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: GestureDetector(
onTap:(){
final snackBar = SnackBar(
content: Text('文字提示'),
backgroundColor: Colors.green,
action: SnackBarAction(
textColor: Colors.white,
label: '取消',
onPressed: (){
}
),
duration: Duration(minutes: 1),
);
Scaffold.of(context).showSnackBar(snackBar);
},
child: Text('显示屏幕底部消息'),
)
);
}
}
复制代码
8.TabBar组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TabBar组件',
home: Scaffold(
appBar: AppBar(
title: Text('TabBar组件')
),
body: DemoPage()
),
);
}
}
class DemoPage extends StatefulWidget {
@override
_DemoPageState createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> with SingleTickerProviderStateMixin {
ScrollController _scrollViewController;
TabController _tabController;
@override
void initState(){
super.initState();
_scrollViewController = ScrollController();
_tabController = TabController(vsync: this, length: 6);
}
@override
void dispose(){
super.initState();
_scrollViewController.dispose();
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: 500.0,
child: Scaffold(
appBar: AppBar(
title: Text('选项卡'),
leading: Icon(Icons.home),
backgroundColor: Colors.blue,
bottom: TabBar(
controller: _tabController,
isScrollable: true,
tabs: <Widget>[
Tab(
text: "选项1",
icon: Icon(Icons.home),
),
Tab(
text: "选项2"
),
Tab(text: "选项3"),
Tab(text: "选项4"),
Tab(text: "选项5"),
Tab(text: "选项6"),
]
),
),
body: TabBarView(controller: _tabController,
children: <Widget>[
Text('选项1'),
Text('选项2'),
Text('选项3'),
Text('选项4'),
Text('选项5'),
Text('选项6')
]
),
),
);
}
}
复制代码
9.BottomNavigationBar组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BottomNavigationBar组件',
home: Scaffold(
appBar: AppBar(
title: Text('BottomNavigationBar组件')
),
body: Container(
width: 200.0,
height: 500.0
),
bottomNavigationBar: DemoPage(),
),
);
}
}
class DemoPage extends StatefulWidget {
@override
_DemoPageState createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
int _currentIndex = 1;
void _onItemTapped(int index){
print(index);
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
iconSize: 24.0,
onTap: _onItemTapped,
currentIndex: _currentIndex,
fixedColor: Colors.red,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('聊天'),
icon: Icon(Icons.home)
),
BottomNavigationBarItem(
title: Text('通信录'),
icon: Icon(Icons.home)
),
BottomNavigationBarItem(
title: Text('个人'),
icon: Icon(Icons.home)
)
]
);
}
}
复制代码
10.BottomNavigationBar组件
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BottomNavigationBar组件',
home: Scaffold(
appBar: AppBar(
title: Text('BottomNavigationBar组件')
),
body: Center(
child: Column(
children: <Widget>[
ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: 100.0,
minHeight: 20.0,
maxWidth: 200.0,
minWidth: 60.0
),
child: Container(
width: 150.0,
height: 100,
color: Colors.blue,
child: Text(
'Width>maxWidth',
style: TextStyle(
color: Colors.yellow
)
)
),
)
]
)
),
bottomNavigationBar: DemoPage(),
),
);
}
}
复制代码