Text

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'Text widget',
  8. home: Scaffold(
  9. appBar: AppBar(
  10. title: Text('哈利波特')
  11. ),
  12. body: Center(
  13. child: Text(
  14. '哈利波特一共有八部,而我在2月份看了八部,里面哈利波特的勇敢,赫敏的睿智都给我留下了很深的印象。',
  15. textAlign: TextAlign.center,
  16. maxLines: 1,
  17. overflow: TextOverflow.ellipsis,
  18. style: TextStyle(
  19. fontSize: 24.0,
  20. color: Color.fromARGB(255, 255, 100, 100),
  21. decoration: TextDecoration.underline,
  22. decorationStyle: TextDecorationStyle.dashed,
  23. ),
  24. ),
  25. ),
  26. ),
  27. );
  28. }
  29. }

Container

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'Text Widget',
  8. home: Scaffold(
  9. appBar: AppBar(
  10. title: Text('暮光之城'),
  11. ),
  12. body: Center(
  13. child: Container(
  14. child: Text('暮光破晓', style: TextStyle(fontSize: 26.0, color: Color.fromARGB(255, 0, 0, 50))),
  15. alignment: Alignment.topLeft,
  16. width: 500.0,
  17. height: 400.0,
  18. // color: Colors.lightBlue,
  19. // padding: const EdgeInsets.all(10.0),
  20. padding: const EdgeInsets.fromLTRB(10.0, 100.0, 0.0, 0.0),
  21. margin: const EdgeInsets.all(10.0),
  22. decoration: new BoxDecoration(
  23. gradient: const LinearGradient(
  24. colors: [Colors.lightBlue, Colors.greenAccent, Colors.purple]
  25. ),
  26. border: Border.all(width: 5.0, color: Colors.red)
  27. ),
  28. ),
  29. ),
  30. ),
  31. );
  32. }
  33. }
  34. decoration下加渐变色、边框等。

屏幕快照 2020-03-21 下午9.08.20.png

Image

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: '应用描述',
  8. home: Scaffold(
  9. appBar: AppBar(
  10. title: Text('光明与黑暗')
  11. ),
  12. body: Center(
  13. child: Container(
  14. child: new Image.netWork(
  15. '图片链接',
  16. fit: BoxFit.cover,
  17. color: Colors.green,
  18. colorBlendMode: BlendMode.darken,
  19. repeat: ImageRepeat.noRepeat,
  20. ),
  21. width: 300.0,
  22. height: 500.0,
  23. color: Colors.green,
  24. ),
  25. ),
  26. ),
  27. );
  28. }
  29. }

屏幕快照 2020-03-21 下午10.05.52.png

ListView

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp(
  3. items: List<String>.generate(1000, (i) => "I love you $i")
  4. ));
  5. class MyApp extends StatelessWidget {
  6. final List<String> items;
  7. MyApp({Key key, @required this.items}):super(key: key);
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: '爱丽丝',
  12. home: Scaffold(
  13. appBar: AppBar(
  14. title: Text('爱丽丝'),
  15. ),
  16. body: ListView.builder(
  17. itemCount: items.length,
  18. itemBuilder: (context, index) {
  19. return ListTile(
  20. title: Text('${items[index]}')
  21. );
  22. }
  23. ),
  24. )
  25. );
  26. }
  27. }

基础的路由跳转

  1. import "package:flutter/material.dart";
  2. void main() => runApp(MaterialApp(
  3. title: '基础导航练习',
  4. home: FirstScreen()
  5. ));
  6. class FirstScreen extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar: AppBar(title: Text('商品页')),
  11. body: Center(
  12. child: RaisedButton(
  13. child: Text('查看商品详情页'),
  14. onPressed: () {
  15. Navigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen()));
  16. },
  17. ),
  18. ),
  19. );
  20. }
  21. }
  22. class SecondScreen extends StatelessWidget {
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: AppBar(title: Text('商品详情页')),
  27. body: Center(
  28. child: RaisedButton(
  29. child: Text('返回到商品页'),
  30. onPressed: () {
  31. Navigator.pop(context);
  32. },
  33. )
  34. ),
  35. );
  36. }
  37. }

通过route name跳转

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MaterialApp(
  3. initialRoute: '/',
  4. routes: {
  5. '/': (context) => FirstScreen(),
  6. '/second': (context) => SecondScreen(),
  7. },
  8. ));
  9. class FirstScreen extends StatelessWidget {
  10. @override
  11. Widget build(BuildContext context) {
  12. return Scaffold(
  13. appBar: AppBar(title: Text('/匹配的是第一页')),
  14. body: Center(
  15. child: RaisedButton(
  16. child: Text('/'),
  17. onPressed: () {
  18. Navigator.pushNamed(context, '/second');
  19. },
  20. )
  21. )
  22. );
  23. }
  24. }
  25. class SecondScreen extends StatelessWidget {
  26. @override
  27. Widget build(BuildContext context) {
  28. return Scaffold(
  29. appBar: AppBar(title: Text('/second匹配的是第二页')),
  30. body: Center(
  31. child: RaisedButton(
  32. child: Text('go back'),
  33. onPressed: () {
  34. Navigator.pop(context);
  35. },
  36. )
  37. ),
  38. );
  39. }
  40. }
  41. 比如在app中许多地方都要导航到同一个页面,基础路由跳转会导致代码重复。解决方法就是 named routes

页面跳转且传递数据

  1. import 'package:flutter/material.dart';
  2. class Todo {
  3. final String title;
  4. final String description;
  5. Todo(this.title, this.description);
  6. }
  7. void main() => runApp(MaterialApp(
  8. title: '传数据',
  9. // todos 是传递给TodoScreen 的参数
  10. home: TodoScreen(
  11. todos: List.generate(20, (i) => Todo('Todo $i', 'A description of what needs to be done for Todo $i',))
  12. ),
  13. ));
  14. class TodoScreen extends StatelessWidget {
  15. final List<Todo> todos; // 接收参数 todos
  16. TodoScreen({Key key, @required this.todos}) : super(key: key);
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. appBar: AppBar(title: Text('Todos')),
  21. body: ListView.builder(
  22. itemCount: todos.length,
  23. itemBuilder: (context, index) {
  24. return ListTile(
  25. title: Text(todos[index].title),
  26. onTap: () {
  27. Navigator.push(context, MaterialPageRoute(
  28. builder: (context) => DetailScreen(todo: todos[index]) // 给 详情页传递数据 todo
  29. ));
  30. },
  31. );
  32. }
  33. ),
  34. );
  35. }
  36. }
  37. class DetailScreen extends StatelessWidget {
  38. final Todo todo; // 接收参数 todo
  39. DetailScreen({Key key, @required this.todo}) : super(key: key);
  40. @override
  41. Widget build(BuildContext context) {
  42. return Scaffold(
  43. appBar: AppBar(title: Text(todo.title)),
  44. body: Padding(
  45. padding: EdgeInsets.all(16.0),
  46. child: Text(todo.description),
  47. ),
  48. );
  49. }
  50. }

页面跳转并返回数据

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MaterialApp(
  4. title: 'Returning Data',
  5. home: HomeScreen(),
  6. ));
  7. }
  8. class HomeScreen extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. appBar: AppBar(
  13. title: Text('Returning Data Demo'),
  14. ),
  15. body: Center(child: SelectionButton()),
  16. );
  17. }
  18. }
  19. class SelectionButton extends StatelessWidget {
  20. @override
  21. Widget build(BuildContext context) {
  22. return RaisedButton(
  23. child: Text('Pick an option, any option!'),
  24. onPressed: () {
  25. _navigateAndDisplaySelection(context);
  26. },
  27. );
  28. }
  29. // A method that launches the SelectionScreen and awaits the result from
  30. // Navigator.pop.
  31. _navigateAndDisplaySelection(BuildContext context) async {
  32. // Navigator.push returns a Future that completes after calling
  33. // Navigator.pop on the Selection Screen.
  34. final result = await Navigator.push(
  35. context,
  36. MaterialPageRoute(builder: (context) => SelectionScreen()),
  37. );
  38. // After the Selection Screen returns a result, hide any previous snackbars
  39. // and show the new result.
  40. Scaffold.of(context)
  41. ..removeCurrentSnackBar()
  42. ..showSnackBar(SnackBar(content: Text("$result")));
  43. }
  44. }
  45. class SelectionScreen extends StatelessWidget {
  46. @override
  47. Widget build(BuildContext context) {
  48. return Scaffold(
  49. appBar: AppBar(
  50. title: Text('Pick an option'),
  51. ),
  52. body: Center(
  53. child: Column(
  54. mainAxisAlignment: MainAxisAlignment.center,
  55. children: <Widget>[
  56. Padding(
  57. padding: const EdgeInsets.all(8.0),
  58. child: RaisedButton(
  59. onPressed: () {
  60. // Close the screen and return "Yep!" as the result.
  61. Navigator.pop(context, 'Yep!');
  62. },
  63. child: Text('Yep!'),
  64. ),
  65. ),
  66. Padding(
  67. padding: const EdgeInsets.all(8.0),
  68. child: RaisedButton(
  69. onPressed: () {
  70. // Close the screen and return "Nope!" as the result.
  71. Navigator.pop(context, 'Nope.');
  72. },
  73. child: Text('Nope.'),
  74. ),
  75. )
  76. ],
  77. ),
  78. ),
  79. );
  80. }
  81. }