博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2_flutter_TextField(文本框),TabBar(选项卡),bottomNavigationBar(底部导航栏)
阅读量:5984 次
发布时间:2019-06-20

本文共 10193 字,大约阅读时间需要 33 分钟。

1_TextField(文本框)


import 'package:flutter/material.dart';void main() {  runApp(MaterialApp(    home: MyEditText(),  ));}class MyEditText extends StatefulWidget {  @override  MyEditTextState createState() => MyEditTextState();}class MyEditTextState extends State
{ String results = ""; final TextEditingController controller = TextEditingController(); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Using EditText"), backgroundColor: Colors.red, ), body: Container( child: Center( child: Column( children:
[ TextField( decoration: InputDecoration(hintText: "Enter text here..."), onSubmitted: (String str) { setState(() { results = results + "\n" + str; }); }, ), Text(results) ], ), ), ), ); }}复制代码

1.1文本框获取值

import 'package:flutter/material.dart';void main() => runApp(MyApp());class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      title: 'Retrieve Text Input',      home: MyForm(),    );  }}// Define a Custom Form Widgetclass MyForm extends StatefulWidget {  @override  _MyFormState createState() => _MyFormState();}// Define a corresponding State class. This class will hold the data related to// our Form.class _MyFormState extends State
{ // Create a text controller. We will use it to retrieve the current value // of the TextField! final myController = TextEditingController(); @override void dispose() { // Clean up the controller when the Widget is disposed myController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Retrieve Text Input'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: TextField( controller: myController, ), ), floatingActionButton: FloatingActionButton( // When the user presses the button, show an alert dialog with the // text the user has typed into our text field. onPressed: () { return showDialog( context: context, builder: (context) { return AlertDialog( // Retrieve the text the user has typed in using our // TextEditingController content: Text(myController.text), ); }, ); }, tooltip: 'Show me the value!', child: Icon(Icons.text_fields), ), ); }}复制代码

2_TabBar(选项卡)


import 'package:flutter/material.dart';void main() {  runApp(TabBarDemo());}class TabBarDemo extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      home: DefaultTabController(        length: 3,        child: Scaffold(          appBar: AppBar(            bottom: TabBar(              tabs: [                Tab(icon: Icon(Icons.directions_car)),                Tab(icon: Icon(Icons.directions_transit)),                Tab(icon: Icon(Icons.directions_bike)),              ],            ),            title: Text('Tabs Demo'),          ),          body: TabBarView(            children: [              Icon(Icons.directions_car),              Icon(Icons.directions_transit),              Icon(Icons.directions_bike),            ],          ),        ),      ),    );  }}复制代码

2.1_顶部选项卡

import 'package:flutter/material.dart';void main() {  runApp(TabBarDemo());}class TabBarDemo extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      home: DefaultTabController(        length: 3,        child: Scaffold(          appBar: AppBar(            title: Material(              color: Colors.blue,        child: TabBar(          tabs: [            Tab(icon: Icon(Icons.directions_car)),            Tab(icon: Icon(Icons.directions_transit)),            Tab(icon: Icon(Icons.directions_bike)),          ],          ),            ),          ),                    body: TabBarView(            children: [              Icon(Icons.directions_car),              Icon(Icons.directions_transit),              Icon(Icons.directions_bike),            ],          ),        ),      ),    );  }}复制代码

2.2_选项卡页面

import 'package:flutter/material.dart';void main() {  runApp(TabBarDemo());}class TabBarDemo extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      home: DefaultTabController(        length: 3,        child: Scaffold(          appBar: AppBar(            title: Material(              color: Colors.blue,        child: TabBar(          tabs: [            Tab(icon: Icon(Icons.directions_car)),            Tab(icon: Icon(Icons.directions_transit)),            Tab(icon: Icon(Icons.directions_bike)),          ],          ),            ),          ),                    body: TabBarView(            children: [             Home1(),             Home2(),             Home3(),            ],          ),        ),      ),    );  }}class Home1 extends StatelessWidget{  @override  Widget build(BuildContext context) {    return Scaffold(      body: Center(child:Text('HOME1') ,),    );  }}class Home2 extends StatelessWidget{  @override  Widget build(BuildContext context) {    return Scaffold(      body: Center(child:Text('HOME2') ,),    );  }}class Home3 extends StatelessWidget{  @override  Widget build(BuildContext context) {    return Scaffold(      body: Center(child:Text('HOME3') ,),    );  }}复制代码

3_bottomNavigationBar(底部导航栏)


import 'package:flutter/material.dart';void main() {  runApp(MyApp());}class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      title: 'Generated App',      theme: ThemeData(        primarySwatch: Colors.blue,        primaryColor: const Color(0xFF2196f3),        accentColor: const Color(0xFF2196f3),        canvasColor: const Color(0xFFfafafa),      ),      home: MyHomePage(),    );  }}class MyHomePage extends StatefulWidget {  MyHomePage({Key key}) : super(key: key);  @override  _MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State
{ int index=0; List
pages=[ Container(color: Colors.deepOrange), Container(color: Colors.amber), Container(color: Colors.blue), Container(color: Colors.green), ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('App Name'), ), bottomNavigationBar: BottomNavigationBar( type: BottomNavigationBarType.fixed, onTap: (int idx){ setState(() { index=idx; }); }, items: [ BottomNavigationBarItem( icon: const Icon(Icons.access_alarm,color: Colors.black), title: Text('Title',style: TextStyle(color: Colors.black)), ), BottomNavigationBarItem( icon: const Icon(Icons.star,color: Colors.black), title: Text('Title',style: TextStyle(color: Colors.black)), ), BottomNavigationBarItem( icon: const Icon(Icons.pages,color: Colors.black), title: Text('Title',style: TextStyle(color: Colors.black)), ), BottomNavigationBarItem( icon: const Icon(Icons.adjust,color: Colors.black), title: Text('Title',style: TextStyle(color: Colors.black)), ), ] ), body:pages[index] , ); }}复制代码

3.1_底部导航页面

import 'package:flutter/material.dart';void main() {  runApp(MyApp());}class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      theme: ThemeData(        primarySwatch: Colors.blue,        primaryColor: const Color(0xFF2196f3),        accentColor: const Color(0xFF2196f3),        canvasColor: const Color(0xFFfafafa),      ),      home: MyHomePage(),    );  }}class MyHomePage extends StatefulWidget {  MyHomePage({Key key}) : super(key: key);  @override  _MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State
{ int index=0; List
pages=[ Home1(), Home2(), Home3(), Home4() ]; @override Widget build(BuildContext context) { return Scaffold( bottomNavigationBar: BottomNavigationBar( type: BottomNavigationBarType.fixed, currentIndex: index, fixedColor: Colors.blue, onTap: (int idx){ setState(() { index=idx; }); }, items: [ BottomNavigationBarItem( icon: const Icon(Icons.access_alarm), title: Text('Title'), ), BottomNavigationBarItem( icon: const Icon(Icons.star), title: Text('Title'), ), BottomNavigationBarItem( icon: const Icon(Icons.pages), title: Text('Title'), ), BottomNavigationBarItem( icon: const Icon(Icons.adjust), title: Text('Title'), ), ] ), body:pages[index] , ); }}class Home1 extends StatelessWidget{ @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Home1'),), body: Center(child:Text('HOME1') ,), ); }}class Home2 extends StatelessWidget{ @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Home2'),), body: Center(child:Text('HOME2') ,), ); }}class Home3 extends StatelessWidget{ @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Home3'),), body: Center(child:Text('HOME3') ,), ); }}class Home4 extends StatelessWidget{ @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Home4'),), body: Center(child:Text('HOME4') ,), ); }}复制代码

转载于:https://juejin.im/post/5c6ce72af265da2dcc7fea09

你可能感兴趣的文章
Web service (一) 原理和项目开发实战
查看>>
跑带宽度多少合适_跑步机选购跑带要多宽,你的身体早就告诉你了
查看>>
Javascript异步数据的同步处理方法
查看>>
iis6 zencart1.39 伪静态规则
查看>>
SQL Server代理(3/12):代理警报和操作员
查看>>
Linux备份ifcfg-eth0文件导致的网络故障问题
查看>>
2018年尾总结——稳中成长
查看>>
通过jsp请求Servlet来操作HBASE
查看>>
Shell编程基础
查看>>
Shell之Sed常用用法
查看>>
Centos下基于Hadoop安装Spark(分布式)
查看>>
mysql开启binlog
查看>>
设置Eclipse编码方式
查看>>
分布式系统唯一ID生成方案汇总【转】
查看>>
并查集hdu1232
查看>>
Mysql 监视工具
查看>>
博客搬家了
查看>>
Python中使用ElementTree解析xml
查看>>
linux的日志服务器关于屏蔽一些关键字的方法
查看>>
mysql多实例实例化数据库
查看>>