Last active
May 29, 2022 04:12
-
-
Save FrizzleFur/dc9cfe384f25c4c427232f52827adc7f to your computer and use it in GitHub Desktop.
A example for Flutter Key illustration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'dart:math'; | |
| void main() { | |
| runApp(MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| // This widget is the root of your application. | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| // is not restarted. | |
| primarySwatch: Colors.blue, | |
| visualDensity: VisualDensity.adaptivePlatformDensity, | |
| ), | |
| home: MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| MyHomePage({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Screen(), | |
| ); | |
| } | |
| } | |
| class Screen extends StatefulWidget { | |
| @override | |
| _ScreenState createState() => _ScreenState(); | |
| } | |
| class _ScreenState extends State<Screen> { | |
| List<Widget> noStateWidgets = [ | |
| StatelessContainer(), | |
| StatelessContainer(), | |
| ]; | |
| List<Widget> stateWidgets = [ | |
| StatefulContainer(), | |
| StatefulContainer(), | |
| ]; | |
| List<Widget> stateKeyWidgets = [ | |
| StatefulContainer(key: UniqueKey(),), | |
| StatefulContainer(key: UniqueKey(),), | |
| ]; | |
| List<Widget> paddingStateWidgets1 = [ | |
| Padding( | |
| padding: const EdgeInsets.all(8.0), | |
| child: StatefulContainer(key: UniqueKey(),), | |
| ), | |
| Padding( | |
| padding: const EdgeInsets.all(8.0), | |
| child: StatefulContainer(key: UniqueKey(),), | |
| ), | |
| ]; | |
| List<Widget> paddingStateWidgets2 = [ | |
| Padding( | |
| key: UniqueKey(), | |
| padding: const EdgeInsets.all(8.0), | |
| child: StatefulContainer(), | |
| ), | |
| Padding( | |
| key: UniqueKey(), | |
| padding: const EdgeInsets.all(8.0), | |
| child: StatefulContainer(), | |
| ), | |
| ]; | |
| List<Widget> widgets; | |
| // bool isContainerWithState = false; | |
| bool isContainerWithState = true; | |
| @override | |
| Widget build(BuildContext context) { | |
| widgets = isContainerWithState ? stateWidgets : noStateWidgets; | |
| // widgets = stateKeyWidgets; | |
| widgets = paddingStateWidgets1; | |
| // widgets = paddingStateWidgets2; | |
| return Scaffold( | |
| body: Center( | |
| child: Row( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: widgets, | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: switchWidget, | |
| child: Icon(Icons.undo), | |
| ), | |
| ); | |
| } | |
| switchWidget(){ | |
| widgets.insert(0, widgets.removeAt(1)); | |
| setState(() {}); | |
| } | |
| } | |
| // 无状态Container | |
| class StatelessContainer extends StatelessWidget { | |
| final Color color = [Colors.red, Colors.blue][Random().nextInt(2)]; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Container( | |
| width: 100, | |
| height: 100, | |
| color: color, | |
| ); | |
| } | |
| } | |
| // 有状态Container | |
| // 在 StatefulContainer 中,我们将定义 Color 和 build 方法都放进了 State 中 | |
| class StatefulContainer extends StatefulWidget { | |
| StatefulContainer({Key key}) : super(key: key); | |
| @override | |
| _StatefulContainerState createState() => _StatefulContainerState(); | |
| } | |
| class _StatefulContainerState extends State<StatefulContainer> { | |
| final Color color = [Colors.red, Colors.blue][Random().nextInt(2)]; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Container( | |
| width: 100, | |
| height: 100, | |
| color: color, | |
| ); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment