• 动态逐行添加list,按一下按钮添加一行 ```dart class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); }

    class _HomePageState extends State { int countNum=0; List list=[]; @override Widget build(BuildContext context) { return ListView( children: [ Column( children: this.list.map((e){ return ListTile( title: Text(e), ); }).toList(), ), ElevatedButton( child: Text(“你好吗”), onPressed: (){ setState(() { this.countNum++; this.list.add(“${this.countNum}”); }); }, ) ], ); } }

    1. - 底部导航栏
    2. ```dart
    3. class MyApp extends StatelessWidget {
    4. @override
    5. Widget build(BuildContext context) {
    6. // TODO: implement build
    7. return MaterialApp(
    8. home: Tabs(),
    9. theme: ThemeData(primarySwatch: Colors.yellow),
    10. );
    11. }
    12. }
    13. class Tabs extends StatefulWidget {
    14. const Tabs({Key? key}) : super(key: key);
    15. @override
    16. _TabsState createState() => _TabsState();
    17. }
    18. class _TabsState extends State<Tabs> {
    19. int _currentIndex=0;
    20. var pages=HomePage();
    21. List _page=[HomePage(),Text('1'),Text('2'),Text('3'),];
    22. @override
    23. Widget build(BuildContext context) {
    24. return Scaffold(
    25. appBar: AppBar(
    26. title: Text('测试!'),
    27. ),
    28. body: this._page[_currentIndex],//切换页面
    29. bottomNavigationBar: BottomNavigationBar(
    30. currentIndex: this._currentIndex,
    31. onTap: (int index){
    32. setState(() {
    33. this._currentIndex=index;
    34. });
    35. },
    36. type: BottomNavigationBarType.fixed, //超过3个必须要设置
    37. items: [
    38. BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
    39. BottomNavigationBarItem(icon: Icon(Icons.sanitizer), label: '1'),
    40. BottomNavigationBarItem(icon: Icon(Icons.radio), label: '2'),
    41. BottomNavigationBarItem(icon: Icon(Icons.queue), label: '3'),
    42. ],
    43. )
    44. );
    45. }
    46. }
    • 顶部导航栏1,不推荐

      1. class TopTab extends StatelessWidget {
      2. const TopTab({Key? key}) : super(key: key);
      3. @override
      4. Widget build(BuildContext context) {
      5. return DefaultTabController(
      6. length: 2,
      7. child: Scaffold(
      8. appBar: AppBar(
      9. title: Text('111!'),
      10. bottom: TabBar(
      11. isScrollable:true,//如果多个可滑动
      12. tabs: [
      13. Tab(text: '1',),
      14. Tab(text: '2',),
      15. ],
      16. ),
      17. ),
      18. body: TabBarView(
      19. children: [
      20. ListView(
      21. children: [
      22. ListTile(title: Text('1'),),
      23. ListTile(title: Text('11'),),
      24. ],
      25. ),ListView(
      26. children: [
      27. ListTile(title: Text('2'),),
      28. ListTile(title: Text('22'),),
      29. ],
      30. )
      31. ],
      32. ),
      33. ));
      34. }
      35. }
    • 顶部导航栏2,推荐,可监听 ```dart class TabBarControllerPage extends StatefulWidget { const TabBarControllerPage({Key? key}) : super(key: key);

      @override _TabBarControllerPageState createState() => _TabBarControllerPageState(); }

    class _TabBarControllerPageState extends State with SingleTickerProviderStateMixin { late TabController _tabController; @override void initState() { // TODO: implement initState super.initState(); _tabController = new TabController(length: 2, vsync: this); _tabController.addListener(() {print(_tabController.index);}); }

    @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(‘TabBarControllerPage’), bottom: TabBar( controller: this._tabController, tabs: [ Tab(text: ‘111’,), Tab(text: ‘222’,), ], ), ), body: TabBarView( controller: this._tabController, children: [ Center(child: Text(‘11’),), Center(child: Text(‘22’),) ]), ); } }

    1. - 侧边栏,头部自定义
    2. ```dart
    3. //放AppBar里面
    4. drawer: Drawer(
    5. child: Column(
    6. children: [
    7. Row(
    8. children: [
    9. Expanded(child: DrawerHeader(
    10. child: Text("头"),
    11. decoration: BoxDecoration(
    12. color: Colors.red,
    13. image: DecorationImage(
    14. image: NetworkImage("https://z3.ax1x.com/2021/09/19/43hiWt.jpg"),
    15. fit: BoxFit.cover,
    16. ),
    17. ),
    18. ))
    19. ],
    20. ),
    21. ListTile(
    22. title: Text('1'),
    23. onTap: (){
    24. Navigator.of(context).pop();//隐藏侧边栏
    25. Navigator.pushNamed(context, '/seach');//跳转
    26. },
    27. ),
    28. ListTile(title: Text('2'),),
    29. ListTile(title: Text('3'),),
    30. ListTile(title: Text('4'),),
    31. ListTile(title: Text('5'),),
    32. ],
    33. ),
    34. ),
    • 侧边栏,头部自带模板

      1. //放AppBar里面
      2. drawer: Drawer(
      3. child: Column(
      4. children: [
      5. UserAccountsDrawerHeader(
      6. accountName: Text("chin"),
      7. accountEmail: Text("chin.cc@outlook.in"),
      8. currentAccountPicture: CircleAvatar(
      9. backgroundImage: NetworkImage("https://z3.ax1x.com/2021/09/19/43hiWt.jpg"),
      10. ),
      11. decoration: BoxDecoration(
      12. color: Colors.red,
      13. image: DecorationImage(
      14. image: NetworkImage("https://z3.ax1x.com/2021/09/19/43hiWt.jpg"),
      15. fit: BoxFit.cover,
      16. ),
      17. ),
      18. ),
      19. ListTile(
      20. title: Text('1'),
      21. onTap: (){
      22. Navigator.of(context).pop();//隐藏侧边栏
      23. Navigator.pushNamed(context, '/seach');//跳转
      24. },
      25. ),
      26. ListTile(title: Text('2'),),
      27. ListTile(title: Text('3'),),
      28. ListTile(title: Text('4'),),
      29. ListTile(title: Text('5'),),
      30. ],
      31. ),
      32. ),
    • 日期选择框-系统

      1. //20行格式化日期需安装date_format
      2. DateTime _date=DateTime.now();
      3. _showDatePicker() async{
      4. var result=await showDatePicker(
      5. context: context,
      6. initialDate: _date, //初始化日期
      7. firstDate: DateTime(1980),//可选择的最早日期
      8. lastDate: DateTime(2050),//可选择的最迟日期
      9. );
      10. print(result);
      11. setState(() {
      12. this._date=result!;
      13. });
      14. }
      15. ...
      16. InkWell(
      17. child: Row(
      18. mainAxisAlignment: MainAxisAlignment.center,
      19. children: [
      20. Text("${formatDate(_date, [yyyy,'年',mm,'月',dd,'日'])}"),
      21. Icon(Icons.arrow_drop_down),
      22. ],
      23. ),
      24. onTap: _showDatePicker,
      25. ),

      时间选择框-系统

      1. TimeOfDay _time=TimeOfDay(hour: 0, minute: 0);
      2. _showTimePicker() async{
      3. var result=await showTimePicker(
      4. context: context,
      5. initialTime: _time, //初始化日期
      6. );
      7. print(result);
      8. setState(() {
      9. this._time=result!;
      10. });
      11. }
      12. ...
      13. InkWell(
      14. child: Row(
      15. mainAxisAlignment: MainAxisAlignment.center,
      16. children: [
      17. Text("${_time.format(context)}"),
      18. Icon(Icons.arrow_drop_down),
      19. ],
      20. ),
      21. onTap: _showTimePicker,
      22. ),

      日期选择框-第三方

      1. //28行格式化日期需安装date_format
      2. DateTime _date=DateTime.now();
      3. _showDatePicker2(){
      4. DatePicker.showDatePicker(context,
      5. showTitleActions: true,
      6. minTime: DateTime(1999,1,1),
      7. maxTime:DateTime(2050,12,31),
      8. onChanged: (result){//更改时触发
      9. setState(() {
      10. this._date=result;
      11. print('onChanged');
      12. });
      13. },onConfirm: (result){//确认后触发
      14. setState(() {
      15. this._date=result;
      16. print('onConfirm');
      17. });
      18. },
      19. currentTime: _date,//默认时间
      20. locale: LocaleType.zh,
      21. );
      22. }
      23. ...
      24. InkWell(
      25. child: Row(
      26. mainAxisAlignment: MainAxisAlignment.center,
      27. children: [
      28. Text("${formatDate(_date, [yyyy,'年',mm,'月',dd,'日'])}"),
      29. Icon(Icons.arrow_drop_down),
      30. ],
      31. ),
      32. onTap: _showDatePicker2,
      33. ),

      时间选择框-第三方

      1. //24行格式化日期需安装date_format
      2. DateTime _date=DateTime.now();
      3. _showTimePicker2(){
      4. DatePicker.showTimePicker(context,
      5. showTitleActions: true,
      6. onChanged: (result){//更改时触发
      7. setState(() {
      8. this._date=result;
      9. });
      10. },onConfirm: (result){//确认后触发
      11. setState(() {
      12. this._date=result;
      13. });
      14. },
      15. currentTime: _date,//默认时间
      16. locale: LocaleType.zh,
      17. );
      18. }
      19. ...
      20. InkWell(
      21. child: Row(
      22. mainAxisAlignment: MainAxisAlignment.center,
      23. children: [
      24. Text("${formatDate(_date, [HH, ':', nn])}"),
      25. Icon(Icons.arrow_drop_down),
      26. ],
      27. ),
      28. onTap: _showTimePicker2,
      29. ),

      第三方时间控件datetime_picker的坑

      1. //color: theme.backgroundColor ?? Colors.white,
      2. flutter_datetime_picker.dart 文件中的改为
      3. color: theme.headerColor ?? theme.backgroundColor,

      Dialog对话框 ```dart _modelBottomSheet() async{ var result=await showModalBottomSheet(

      1. context: context,
      2. builder: (context){
      3. return Container(
      4. height: 200,
      5. child: Column(
      6. children: [
      7. ListTile(
      8. title: Text("A"),
      9. onTap: ()=>Navigator.pop(context,'选项A'),
      10. ),
      11. ListTile(
      12. title: Text("B"),
      13. onTap: ()=>Navigator.pop(context,'选项B'),
      14. ),
      15. ],
      16. ),
      17. );
      18. }

      ); print(result); }

    _simpleDialog() async{ var result=await showDialog( context: context, builder: (context){ return SimpleDialog( title: Text(‘提示标题’), children: [ SimpleDialogOption( child: Text(‘选项1’), onPressed: ()=>Navigator.pop(context,’选项1’), ), SimpleDialogOption( child: Text(‘选项2’), onPressed: ()=>Navigator.pop(context,’选项2’), ), ], ); } ); print(result); }

    _alertDialog() async{ var result=await showDialog( context: context, builder: (context){ return AlertDialog( title: Text(‘提示标题’), content: Text(‘提示内容’), actions: [ TextButton( onPressed:()=>Navigator.pop(context,’no’), child: Text(‘取消’) ), TextButton( onPressed:()=>Navigator.pop(context,’yes’), child: Text(‘确定’) ), ], ); }); print(result); } … ElevatedButton(onPressed: _alertDialog, child: Text(‘alertDialog’)),//是否对话框 ElevatedButton(onPressed: _simpleDialog, child: Text(‘simpleDialog’)),//选项对话框 ElevatedButton(onPressed: _modelBottomSheet, child: Text(‘modelBottomSheet’)),//底部弹出选项对话框

    1. 自定义弹框Dialo
    2. ```dart
    3. class DiyDialog extends Dialog{
    4. @override
    5. Widget build(BuildContext context) {
    6. return Material(
    7. type: MaterialType.transparency,
    8. child: Center(
    9. child: Container(
    10. height: 120,
    11. width: 250,
    12. color: Colors.white,
    13. ),
    14. ),
    15. );
    16. }
    17. }
    18. ...
    19. _diyDialog(BuildContext context) async{
    20. var result=await showDialog(
    21. context: context,
    22. builder: (context){
    23. return DiyDialog();
    24. });
    25. print(result);
    26. }
    27. ...
    28. ElevatedButton(onPressed: _diyDialog, child: Text('diyDialog')),