打开vscode命令面板(command+shift+p)选择Flutter:new project,然后输入项目名称选择项目保存路径。
android目录
ios目录
lib目录
test目录
pubspec.yaml文件
void main(){
runApp(MyApp())
}
//简写
void main()=>runApp(MyApp())
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('111'),
),
),
);
home参数是App默认显示的页面
MaterialApp:常用属性
home:主页,
title:标题
color:颜色,
theme:主题,
routes:路由
Scaffold实现了Material风格的基本布局结构
appBar–显示在界面顶部的一个AppBar
body–当前界面所显示的主要内容
drawer – 抽屉菜单控件
Scaffold(
appBar: AppBar(
title: Text('111'),
),
body: Center(
child: Text('一枚有态度的程序员'),
),
)
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
// build 返回一个Widget 一个类就是widget
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('111'),
),
body: Center(
child: Text('一枚有态度的程序员'),
),
),
);
}
}
assets:
- assets/images/
Image.asset('assets/images/home.png')
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
MyApp({
Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
int _currentIndex = 0;
Widget _currBody = HomePage();
_onTap(int index) {
switch (index) {
case 0:
_currBody = HomePage();
break;
case 1:
_currBody = BookPage();
break;
case 2:
_currBody = MyPage();
break;
}
setState(() {
_currentIndex = index;
});
}
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('练习')),
body: _currBody,
bottomNavigationBar: BottomNavigationBar(
onTap: _onTap,
selectedItemColor: Color(0xFFff5656),
unselectedItemColor: Colors.black,
currentIndex: _currentIndex,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('首页'),
icon: Image.asset(
'assets/images/home.png',
width: 24,
height: 24,
),
activeIcon: Image.asset(
'assets/images/homeSelected.png',
width: 24,
height: 24,
),
),
BottomNavigationBarItem(title: Text('书籍'), icon: Icon(Icons.book)),
BottomNavigationBarItem(
title: Text('我的'), icon: Icon(Icons.perm_identity)),
],
),
));
}
}
class HomePage extends StatelessWidget {
const HomePage({
Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Text('13331'),
);
}
}
class BookPage extends StatelessWidget {
const BookPage({
Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Text('111'),
);
}
}
class MyPage extends StatelessWidget {
const MyPage({
Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Text('22'),
);
}
}
import 'package:flutter/material.dart';
class Tabs extends StatefulWidget {
Tabs({
Key key}) : super(key: key);
@override
_TabsState createState() => _TabsState();
}
class _TabsState extends State<Tabs> {
@override
int _currentIndex = 0;
Widget _currBody = HomePage();
_onTap(int index) {
switch (index) {
case 0:
_currBody = HomePage();
break;
case 1:
_currBody = BookPage();
break;
case 2:
_currBody = MyPage();
break;
}
setState(() {
_currentIndex = index;
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('练习demo')),
body: _currBody,
bottomNavigationBar: BottomNavigationBar(
onTap: _onTap,
selectedItemColor: Color(0xFFff5656),
unselectedItemColor: Colors.black,
currentIndex: _currentIndex,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('首页'),
icon: Image.asset(
'assets/images/home.png',
width: 24,
height: 24,
),
activeIcon: Image.asset(
'assets/images/homeSelected.png',
width: 24,
height: 24,
),
),
BottomNavigationBarItem(title: Text('书籍'), icon: Icon(Icons.book)),
BottomNavigationBarItem(
title: Text('我的'), icon: Icon(Icons.perm_identity)),
],
),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({
Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Text('13331'),
);
}
}
class BookPage extends StatelessWidget {
const BookPage({
Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Text('111'),
);
}
}
class MyPage extends StatelessWidget {
const MyPage({
Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Text('22'),
);
}
}
import 'package:flutter/material.dart';
import 'pages/tabs/tabs.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({
Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(home: Tabs());
}
}
pub.dev
flutter_swiper: ^1.1.6
import 'package:flutter_swiper/flutter_swiper.dart';
new Swiper(
itemBuilder: (BuildContext context,int index){
return new Image.network("http://via.placeholder.com/350x150",fit: BoxFit.fill,);
},
itemCount: 3,
pagination: new SwiperPagination(),
control: new SwiperControl(),
),
官网教程