Flutter 代码分享(一)
2019-07-31 本文已影响0人
Victory_886
main.dart
import 'package:flutter/material.dart';
import './home/home_view.dart';
import './messaage/message.dart';
import './me/me.dart';
import './set/settings.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
color: Colors.red,
theme: ThemeData(
backgroundColor: Colors.red,
),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _currentIndex = 0;
List <Widget> _pages;
PageController _pageController;
List tabbar = <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text("首页"),
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
title: Text("消息"),
icon: Icon(Icons.message),
),
BottomNavigationBarItem(
title: Text("我的"),
icon: Icon(Icons.people),
),
BottomNavigationBarItem(
title: Text("设置"),
icon: Icon(Icons.settings),
),
];
@override
void initState() {
super.initState();
_pageController = PageController(initialPage: _currentIndex);
_pages = [
HomeViewController(),
MessageViewController(),
MeViewController(),
SettingsViewController(),
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView.builder(
itemBuilder: (BuildContext context, int index) {
return _pages[index];
},
itemCount: _pages.length,
controller: _pageController,
onPageChanged: (int index) {
setState(() {
_currentIndex = index;
});
},
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: this._currentIndex,
selectedFontSize: 12.0,
type: BottomNavigationBarType.fixed,
items: tabbar,
onTap: (int index) {
debugPrint("index = $index");
setState(() {
this._currentIndex = index;
this._pageController.animateToPage(index,
duration: Duration(microseconds: 200), curve: Curves.easeInOut);
});
},
),
);
}
}
home_view.dart
import 'package:flutter/material.dart';
class HomeViewController extends StatefulWidget {
HomeViewController({Key key}) : super(key: key);
_HomeViewControllerState createState() => _HomeViewControllerState();
}
class _HomeViewControllerState extends State<HomeViewController> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("首页"),
),
body: Center(
child: Container(
height: 400.0,
width: 300.0,
color: Colors.red,
child: MyStack(),
),
),
);
}
}
class MyStack extends StatelessWidget {
const MyStack({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.center,
children: <Widget>[
Positioned(
top: 20.0,
left: 20.0,
right: 20.0,
child: Container(
color: Colors.green[400],
// alignment: Alignment(-0.7,-.7),
alignment: Alignment.center,
height: 240,
child: Text(
"朋友",
style: TextStyle(fontSize: 40,color: Colors.yellow),
),
),
),
Align(
alignment: Alignment(-0.8, 0.8),
child: Icon(
Icons.skip_previous,
color: Colors.white,
size: 35.0,
),
),
Align(
alignment: Alignment(0, 0.8),
child: Icon(
Icons.play_arrow,
color: Colors.white,
size: 35.0,
),
),
Align(
alignment: Alignment(0.8, 0.8),
child: Icon(
Icons.skip_next,
color: Colors.white,
size: 35.0,
),
),
],
);
}
}
settings.dart
import 'package:flutter/material.dart';
class SettingsViewController extends StatefulWidget {
SettingsViewController({Key key}) : super(key: key);
_SettingsViewControllerState createState() => _SettingsViewControllerState();
}
class _SettingsViewControllerState extends State<SettingsViewController> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("设置"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.settings_overscan),
tooltip: 'Navigration',
onPressed: () {
// debugPrint('search button is pressed.');
showAlertDialog(context);
},
),
],
),
body: Container(
alignment: Alignment.center,
margin: EdgeInsets.all(10),
color: Colors.blue,
child: Column(
children: <Widget>[
Icon(Icons.add),
Text("添加"),
],
),
),
);
}
}
void showAlertDialog(BuildContext context) {
showDialog(
context: context,
builder: (_) => new AlertDialog(
title: new Text("Dialog Title"),
content: new Text("This is my content"),
actions: <Widget>[
new FlatButton(
child: new Text("CANCEL"),
onPressed: () {
Navigator.of(context).pop();
},
),
new FlatButton(
child: new Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
)
]));
}