简体   繁体   English

Flutter 同一个容器不同内容 animation

[英]Flutter same container different content animation

In a container widget I am showing a text widget for displaying content.在一个容器小部件中,我展示了一个用于显示内容的文本小部件。 I am using same container but the text content differs based on user selection.我使用相同的容器,但文本内容因用户选择而异。 when user choose different option is it possible to fade the old content and show new content with animation.当用户选择不同的选项时,是否可以使用 animation 淡化旧内容并显示新内容。

    String content = "content";
    String subcontent = "subContent";

    switch (contentType) {
      case 1:
        content = "content1";
        subcontent = "subContent1";
        break;case 2:
     content = "content2";
        subcontent = "subContent2";
        break;case 3:
      content = "content3";
        subcontent = "subContent3";
        break;
      default:
        constantDataName = "content1";
        subcontentFieldName = "subContent1";
        break;
    }

return Padding(
      padding: const EdgeInsets.all(20.0),
      child: Container(
        margin: const EdgeInsets.only(left: 20, right: 20),
        padding: const EdgeInsets.all(20),
        decoration: const BoxDecoration(
          color: Color.fromRGBO(255, 255, 255, 1),
          borderRadius: BorderRadius.only(topLeft: Radius.circular(25), topRight: Radius.circular(25), bottomLeft: Radius.circular(25), bottomRight: Radius.circular(25)),
          boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.03999999910593033), offset: Offset(0, 1), spreadRadius: 5, blurRadius: 10)],
        ),
        child: Column(
          children: [
            Text(content1),const SizedBox(height: 10),Text(subcontent1),],
      ),
    );

Thanks,谢谢,

you could use AnimatedSwitcher , the Important think you should attend to is that use UniqueKey() , try this:你可以使用AnimatedSwitcher ,你应该注意的重要一点是使用UniqueKey() ,试试这个:

class ListWidget extends StatefulWidget {
  const ListWidget({Key? key}) : super(key: key);

  @override
  State<ListWidget> createState() => _ListWidgetState();
}

class _ListWidgetState extends State<ListWidget> {
  List _list = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
  ];
  String _maintText = 'select your number';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          SizedBox(
            height: 100,
          ),
          AnimatedSwitcher(
            child: Container(
              alignment: Alignment.center,
              key: UniqueKey(), // <---- very important part
              height: 100.0,
              width: 100.0,
              color: Colors.red,
              child: Text(
                _maintText,
                style: TextStyle(color: Colors.black),
              ),
            ),
            duration: Duration(milliseconds: 500),
          ),
          Expanded(
              child: ListView.builder(
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () {
                  setState(() {
                    _maintText = _list[index];
                  });
                },
                child: Container(
                  margin: EdgeInsets.all(12),
                  alignment: Alignment.center,
                  child: Text(_list[index]),
                ),
              );
            },
            itemCount: _list.length,
          ))
        ],
      ),
    );
  }
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM