mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
accidentally pushed to master
This commit is contained in:
@@ -1,99 +0,0 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'tab_1.dart';
|
||||
import 'tab_2.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.green,
|
||||
),
|
||||
builder: (BuildContext context, Widget child) {
|
||||
return CupertinoTheme(
|
||||
data: CupertinoThemeData(),
|
||||
child: child,
|
||||
);
|
||||
},
|
||||
home: PlatformAdaptingHomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PlatformAdaptingHomePage extends StatefulWidget {
|
||||
PlatformAdaptingHomePage({ Key key }) : super(key: key);
|
||||
|
||||
@override
|
||||
_PlatformAdaptingHomePageState createState() => _PlatformAdaptingHomePageState();
|
||||
}
|
||||
|
||||
class _PlatformAdaptingHomePageState extends State<PlatformAdaptingHomePage> {
|
||||
final tab1Key = GlobalKey();
|
||||
final tab2Key = GlobalKey();
|
||||
|
||||
Widget _buildAndroidHomePage() {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(tab1Title)),
|
||||
drawer: Drawer(
|
||||
|
||||
),
|
||||
body: Tab1(key: tab1Key),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildIosHomePage() {
|
||||
return CupertinoTabScaffold(
|
||||
tabBar: CupertinoTabBar(
|
||||
items: <BottomNavigationBarItem>[
|
||||
BottomNavigationBarItem(
|
||||
title: Text(tab1Title),
|
||||
icon: tab1IosIcon,
|
||||
),
|
||||
BottomNavigationBarItem(
|
||||
title: Text(tab2Title),
|
||||
icon: tab2IosIcon,
|
||||
),
|
||||
],
|
||||
),
|
||||
tabBuilder: (BuildContext context, int index) {
|
||||
switch (index) {
|
||||
case 0:
|
||||
return CupertinoTabView(
|
||||
defaultTitle: tab1Title,
|
||||
builder: (BuildContext context) {
|
||||
return Tab1(key: tab1Key);
|
||||
},
|
||||
);
|
||||
case 1:
|
||||
return CupertinoTabView(
|
||||
defaultTitle: tab2Title,
|
||||
builder: (BuildContext context) {
|
||||
return Tab2(key: tab2Key);
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
return _buildAndroidHomePage();
|
||||
case TargetPlatform.iOS:
|
||||
return _buildIosHomePage();
|
||||
}
|
||||
|
||||
assert(false, 'Unexpected platform');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'utils.dart';
|
||||
|
||||
/// This file feeds the means to navigate to and the content of tab 1 of our
|
||||
/// app.
|
||||
|
||||
const tab1Title = 'Songs';
|
||||
const tab1IosIcon = Icon(CupertinoIcons.music_note);
|
||||
const _itemsLength = 50;
|
||||
|
||||
class Tab1 extends StatefulWidget {
|
||||
const Tab1({ Key key }) : super(key: key);
|
||||
|
||||
@override
|
||||
_Tab1State createState() => _Tab1State();
|
||||
}
|
||||
|
||||
class _Tab1State extends State<Tab1> {
|
||||
List<MaterialColor> colors;
|
||||
List<String> songNames;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
colors = getRandomColors(_itemsLength);
|
||||
songNames = getRandomNames(_itemsLength);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
Widget _listBuilder(BuildContext context, int index) {
|
||||
var color = defaultTargetPlatform == TargetPlatform.iOS
|
||||
? colors[index]
|
||||
: colors[index].shade400;
|
||||
|
||||
return Card(
|
||||
margin: EdgeInsets.symmetric(vertical: 16, horizontal: 16),
|
||||
elevation: 20,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: <Widget>[
|
||||
Container(
|
||||
color: color,
|
||||
height: 250,
|
||||
),
|
||||
_Banner(songNames[index]),
|
||||
_PlayButton(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAndroid(BuildContext context) {
|
||||
return ListView.builder(
|
||||
padding: EdgeInsets.symmetric(vertical: 12),
|
||||
itemBuilder: _listBuilder,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildIos(BuildContext context) {
|
||||
return CustomScrollView(
|
||||
slivers: <Widget>[
|
||||
CupertinoSliverNavigationBar(),
|
||||
SliverSafeArea(
|
||||
top: false,
|
||||
sliver: SliverPadding(
|
||||
padding: EdgeInsets.symmetric(vertical: 12),
|
||||
sliver: SliverList(
|
||||
delegate: SliverChildBuilderDelegate(_listBuilder),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
return _buildAndroid(context);
|
||||
case TargetPlatform.iOS:
|
||||
return _buildIos(context);
|
||||
}
|
||||
|
||||
assert(false, 'Unexpected platform');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class _Banner extends StatelessWidget {
|
||||
const _Banner(this.text);
|
||||
|
||||
final String text;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Positioned(
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: Container(
|
||||
height: 80,
|
||||
color: Colors.black12,
|
||||
alignment: Alignment.centerLeft,
|
||||
padding: EdgeInsets.symmetric(horizontal: 12),
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(fontSize: 21, fontWeight: FontWeight.w500),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PlayButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
height: 50,
|
||||
width: 50,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: Colors.black12,
|
||||
),
|
||||
alignment: Alignment.center,
|
||||
margin: EdgeInsets.only(bottom: 45),
|
||||
child: Icon(Icons.play_arrow, size: 50, color: Colors.black38),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:english_words/english_words.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_lorem/flutter_lorem.dart';
|
||||
|
||||
import 'utils.dart';
|
||||
|
||||
const tab2Title = 'News';
|
||||
const tab2IosIcon = Icon(CupertinoIcons.news);
|
||||
const _itemsLength = 20;
|
||||
|
||||
String _makeFakeNews(Random random) {
|
||||
final pair = generateWordPairs(random: random);
|
||||
final artist = capitalizePair(pair.first);
|
||||
|
||||
switch (random.nextInt(9)) {
|
||||
case 0:
|
||||
return '$artist says ${nouns[random.nextInt(nouns.length)]}';
|
||||
case 1:
|
||||
return '$artist arrested due to ${pair.first}';
|
||||
case 2:
|
||||
return '$artist releases ${capitalizePair(pair.first)}';
|
||||
case 3:
|
||||
return '$artist talks about his ${nouns[random.nextInt(nouns.length)]}';
|
||||
case 4:
|
||||
return '$artist talks about her ${nouns[random.nextInt(nouns.length)]}';
|
||||
case 5:
|
||||
return '$artist talks about their ${nouns[random.nextInt(nouns.length)]}';
|
||||
case 6:
|
||||
return '$artist says their music is inspired by ${pair.first}';
|
||||
case 7:
|
||||
return '$artist says the world needs more ${nouns[random.nextInt(nouns.length)]}';
|
||||
case 7:
|
||||
return '$artist calls their band ${adjectives[random.nextInt(adjectives.length)]}';
|
||||
case 8:
|
||||
return '$artist finally ready to talk about ${nouns[random.nextInt(nouns.length)]}';
|
||||
}
|
||||
}
|
||||
|
||||
class Tab2 extends StatefulWidget {
|
||||
const Tab2({ Key key }) : super(key: key);
|
||||
|
||||
@override
|
||||
_Tab2State createState() => _Tab2State();
|
||||
}
|
||||
|
||||
class _Tab2State extends State<Tab2> {
|
||||
List<Color> colors;
|
||||
List<String> titles;
|
||||
List<String> contents;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
final random = Random();
|
||||
colors = getRandomColors(_itemsLength);
|
||||
titles = List<String>.generate(
|
||||
_itemsLength,
|
||||
(int index) => _makeFakeNews(random),
|
||||
);
|
||||
contents = List<String>.generate(
|
||||
_itemsLength,
|
||||
(int index) => lorem(paragraphs: 1, words: 50),
|
||||
);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
Widget _listBuilder(BuildContext context, int index) {
|
||||
return Card(
|
||||
elevation: 1,
|
||||
margin: EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 12,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Row(
|
||||
children: <Widget>[
|
||||
CircleAvatar(
|
||||
backgroundColor: colors[index],
|
||||
),
|
||||
Padding(padding: EdgeInsets.only(left: 12)),
|
||||
Expanded(child: Text(titles[index])),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAndroid(BuildContext context) {
|
||||
return ListView.builder(
|
||||
itemBuilder: _listBuilder,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildIos(BuildContext context) {
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(),
|
||||
child: ListView.builder(
|
||||
itemBuilder: _listBuilder,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
case TargetPlatform.fuchsia:
|
||||
return _buildAndroid(context);
|
||||
case TargetPlatform.iOS:
|
||||
return _buildIos(context);
|
||||
}
|
||||
|
||||
assert(false, 'Unexpected platform');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:english_words/english_words.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
const _myListOfRandomColors = <MaterialColor>[
|
||||
Colors.red, Colors.blue, Colors.teal, Colors.yellow, Colors.amber,
|
||||
Colors.deepOrange, Colors.green, Colors.indigo, Colors.lime, Colors.pink,
|
||||
Colors.orange,
|
||||
];
|
||||
|
||||
List<MaterialColor> getRandomColors(int amount) {
|
||||
final random = Random();
|
||||
return List<MaterialColor>.generate(amount, (int index) {
|
||||
return _myListOfRandomColors[random.nextInt(_myListOfRandomColors.length)];
|
||||
});
|
||||
}
|
||||
|
||||
List<String> getRandomNames(int amount) {
|
||||
return generateWordPairs(maxSyllables: 5)
|
||||
.take(amount)
|
||||
.map((var pair) => capitalizePair(pair))
|
||||
.toList();
|
||||
}
|
||||
|
||||
String capitalize(String word) {
|
||||
return '${word[0].toUpperCase()}${word.substring(1).toLowerCase()}';
|
||||
}
|
||||
|
||||
String capitalizePair(WordPair pair) {
|
||||
return '${capitalize(pair.first)} ${capitalize(pair.second)}';
|
||||
}
|
||||
Reference in New Issue
Block a user