Commit db9ee1b0 authored by jinzhu's avatar jinzhu

基本搭建好 tabBarcontroller

parent a26cda11
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
void main() => runApp(MyApp());
void main() => runApp(App());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
debugShowCheckedModeBanner: false,
initialRoute: '/',
routes: {
'/': (context) => Home(),
},
theme: ThemeData(
primarySwatch: Colors.yellow,
highlightColor: Color.fromRGBO(255, 255, 255, 0.5),
splashColor: Colors.white70,
accentColor: Color.fromRGBO(3, 54, 255, 1.0),
));
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() => new HomeState();
}
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
class HomeState extends State<Home> {
final appBarTitles = ['首页', '订单', '消息', '我'];
final tabTextStyleSelected = new TextStyle(color: const Color(0xff333333));
final tabTextStyleNormal = new TextStyle(color: const Color(0xff666666));
int _tabIndex = 0;
var tabImages;
var _body;
var pages;
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
TextStyle getTabTextStyle(int curIndex) {
if (curIndex == _tabIndex) {
return tabTextStyleSelected;
}
return tabTextStyleNormal;
}
final String title;
Image getTabIcon(int curIndex) {
if (curIndex == _tabIndex) {
return tabImages[curIndex][1];
}
return tabImages[curIndex][0];
}
@override
_MyHomePageState createState() => _MyHomePageState();
}
Image getTabImage(path) {
return new Image.asset(path, width: 40.0, height: 40.0);
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override
void initState() {
super.initState();
// DataUtils.getColorThemeIndex().then((index) {
// print('color theme index = $index');
// if (index != null) {
// ThemeUtils.currentColorTheme = ThemeUtils.supportColors[index];
// Constants.eventBus.fire(new ChangeThemeEvent(ThemeUtils.supportColors[index]));
// }
// });
// Constants.eventBus.on<ChangeThemeEvent>().listen((event) {
// setState(() {
// themeColor = event.color;
// });
// });
pages = <Widget>[
// new NewsListPage(),
// new TweetsListPage(),
// new DiscoveryPage(),
// new MyInfoPage()
];
if (tabImages == null) {
tabImages = [
[
getTabImage('images/menu_home.png'),
getTabImage('images/menu_home_pr.png')
],
[
getTabImage('images/menu_order.png'),
getTabImage('images/menu_order_pr.png')
],
[
getTabImage('images/menu_message.png'),
getTabImage('images/menu_message_pr.png')
],
[
getTabImage('images/menu_me.png'),
getTabImage('images/menu_me_pr.png')
]
];
}
}
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
Text getTabTitle(int curIndex) {
return new Text(appBarTitles[curIndex], style: getTabTextStyle(curIndex));
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
_body = new IndexedStack(
children: pages,
index: _tabIndex,
);
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
title: Text('首页'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
tooltip: 'Search',
onPressed: () => debugPrint('Search button is pressed.'),
)
],
elevation: 0.0,
),
body: _body,
bottomNavigationBar: new CupertinoTabBar(
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
icon: getTabIcon(0),
title: getTabTitle(0)),
new BottomNavigationBarItem(
icon: getTabIcon(1),
title: getTabTitle(1)),
new BottomNavigationBarItem(
icon: getTabIcon(2),
title: getTabTitle(2)),
new BottomNavigationBarItem(
icon: getTabIcon(3),
title: getTabTitle(3)),
],
currentIndex: _tabIndex,
onTap: (index) {
setState((){
_tabIndex = index;
});
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
......@@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.8"
version: "2.1.0"
boolean_selector:
dependency: transitive
description:
......@@ -52,7 +52,7 @@ packages:
name: matcher
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.12.3+1"
version: "0.12.5"
meta:
dependency: transitive
description:
......@@ -80,7 +80,7 @@ packages:
name: quiver
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.1"
version: "2.0.2"
sky_engine:
dependency: transitive
description: flutter
......@@ -106,7 +106,7 @@ packages:
name: stream_channel
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.6.8"
version: "2.0.0"
string_scanner:
dependency: transitive
description:
......@@ -127,7 +127,7 @@ packages:
name: test_api
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.2.2"
version: "0.2.4"
typed_data:
dependency: transitive
description:
......@@ -143,4 +143,4 @@ packages:
source: hosted
version: "2.0.8"
sdks:
dart: ">=2.1.1-dev.0.0 <3.0.0"
dart: ">=2.2.0 <3.0.0"
......@@ -41,9 +41,17 @@ flutter:
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- images/menu_home.png
- images/menu_home_pr.png
- images/menu_order.png
- images/menu_order_pr.png
- images/menu_message.png
- images/menu_message_pr.png
- images/menu_me.png
- images/menu_me_pr.png
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.io/assets-and-images/#resolution-aware.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment