flutterDev week Flutter widget (continually updated)

Backup Tutorial

flutter sample of example -

navigation:

  • AnimatedList delete and list of new Animated;

AnimatedList delete and list of new Animated;

Flutter sample from the rewrite simplified example, the effect of deletions into the data FadeTransition assembly, similar components have animation Size Transition , the Align Transition, SlideTransition, ScaleTransion, RotationTransition

 
 
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class AnimatedListSample extends StatefulWidget {
@override
_AnimatedListSampleState createState() => _AnimatedListSampleState();
}

class _AnimatedListSampleState extends State<AnimatedListSample> {
final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
List<int> _list;
int _selectedItem;
int _nextItem; // The next item inserted when the user presses the '+' button.

@override
void initState() {
super.initState();
_list = [0, 1, 2];
_nextItem = 3;
}

// Used to build list items that haven't been removed.
Widget _buildItem(
BuildContext context, int index, Animation<double> animation) {
return InkWell(
onTap: () {
setState(() {
_selectedItem = _list[index];
});
},
child: Card(
color: _selectedItem == _list[index] ? Colors.lightGreen : Colors.white,
child: FadeTransition(
opacity: animation.drive(Tween(begin: 0.0, end: 1.0)),
child: ListTile(
selected: _selectedItem == _list[index],
title: Text('${_list[index]}'),
),
),
),
);
}

Widget _buildRemovedItem(
int removedItem, BuildContext context, Animation<double> animation) {
return InkWell(
child: Card(
color: Colors.lightGreen,
child: FadeTransition(
opacity: animation.drive(Tween(begin: 0.0, end: 1.0)),
child: ListTile(
selected: true,
title: Text('${removedItem}'),
),
),
),
);
}

// Insert the "next item" into the list model.
void _insert() {
_list.insert(0, _nextItem++);
_listKey.currentState.insertItem(0, duration: Duration(milliseconds: 300));
}

// Remove the selected item from the list model.
void _remove() {
if (_selectedItem != null) {
int index = _list.indexOf(_selectedItem);
var removedItem = _list.removeAt(index);
print(removedItem);
_listKey.currentState.removeItem(index,
(BuildContext context, Animation<double> animation) {
return _buildRemovedItem(removedItem, context, animation);
});
setState(() {
_selectedItem = null;
});
}
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('AnimatedList'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.add_circle),
onPressed: _insert,
tooltip: 'insert a new item',
),
IconButton(
icon: const Icon(Icons.remove_circle),
onPressed: _remove,
tooltip: 'remove the selected item',
),
],
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: AnimatedList(
key: _listKey,
initialItemCount: _list.length,
itemBuilder: _buildItem,
),
),
),
);
}
}

void main() {
runApp(AnimatedListSample());
}
 

ListView && ExpansionTile  based ExpansionTile expandable achieve the ListView (tree structure)

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart'; class ExpansionTileSample extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('ExpansionTile'), ), body: ListView.builder( itemBuilder: (BuildContext context, int index) => EntryItem(data[index]), itemCount: data.length, ), ), ); } }
//元数据结构
class Entry { Entry(this.title, [this.children = const <Entry>[]]); final String title; final List<Entry> children; } //数据 final List<Entry> data = <Entry>[ Entry('Chapter A', <Entry>[ Entry('Section A0', <Entry>[ Entry('Item A0.1'), Entry('Item A0.2'), Entry('Item A0.3'), ], ), Entry('Section A1'), Entry('Section A2'), ], ), Entry('Chapter B', <Entry>[ Entry('Section B0'), Entry('Section B1'), ], ), Entry('Chapter C', <Entry>[ Entry('Section C0'), Entry('Section C1'), Entry('Section C2', <Entry>[ Entry('Item C2.0'), Entry('Item C2.1'), Entry('Item C2.2'), Entry('Item C2.3'), ], ), ], ), ]; classThe extends StatelessWidget {EntryItem const EntryItem ( the this .entry); Final entry the Entry; the Widget _buildTiles (the Entry the root) { IF (root.children.isEmpty) return ListTile (title: the Text (root.title)); return ExpansionTile ( Key: PageStorageKey <the Entry> (root), // save the page scroll offset, position offset can return to re-render the title: Text (root.title), Children: root.children.map <Widget> (_buildTiles) .ToList (), ); } @override the Widget Build (BuildContext context) { return _buildTiles (entry); } } void main() { runApp(ExpansionTileSample()); }

 TabBar&&TabBarView&&DefaultTabController&&TabPageSelector    标签页

Examples: TabPageSelector and TabBarView combination, create a TabController addition control;

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

......

class _AppBarBottomSampleState extends State<AppBarBottomSample> with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: choices.length);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void _nextPage(int delta) {
    final int newIndex = _tabController.index + delta;
    if (newIndex < 0 || newIndex >= _tabController.length)
      return;
    _tabController.animateTo(newIndex);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('AppBar Bottom Widget'),
          leading: IconButton(
            tooltip: 'Previous choice',
            icon: const Icon(Icons.arrow_back),
            onPressed: () { _nextPage(-1); },
          ),
          actions: <Widget>[
            IconButton(
              icon: const Icon(Icons.arrow_forward),
              tooltip: 'Next choice',
              onPressed: () { _nextPage(1); },
            ),
          ],
          bottom: PreferredSize(
            preferredSize: const Size.fromHeight(48.0),
            child: Theme(
              data: Theme.of(context).copyWith(accentColor: Colors.white),
              child: Container(
                height: 48.0,
                alignment: Alignment.center,
                child: TabPageSelector(controller: _tabController),
              ),
            ),
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: choices.map<Widget>((Choice choice) {
            return Padding(
              padding: const EdgeInsets.all(16.0),
              child: ChoiceCard(choice: choice),
            );
          }).toList(),
        ),
      ),
    );
  }
}

......
......

Example: Using DefaultTabController default control page TabBar and TabBarView

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

......

class TabbedAppBarSample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: choices.length,
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Tabbed AppBar'),
            bottom: TabBar(
              isScrollable: true,
              tabs: choices.map<Widget>((Choice choice) {
                return Tab(
                  text: choice.title,
                  icon: Icon(choice.icon),
                );
              }).toList(),
            ),
          ),
          body: TabBarView(
            children: choices.map<Widget>((Choice choice) {
              return Padding(
                padding: const EdgeInsets.all(16.0),
                child: ChoiceCard(choice: choice),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
}

......

Guess you like

Origin www.cnblogs.com/Merrys/p/11765459.html
Recommended